开发者

How to transform an XML with default namespace?

开发者 https://www.devze.com 2023-03-13 02:23 出处:网络
I need some to help oon generating the XSL file for my XML Data. Here is my XML Data <?xml-stylesheet href=\"C:\\Style.xsl\" type=\"text/xsl\" ?>

I need some to help oon generating the XSL file for my XML Data.

Here is my XML Data

<?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="Remove开发者_高级运维dUser" 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>

My XSL file is below

<?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 >


You are not correctly dealing with the default namespace present in the input document. If you do not associate a prefix to the corresponding namespace uri, the XSLT processor will search for elements in no namespace. Actually, the elements in your input document, are all in the namespace http://tempuri.org/.

So, you need first to declare the namespace prefix in the transform:

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

Then, you have to use the prefix accordingly. For instance:

                <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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号