使用 XSLT 复制 XML 时绕过名称空间 [英] Bypassing namespaces while copying an XML with XSLT
本文介绍了使用 XSLT 复制 XML 时绕过名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从具有默认命名空间的 XML 开始:
Starting from an XML with a default namespace:
<Root>
<A>foo</A>
<B></B>
<C>bar</C>
</Root>
我应用 XSLT 来删除 'C' 元素:
I apply an XSLT to remove the 'C' element:
<?xml version="1.0" ?>
<xsl:stylesheet version="2.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="no" encoding="utf-8" />
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="C" />
</xsl:stylesheet>
我最终得到以下 XML(没有折叠B"是可以的,因为我使用 HTML 作为输出方法):
and I end up with the following XML (it's OK to have 'B' not collapsed because I'm using HTML as output method):
<Root>
<A>foo</A>
<B></B>
</Root>
但是如果我得到另一个 XML,这次有一个命名空间:
But then if I ever get another XML, this time with a namespace:
<Root xmlns="http://company.com">
<A>foo</A>
<B></B>
<C>bar</C>
</Root>
在 XSLT 处理后不会删除C"元素.
the 'C' element is not removed after XSLT process.
我该怎么做才能绕过这个命名空间,有没有办法?
What can I do to bypass this namespace, is there a way?
推荐答案
不太值得推荐,但有效:
Not so recommendable, but works:
<xsl:template match="*[local-name()='C']" />
更好:
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foo="http://company.com"
exclude-result-prefixes="foo"
>
<!-- ... -->
<xsl:template match="C | foo:C" />
<!-- ... -->
</xsl:stylesheet>
这篇关于使用 XSLT 复制 XML 时绕过名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文