删除指定的节点及其子节点

时间:2013-12-20 10:22:04

标签: xml xslt-1.0

我有这样的XML。

<a>
  <b>
    <c>
      <e>
      </e>
    </c>
    <d>
    </d>
  </b>
  <b>
    <c>
      <e>
      <e>
    </c>
    <d>
    </d>
  </b>
</a>

我需要复制整个文档并删除所有<c>元素及其子元素。之后我想将新文档存储到变量中。

以下是转换后我想要的XML:

<a>
  <b>
    <d>
    </d>
  </b>
  <b>
    <d>
    </d>
  </b>
</a>

我现在拥有的东西:

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

 <xsl:template match="c"/>

1 个答案:

答案 0 :(得分:0)

我认为有两件事是错的:

  • 您的输入缺少第二个<e>元素的结束标记
  • 您的xslt脚本缺少标题

因此,将您的输入更改为如下形式:

<a>
  <b>
    <c>
      <e>
      </e>
    </c>
    <d>
    </d>
  </b>
  <b>
    <c>
      <e>
      </e>
    </c>
    <d>
    </d>
  </b>
</a>

在样式表周围添加标题和xsl:stylesheet标记,如下所示:

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
 <xsl:template match="c"/>
</xsl:stylesheet>

整个过程都有效,产生类似的输出:

<?xml version="1.0" encoding="UTF-8"?>
<a>
  <b>
    <d>
    </d>
  </b>
  <b>
    <d>
    </d>
  </b>
</a>
相关问题