您的输入 XML 位于名称空间,即默认命名空间:
<Report xmlns="testreport">
所有后代元素也将属于该命名空间。在你的情况下重要的是元素Report
那个叫{testreport}Report
(the {}
告诉您存在命名空间)是完全不同的元素。
所以,如果你的模板匹配
<xsl:template match="Report">
仅当找到名称为“Report”且不在命名空间中的元素时才会触发它。反而,
如果您需要寻址特定元素的名称并且它们位于命名空间中,则需要在 XSLT 样式表中声明此命名空间并为这些元素名称添加前缀:
<xsl:stylesheet xmlns:tst="testreport">
<xsl:template match="tst:Report">
<!--...-->
</xsl:template>
<!--...-->
</xsl:stylesheet>
你观察到
唯一与似乎有效的元素匹配的模板是"/"
, "*"
and "/*"
因为这些模式是通用模式,不使用需要前缀的特定元素名称。
XSLT 样式表
更多详细信息:无需重新声明xsi
样式表中的命名空间 - 我省略了它。另外,一些处理器保留空白节点,这会导致输出格式错误 - 我已经添加了xsl:strip-space
来解释这一点。
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:tst="testreport"
exclude-result-prefixes="tst">
<xsl:output method="xml" indent="yes" encoding="utf-8" />
<xsl:strip-space elements="*"/>
<!-- rule to suppress the undesired nodes -->
<xsl:template match="tst:Report|tst:ProductList|tst:Details_Collection">
<xsl:apply-templates/>
</xsl:template>
<!-- rule to rename the Details node -->
<xsl:template match="tst:Details">
<item>
<xsl:apply-templates/>
</item>
</xsl:template>
<!-- rule to copy everything else -->
<xsl:template match="*|@*">
<xsl:element name="{name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<!-- rule to start the conversion and provide the wrapper tags -->
<xsl:template match="/">
<rss version="2.0">
<channel>
<xsl:apply-templates/>
</channel>
</rss>
</xsl:template>
</xsl:stylesheet>
XML输出
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<item>
<ID>602</ID>
<Title>302</Title>
</item>
<item>
<ID>603</ID>
<Title>303</Title>
</item>
</channel>
</rss>