如何使用默认命名空间转换XML?

时间:2011-06-15 20:33:22

标签: xslt

我需要一些帮助为我的XML数据生成XSL文件。

这是我的XML数据

<?xml-stylesheet href="C:\Style.xsl" type="text/xsl" ?>    

<xml>
<ApproverRoles OperationType="RemovedUser" xmlns="http://tempuri.org/">
<UserName>Bhupathiraju, Venkata</UserName><UserRole>IT Owner</UserRole><RoleDescription>Role Owner
</RoleDescription><UserRoleID>138</UserRoleID></ApproverRoles>
<ApproverRoles OperationType="RemovedUser" xmlns="http://tempuri.org/">
<UserName>Bhupathiraju, Venkata</UserName><UserRole>Business Owner</UserRole>
<RoleDescription>Role Owner</RoleDescription><UserRoleID>136</UserRoleID></ApproverRoles>
<ApproverRoles OperationType="RemovedUser" xmlns="http://tempuri.org/"><UserName>Amperayeni, Kiran K</UserName>
<UserRole>IT Owner</UserRole><RoleDescription>asdasdasd</RoleDescription><UserRoleID>97</UserRoleID>
</ApproverRoles>
<ApproverRoles OperationType="RemovedUser" xmlns="http://tempuri.org/"><UserName>Amperayeni, Kiran K</UserName>
<UserRole>IT Owner</UserRole><RoleDescription>i</RoleDescription><UserRoleID>135</UserRoleID></ApproverRoles>
</xml>

我的XSL文件位于

之下
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match ="/" >
        <html>
            <head>
                <title>User Management</title>
            </head>
            <body>
                <table width="600" border="1" style='font-family:Calibri;font-size:10pt;background-color:#FFFFFF;border-color:#ccccff'>
            <tr bgcolor = "#ccccff" style='font-weight:bold;'>
                <td colspan="3">Proposed Users :</td>
            </tr>
            <tr bgcolor = "#cccccc" style='font-weight:bold;'>
                <td>User Name</td>
                <td>Role</td>
                <td>Role Qualifier</td>
            </tr>

            <xsl:for-each select="//ns1:ApproverRoles" >
                <tr>
                    <td>
                        <xsl:value-of select="UserName" />
                    </td>
                    <td>
                        <xsl:value-of select="UserRole" />
                    </td>
                    <td>
                        <xsl:value-of select="RoleDescription" />
                    </td>
                </tr>
            </xsl:for-each>
            <tr bgcolor = "#ccccff" style='font-weight:bold;'>
                <td colspan="3">Removed Users :</td>
            </tr>
            <tr bgcolor = "#cccccc" style='font-weight:bold;'>
                <td>User Name</td>
                <td>Role</td>
                <td>Role Qualifier</td>
            </tr>
        </table>
            </body>
        </html>
    </xsl:template>


</xsl:stylesheet >

1 个答案:

答案 0 :(得分:2)

您未正确处理输入文档中存在的默认命名空间。如果不将前缀与相应的名称空间uri相关联,则XSLT处理器将搜索无名称空间中的元素。实际上,输入文档中的元素都在命名空间http://tempuri.org/中。

因此,您首先需要在转换中声明名称空间前缀:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns1="http://tempuri.org/">

然后,您必须相应地使用前缀。例如:

                <xsl:for-each select="//ns1:ApproverRoles" >
                    <tr>
                        <td>
                            <xsl:value-of select="ns1:UserName" />
                        </td>
                        <td>
                            <xsl:value-of select="ns1:UserRole" />
                        </td>
                        <td>
                            <xsl:value-of select="ns1:RoleDescription" />
                        </td>
                    </tr>
                </xsl:for-each>