我有一个
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>
我想在它上面应用XSL,以便< weight>标签需要转换为权重并考虑唯一的数字.这样我想添加一些额外的标签,这在输入XML中不存在.< unitWeights>如果< weight>的值只是预定,然后它会显示KG休息它将是空白的.
预期产量:
<?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>
为此我申请了:
<xsl:template match="Weight">
<weight>
<xsl:value-of select="translate(.,translate(., '0123456789', ''), '')"/>
</weight>
</xsl:template>
<xsl:template match="WeightUnits">
<unitWeights>KG</unitWeights>
</xsl:template>
请帮我解决我错的地方.
最佳答案
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="weight">
<Weight>
<xsl:copy-of select="substring-before(.,'kg')"/>
</Weight>
<unitWeights>
<xsl:copy-of select="substring(., string-length(.) - 1)"/>
</unitWeights>
</xsl:template>
<xsl:template match="unitWeights"/>
</xsl:stylesheet>