我想使用 XSLT 从 XML 文件中删除名为“searchBookResponse”的元素(包装器 - 而不是它的后代)。
当“searchBookResponse”元素没有属性或非“xmlns”属性时,我的 XSLT 代码工作得很好。但是,有了这些 xmlns 属性,它就不再起作用了。
这是输入的 XML 文档:
<?xml version="1.0" encoding="utf-8"?>
<books>
<searchBookResponse xmlns="urn:example.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<book>
<author>Tom</author>
<title>Test</title>
</book>
<book>
<author>David</author>
<title>Test two</title>
</book>
</searchBookResponse>
<book>
<author>Anas</author>
<title>Test two</title>
</book>
<book>
<author>Jim</author>
<title>Test two</title>
</book>
</books>
Run Code Online (Sandbox Code Playgroud)
这是 XSL 样式表:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<xsl:template match="@xmlns" />
<xsl:template match="searchBookResponse">
<xsl:apply-templates select="node()" />
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
应用后,输出 XML 文件与输入 XML 文件相同。我必须先应用一些字符串模式匹配来删除 …