xslt可以正确替换属性的值,但也可以清空其他属性 [英] xslt replaces attribute's value correctly, but also empties other attributes

查看:53
本文介绍了xslt可以正确替换属性的值,但也可以清空其他属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xslt 1.0,下面是我的xml文件.如果code ="Hello",我想将属性代码的值更新为"Chao".我写了一个小脚本,它确实将code ="Hello"更新为code ="Chao";但是,它也清空其他代码属性.你能帮忙吗?** XML

<Items>
<Item itemIdentifier="07068283" code="Hello" />
<Item itemIdentifier="07059182" code="Hello" />
<Item itemIdentifier="07063805" code="Bye" />
<Item itemIdentifier="07064878" code="Bye" />
</Items>

代码**

<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>

结果

<Items>
   <Item itemIdentifier="07068283" code="Chao"/>
   <Item itemIdentifier="07059182" code="Chao"/>
   <Item itemIdentifier="07063805" code=""/>
   <Item itemIdentifier="07064878" code=""/>
</Items>

推荐答案

尝试:

<xsl:template match="Item/@code[.='Hello']">
    <xsl:attribute name="code">Chao</xsl:attribute>
</xsl:template>

这篇关于xslt可以正确替换属性的值,但也可以清空其他属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆