使用XSLT保留XML字段和一个子节点

时间:2015-12-26 09:14:27

标签: xml xslt xpath lxml

我正在尝试将XML文档修剪为新的XML文档。我有以下XML:

<rpc-reply>
  <configuration changed-seconds="1450987515" changed-localtime="2015-12-24 20:05:15 UTC">
    <routing-instances>
      <instance>
        <name>SOMENAME</name>
        <instance-type>virtual-router</instance-type>
        <interface>
          <name>lo0.1</name>
        </interface>
        <protocols>
          <bgp>
            <group>
              <name>EBGP-TEST</name>
              <type>external</type>
              <neighbor>
                <name>1.1.1.1</name>
                <peer-as>7222</peer-as>
              </neighbor>
            </group>
          </bgp>
          <ospf>
            <area>
              <name>0.0.0.0</name>
              <comment>SOME COMMENT</comment>
              <interface>
                <name>all</name>
              </interface>
            </area>
          </ospf>
        </protocols>
      </instance>
    </routing-instances>
  </configuration>
  <cli>
    <banner>[edit]</banner>
  </cli>
</rpc-reply>

这就是我需要的:

<rpc-reply>
  <configuration changed-seconds="1450987515" changed-localtime="2015-12-24 20:05:15 UTC">
    <routing-instances>
      <instance>
        <name>SOMENAME</name>
        <protocols>
          <ospf>
            <area>
              <name>0.0.0.0</name>
              <comment>SOME COMMENT</comment>
              <interface>
                <name>all</name>
              </interface>
            </area>
          </ospf>
        </protocols>
      </instance>
    </routing-instances>
  </configuration>
</rpc-reply>

这是我正在使用的XSLT代码:

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

  <ns:WhiteList>
   <name>area</name>
   <name>interface</name>
   <name>instance</name>
   <name>metric</name>
  </ns:WhiteList>

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

 <xsl:template match="*[not(descendant-or-self::*[name()=document('')/*/ns:WhiteList/*])]"/>

 </xsl:stylesheet>

它几乎可以运作:

<rpc-reply>
  <configuration changed-seconds="1450987515" changed-localtime="2015-12-24 20:05:15 UTC">
    <routing-instances>
      <instance>
        <interface/>
        <protocols>
          <ospf>
            <area>
              <interface/>
            </area>
          </ospf>
        </protocols>
      </instance>
    </routing-instances>
  </configuration>
</rpc-reply>

我只需要孩子“<name>”来获取有趣的元素(如果有的话)和奖励,如果我能以某种方式得到“<comment>”正好在“<interface>”之上 尝试了不同的组合,但不知道如何做到这一点。

1 个答案:

答案 0 :(得分:3)

  

我只需要孩子&#34; <name>&#34;对于有趣的元素(如果有的话)   是那里的一个)

您可以添加一个模板来专门处理它:

<xsl:template match="name[name(..)=document('')/*/ns:WhiteList/*]" priority="1">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>
  如果我能以某种方式获得&#34; <comment>&#34;正上方   &#34; <interface>&#34;

类似地:

<xsl:template match="comment[following-sibling::*[1][self::interface]]" priority="1">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

为避免重复代码,请尝试使用更简化的版本:

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

<ns:WhiteList>
    <name>area</name>
    <name>interface</name>
    <name>instance</name>
    <name>metric</name>
</ns:WhiteList>

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

<xsl:template match="*[not(descendant-or-self::*[name()=document('')/*/ns:WhiteList/*])]"/>

<xsl:template match="name[name(..)=document('')/*/ns:WhiteList/*]" priority="1">
    <xsl:call-template name="identity"/>
</xsl:template>

<xsl:template match="comment[following-sibling::*[1][self::interface]]" priority="1">
    <xsl:call-template name="identity"/>
</xsl:template>

</xsl:stylesheet>