删除名称空间并使用XSL提取XML文件的子集
当我的输入Xml为:删除名称空间并使用XSL提取XML文件的子集,xml,xslt,xml-namespaces,Xml,Xslt,Xml Namespaces,当我的输入Xml为: <country> <state> <city> <name>DELHI</name> </city> </state> </country> 德里 对于要求的输出,如下所示: <city> <name>DELHI
<country>
<state>
<city>
<name>DELHI</name>
</city>
</state>
</country>
德里
对于要求的输出,如下所示:
<city>
<name>DELHI</name>
</city
德里
这是有关XPath、XML和XSLT的最常见问题解答。搜索“默认命名空间和XPath表达式”。
至于解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://india.com/states">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="*[not(ancestor-or-self::x:city)]">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
<country xmlns="http://india.com/states" version="1.0">
<state>
<city>
<name>DELHI</name>
</city>
</state>
</country>
<city>
<name>DELHI</name>
</city>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://india.com/states">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="*[not(ancestor-or-self::x:city)]">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()[not(ancestor::x:city)]"/>
</xsl:stylesheet>
<city>
<name>DELHI</name>
</city>
为了不产生文本“孟买”,需要稍微修改上面的转换——忽略(而不是复制)任何没有x:city
祖先的文本节点。为此,我们添加以下一行空模板:
<xsl:template match="text()[not(ancestor::x:city)]"/>
整个转变现在变成了:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://india.com/states">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="*[not(ancestor-or-self::x:city)]">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
<country xmlns="http://india.com/states" version="1.0">
<state>
<city>
<name>DELHI</name>
</city>
</state>
</country>
<city>
<name>DELHI</name>
</city>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://india.com/states">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="*[not(ancestor-or-self::x:city)]">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()[not(ancestor::x:city)]"/>
</xsl:stylesheet>
<city>
<name>DELHI</name>
</city>
而结果仍然是想要的,正确的结果:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://india.com/states">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="*[not(ancestor-or-self::x:city)]">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
<country xmlns="http://india.com/states" version="1.0">
<state>
<city>
<name>DELHI</name>
</city>
</state>
</country>
<city>
<name>DELHI</name>
</city>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://india.com/states">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="*[not(ancestor-or-self::x:city)]">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()[not(ancestor::x:city)]"/>
</xsl:stylesheet>
<city>
<name>DELHI</name>
</city>
德里
您可以使用以下模板获得所需的输出:
<xsl:template match="*[not(ancestor-or-self::x:*[starts-with(name(),'city')])]">
<xsl:apply-templates/>
</xsl:template>
或
使用Microsoft(R)XSLT Processor 4.0版对您的新输入进行测试:
<city>
<name>DELHI</name>
</city>
<city2>
<name2>MUMBAI</name2>
</city2>
德里
孟买
问得好,+1。请参阅我的答案,以获得完整、简短且简单的解决方案。详细的解释也提供了。我更新了我对修改后的问题“另一个约翰”的答案。如果是您,请联系SO合并您的两个身份。