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

耗尽温柔 提交于 2020-01-06 05:26:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!