I have XML:
<?xml version="1.0" encoding="UTF-8"?>
<COLLECTION>
<Added>
<part>
<weight>5kg</weight>
</part>
<part>
<weight></weight>
</part>
<part>
<weight>2kg</weight>
</part>
</Added>
<Update>
<part>
<weight></weight>
</part>
<part>
<weight>3kg</weight>
</part>
<part>
<weight>2kg</weight>
</part>
</Update>
</COLLECTION>
I want to apply XSL on it so that the tag <weight>needs to be converted to Weightand takes into account a single number. With this, I want to add an additional tag that is missing in the input XML. <unitWeights>if the value <weight>is only presnt, and only then it will show the remnants of KG, it will be empty.
expected output:
<?xml version="1.0" encoding="UTF-8"?>
<COLLECTION>
<Added>
<part>
<Weight>5</Weight>
<unitWeights>KG</unitWeights>
</part>
<part>
<Weight></Weight>
<unitWeights></unitWeights>
</part>
<part>
<Weight>2</Weight>
<unitWeights>KG</unitWeights>
</part>
</Added>
<Update>
<part>
<Weight></Weight>
<unitWeights></unitWeights>
</part>
<part>
<Weight>3</Weight>
<unitWeights>KG</unitWeights>
</part>
<part>
<Weight>2</Weight>
<unitWeights>KG</unitWeights>
</part>
</Update>
</COLLECTION>
for this I applied:
<xsl:template match="Weight">
<weight>
<xsl:value-of select="translate(.,translate(., '0123456789', ''), '')"/>
</weight>
</xsl:template>
<xsl:template match="WeightUnits">
<unitWeights>KG</unitWeights>
</xsl:template>
Please help me where I am wrong.
source
share