lundi 2 décembre 2019

xslt replaces attribute's value correctly, but also empties other attributes

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>

Aucun commentaire:

Enregistrer un commentaire