问题
i'm using xslt 1.0, below is my xml file. I would like to update value of attribute code to "Chao" if code="Hello". I wrote a little script, it does update code="Hello" to code="Chao"; however, it also empties other code attribute. can you please help? **XML
<Items>
<Item itemIdentifier="07068283" code="Hello" />
<Item itemIdentifier="07059182" code="Hello" />
<Item itemIdentifier="07063805" code="Bye" />
<Item itemIdentifier="07064878" code="Bye" />
</Items>
CODE**
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Item/@code">
<xsl:attribute name="code">
<xsl:if test=". = 'Hello'">Chao</xsl:if>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
RESULT
<Items>
<Item itemIdentifier="07068283" code="Chao"/>
<Item itemIdentifier="07059182" code="Chao"/>
<Item itemIdentifier="07063805" code=""/>
<Item itemIdentifier="07064878" code=""/>
</Items>
回答1:
Try:
<xsl:template match="Item/@code[.='Hello']">
<xsl:attribute name="code">Chao</xsl:attribute>
</xsl:template>
来源:https://stackoverflow.com/questions/59143630/xslt-replaces-attributes-value-correctly-but-also-empties-other-attributes