namespace阻止xslt将xml转换为html

时间:2015-11-11 02:56:45

标签: xml xslt

我发现标签中的命名空间"音乐"阻止xslt成功将xml转换为html。

XML文档:

 <?xml-stylesheet type="text/xsl" href="cd-demo.xsl"?>
    <catalog xmlns:junos="http://xml.test.com">
        <music xmlns="http://xml.test.org">
            <cd>
                <title>Empire Burlesque</title>
                <artist>Bob Dylanee</artist>
            </cd>
        </music>
    </catalog>

如果我删除&#34; xmlns =&#34; http://xml.test.org" ;,转换将成功。

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/music/cd">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="artist"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

在此xml中,music节点和所有子节点都在命名空间http://xml.test.org中。因此,当您访问它们时,您需要指定正确的命名空间。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:test="http://xml.test.org" exclude-result-prefixes="test">
    <xsl:template match="/">
        <html>
            <body>
                <h2>My CD Collection</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Title</th>
                        <th>Artist</th>
                    </tr>
                    <xsl:for-each select="catalog/test:music/test:cd">
                        <tr>
                            <td>
                                <xsl:value-of select="test:title"/>
                            </td>
                            <td>
                                <xsl:value-of select="test:artist"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>