Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用XSLT保留XML字段和一个子项_Xml_Xslt_Xpath_Lxml - Fatal编程技术网

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

使用XSLT保留XML字段和一个子项,xml,xslt,xpath,lxml,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>

我正在尝试将一个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>

名字
虚拟路由器
lo0.1
EBGP-T
外部的
1.1.1.1
7222
0.0.0.0
一些评论
全部的
[编辑]
这就是我需要的:

<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>

名字
0.0.0.0
一些评论
全部的
以下是我正在使用的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>

我只需要子元素“
”(如果有)和奖金,如果我能以某种方式获得上面的“
” 尝试了不同的组合,但不确定如何进行

我只需要子元素“
”来获取感兴趣的元素(如果有 有一个吗

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

<xsl:template match="name[name(..)=document('')/*/ns:WhiteList/*]" priority="1">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

如果我能以某种方式得到上面的“
”的话,我还有额外的奖励 “

同样地:

<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>

地区
接口
实例
米制的
我只需要子元素“
”来获取感兴趣的元素(如果有 有一个吗

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

<xsl:template match="name[name(..)=document('')/*/ns:WhiteList/*]" priority="1">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

如果我能以某种方式得到上面的“
”的话,我还有额外的奖励 “

同样地:

<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>

地区
接口
实例
米制的

请定义“有趣的元素”。@michael.hor257k“白名单”部分中的元素,基于我的解决方案,既然
在白名单中,那么
lo0.1
应该在所需的输出中吗?@unutbu理想情况下,我想跳过界面的这一部分,但这不是一个很强的要求,因为它的XPath是
//instance/interface/name
,所以应该排除它,还是因为其他原因而排除它?请定义“有趣的元素”。@michael.hor257k“白名单”部分中的元素,基于此,我的解决方案,因为
在白名单中,
lo0.1
应该在期望的输出中吗?@unutbu理想情况下,我想跳过接口的这一部分,但这不是一个很强的要求,因为它的XPath是
//instance/interface/name
,所以应该将其排除在外吗?太好了!谢谢“优先级”的作用是什么?无论如何,只有当它不为空或与“某些文本”匹配时才有注释?“优先级”的作用是什么?”它解决了两个或多个模板匹配同一节点时的冲突。如果不显式提高优先级,添加的规则将具有与上一个模板相同的0.5优先级。“无论如何,要仅在注释不为空或与“某些文本”匹配时才具有注释,请添加另一个谓词。太好了!谢谢“优先级”的作用是什么?无论如何,只有当它不为空或与“某些文本”匹配时才有注释?“优先级”的作用是什么?”它解决了两个或多个模板匹配同一节点时的冲突。如果不显式提高优先级,添加的规则将具有与上一个模板相同的0.5优先级。“无论如何,要仅在注释不为空或与“某些文本”匹配时才具有注释,请添加另一个谓词。”。