Shawn.X
Shawn.X

Reputation: 3

XML transform with XSLT, issue with namespace

I have an issue with transforming an xml data by using xslt template. I guess the issue is about the namespace in the xml, after I remove the namespace xmlns="http://schemas.microsoft.com/sharepoint/soap/, everything is working fine.

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

Upvotes: 0

Views: 1426

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52848

Just add a namespace to your stylesheet and it will work fine. Here's your stylesheet with the namespace ms used. You can use whatever prefix you want though:

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

This produces the following output:

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

Alternatively, in XSLT 2.0, you can just use an asterisk (*) for the prefix and not add a namespace at all:

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

This will produce the same output as the previous example.

Upvotes: 2

Related Questions