使用XSLT从xml中删除除特定节点之外的所有节点

时间:2010-05-26 16:20:39

标签: xml xslt xpath

我有一堆xml文件,其中包含不同数量的数据节点,我想使用XSLT更改文件以仅包含特定节点。 例如:

<?xml version="1.0" encoding="UTF-8"?> 
 <SomeName> 
 <identifier> 
    <UID> 1234 </UID> 
 </identifier> 
 <MainNode1> 
     <SubNode1> 
        <Subnode1a>DATA1a0</Subnode1a> 
     </SubNode1> 
     <SubNode1> 
        <Subnode1a>DATA1a1</Subnode1a> 
     </SubNode1> 
     <SubNode1> 
        <Subnode1a>DATA1a2</Subnode1a> 
     </SubNode1> 
  </MainNode1> 

  <MainNode2> 
     <SubNode2> 
        <Subnode2a>DATA2a0</Subnode2a> 
     </SubNode2> 
  </MainNode2> 

  <MainNodeIDONTCARE> 
       <SubnodeWhatever> 
       </SubnodeWhatever> 
  </MainNodeIDONTCARE> 

  <MainNodeuseless> 
       <SubnodeWhatever> 
       </SubnodeWhatever> 
  </MainNodeuseless>

  <MainNodewhatever> 
       <SubnodeWhatever> 
       </SubnodeWhatever> 
  </MainNodewhatever>
</SomeName> 

现在我的最终XML文件应如下所示:

<?xml version="1.0" encoding="UTF-8"?> 
 <SomeName> 
 <identifier> 
    <UID> 1234 </UID> 
 </identifier> 
 <MainNode1> 
     <SubNode1> 
        <Subnode1a>DATA1a0</Subnode1a> 
     </SubNode1> 
     <SubNode1> 
        <Subnode1a>DATA1a1</Subnode1a> 
     </SubNode1> 
     <SubNode1> 
        <Subnode1a>DATA1a2</Subnode1a> 
     </SubNode1> 
  </MainNode1> 

  <MainNode2> 
     <SubNode2> 
        <Subnode2a>DATA2a0</Subnode2a> 
     </SubNode2> 
  </MainNode2>
</SomeName> 

我一直试图用XSLT完成它,但我似乎无法完成它。

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

可能最短的解决方案如下

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template
 match="MainNodeIDONTCARE | MainNodeuseless | MainNodewhatever"/>
</xsl:stylesheet>

在提供的XML文档上应用此转换时,会生成所需的输出

<SomeName>
    <identifier>
        <UID> 1234 </UID>
    </identifier>
    <MainNode1>
        <SubNode1>
            <Subnode1a>DATA1a0</Subnode1a>
        </SubNode1>
        <SubNode1>
            <Subnode1a>DATA1a1</Subnode1a>
        </SubNode1>
        <SubNode1>
            <Subnode1a>DATA1a2</Subnode1a>
        </SubNode1>
    </MainNode1>
    <MainNode2>
        <SubNode2>
            <Subnode2a>DATA2a0</Subnode2a>
        </SubNode2>
    </MainNode2>
</SomeName>

请注意使用最基本的XSLT设计模式:使用和覆盖身份规则

答案 1 :(得分:1)

这应该有效:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="SomeName">
    <xsl:copy>
      <xsl:for-each select="identifier|MainNode1|MainNode2">
        <xsl:copy>
          <xsl:apply-templates />
        </xsl:copy>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

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

</xsl:stylesheet>

答案 2 :(得分:0)

这是

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

  <xsl:template match="SomeName">
    <xsl:copy>
      <xsl:for-each select="identifier|MainNode1|MainNode2">
        <xsl:apply-templates select="." />
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

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