使用 XSLT 保留 XML 个字段和一个 child
Keep XML fields and one child using XSLT
我正在尝试将 trim 一个 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>
我只需要 child“<name>
”来获取有趣的元素(如果有的话),如果我能以某种方式获得正上方的“<comment>
”,我将获得奖励<interface>
”
尝试了不同的组合,但不确定该怎么做。
I just need the child "<name>
" for the interesting elements (if there
is one there)
您可以添加一个模板来专门处理它:
<xsl:template match="name[name(..)=document('')/*/ns:WhiteList/*]" priority="1">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
and bonus if I can somehow get the "<comment>
" right above
"<interface>
"
同样:
<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>
我正在尝试将 trim 一个 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>
我只需要 child“<name>
”来获取有趣的元素(如果有的话),如果我能以某种方式获得正上方的“<comment>
”,我将获得奖励<interface>
”
尝试了不同的组合,但不确定该怎么做。
I just need the child "
<name>
" for the interesting elements (if there is one there)
您可以添加一个模板来专门处理它:
<xsl:template match="name[name(..)=document('')/*/ns:WhiteList/*]" priority="1">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
and bonus if I can somehow get the "
<comment>
" right above "<interface>
"
同样:
<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>