2017-04-11 75 views
0

我有一个XML象下面这样:XSLT更新节点值根据病情

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<properties> 
<entry key="user">1234</entry> 
<entry key="name">sam</entry> 
</properties> 

我想改变的关键=“用户”标签的价值。如果key =“user”的初始值是1234,我希望输出xml中key =“user”的值为“test”,或者如果值为6666,则输出xml中key =“user”的值为“你好“使用xslt。 输出XML应该是这样的

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<properties> 
<entry key="user">test</entry> 
<entry key="name">sam</entry> 
</properties> 

我尝试使用此XSLT,但没有得到期望的输出。

<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="@key[.='1234']"> 
<xsl:attribute name="key"> 
<xsl:value-of select="'test'"/> 
</xsl:attribute> 
</xsl:template> 

有人可以帮助我,因为我是XSLT的新手。

回答

1

您在要更改其值的元素的属性节点上匹配。你需要在这些元素上匹配的模板。有很多方法可以做到这一点,这里有一种方法:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="2.0"> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="entry[@key='user'][.='1234']"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
      <xsl:text>test</xsl:text> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="entry[@key='user'][.='6666']"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
      <xsl:text>hello</xsl:text> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 
+0

它很好用。非常感谢 –