Replace attribute value using some complicated match expression
I want to replace xml tags that look like this: <indicator itype="ST" ind="U"/>
with <indicator itype="ST" ind="HELLO"/>
. The xslt stylesheet I'm using looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match = "@*|node()">
<xsl:copy>
<xsl:apply-templates select = "@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match = "indicator[@itype='ST' and @ind='U']">
<xsl:attribute name = "ind">
<xsl:text>HELLO</xsl:text>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
The stylesheet doesn't work and the processor throws开发者_JAVA技巧 an exception. How can I correct it?
If you want to replace an attribute value, then you need to match the attribute node. This stylesheet:
<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="indicator[@itype='ST']/@ind[.='U']">
<xsl:attribute name="ind">
<xsl:text>HELLO</xsl:text>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
Output:
<indicator itype="ST" ind="HELLO"></indicator>
精彩评论