2011-06-15 63 views
0

我需要一些幫助來爲我的XML數據生成XSL文件。如何使用默認命名空間轉換XML?

這裏是我的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 > 
+0

Xsl的輸出是什麼? – Filburt 2011-06-15 20:43:04

+0

僅供參考,將您問題的標題輸入Google可獲得202,000次點擊,其中很多都包含問題的答案。 – 2011-06-15 22:45:55

回答

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>