从具有默认命名空间的XML开始:
<Root>
<A>foo</A>
<B></B>
<C>bar</C>
</Root>
Run Code Online (Sandbox Code Playgroud)
我应用XSLT来删除'C'元素:
<?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>
Run Code Online (Sandbox Code Playgroud)
我最终得到了以下XML(可以让'B'没有崩溃,因为我使用HTML作为输出方法):
<Root>
<A>foo</A>
<B></B>
</Root>
Run Code Online (Sandbox Code Playgroud)
但是如果我得到另一个XML,这次使用命名空间:
<Root xmlns="http://company.com">
<A>foo</A>
<B></B>
<C>bar</C>
</Root>
Run Code Online (Sandbox Code Playgroud)
XSLT进程后不会删除'C'元素.
我可以做些什么来绕过这个命名空间,有办法吗?
不太值得推荐,但有效:
<xsl:template match="*[local-name()='C']" />
Run Code Online (Sandbox Code Playgroud)
更好:
<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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1024 次 |
| 最近记录: |