XSLT仅复制子数据

时间:2019-04-02 16:01:12

标签: xml xslt

我有一些看起来像这样的XML数据:

<data>
  <old><value>1</value></old>
  <new><value>2</value></new>
</data>

我正在尝试仅捕获<new>下的数据,因此结果应为:

<value>2</value>

我正在使用以下XSLT:

<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="new">
    <xsl:copy-of  select="node()" />
  </xsl:template>
</xsl:stylesheet>

但是,输出保留了<old>中的一些杂散数据,看起来像这样:

1
<value>2</value>

我做错了什么?

2 个答案:

答案 0 :(得分:1)

或者简单地:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    

<xsl:template match="/data">
    <xsl:copy-of select="new/node()" />
</xsl:template>

</xsl:stylesheet>

答案 1 :(得分:0)

我知道了...过早发布了这个问题!我需要添加一个xsl模板,以忽略旧版本下的所有内容。

<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="new">
    <xsl:copy-of  select="node()" />
  </xsl:template>
  <xsl:template match="old" />
</xsl:stylesheet>