2011-10-22 38 views
0

我遇到了使用xslt模板轉換xml數據的問題。我想這個問題是關於XML中的命名空間,我刪除名稱空間xmlns="http://schemas.microsoft.com/sharepoint/soap/後,一切工作正常。使用XSLT進行XML轉換,與命名空間問題

<?xml version="1.0"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
     <GetListCollectionResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/"> 
      <GetListCollectionResult> 
       <Lists> 
        <List Title="Announcement1" Description="Announcement 1"/> 
        <List Title="Announcement2" Description="Announcement 2"/> 
       </Lists> 
      </GetListCollectionResult> 
     </GetListCollectionResponse> 
    </soap:Body> 
</soap:Envelope> 

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    exclude-result-prefixes="msxsl"> 

    <xsl:template match="//Lists"> 
    <table> 
     <xsl:for-each select="List"> 
     <tr> 
      <td> 
      <xsl:value-of select="@Title"/>: 
      </td> 
      <td> 
      <xsl:value-of select="@Description"/> 
      </td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </xsl:template> 
</xsl:stylesheet> 

回答

2

一個命名空間只需添加到您的樣式表,它會正常工作。這是您的樣式表,其中使用了命名空間ms。你可以使用任何你想要儘管任何前綴:

<xsl:stylesheet version="1.0" 
    xmlns:ms="http://schemas.microsoft.com/sharepoint/soap/" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    exclude-result-prefixes="msxsl ms"> 

    <xsl:template match="//ms:Lists"> 
    <table> 
     <xsl:for-each select="ms:List"> 
     <tr> 
      <td> 
      <xsl:value-of select="@Title"/>: 
      </td> 
      <td> 
      <xsl:value-of select="@Description"/> 
      </td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </xsl:template> 
</xsl:stylesheet> 

這將產生以下的輸出:

<table><tr><td>Announcement1: 
    </td><td>Announcement 1</td></tr><tr><td>Announcement2: 
    </td><td>Announcement 2</td></tr></table> 

或者,在XSLT 2.0,您只需使用一個星號(*)爲前綴和不加一個命名空間:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    exclude-result-prefixes="msxsl"> 

    <xsl:template match="//*:Lists"> 
    <table> 
     <xsl:for-each select="*:List"> 
     <tr> 
      <td> 
      <xsl:value-of select="@Title"/>: 
      </td> 
      <td> 
      <xsl:value-of select="@Description"/> 
      </td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </xsl:template> 
</xsl:stylesheet> 

這將產生與上例相同的輸出。

+0

xmlns:ms =「http://schemas.microsoft.com/sharepoint/soap/」它確實有幫助,非常感謝。 –

+1

@ Shawn.X:當它確實有幫助時,那麼你必須接受答案。這就是我們如何在SO上說「謝謝」。 –

+0

非常歡迎@ Shawn.X。像Dimitre指出的那樣,請標記我接受的答案。謝謝! –