xml – Add Namespace Prefix To root node

xml – Add Namespace Prefix To root node

This is how you can give the document element a different namespace and move all of the other elements into the null namespace:

<xsl:stylesheet version=1.0 xmlns_xsl=http://www.w3.org/1999/XSL/Transform
                xmlns_ns0=https://api.ladbrokes.com/v1/sportsbook-couchbase/Temp.xsd>
  <xsl:output method=xml indent=yes omit-xml-declaration=yes/>

  <xsl:template match=@* | node()>
    <xsl:copy>
      <xsl:apply-templates select=@* | node()/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match=/*>
    <xsl:element name=ns0:{local-name()}>
      <xsl:apply-templates select=@* | node() />
    </xsl:element>
  </xsl:template>

  <xsl:template match=*/*>
    <xsl:element name={local-name()}>
      <xsl:apply-templates select=@* | node() />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

When run on your sample input, the result is:

<ns0:Class xmlns_ns0=https://api.ladbrokes.com/v1/sportsbook-couchbase/Temp.xsd>
  <blurb>Test</blurb>
  <channels>
    <e>I</e>
    <e>J</e>
    <e>K</e>
  </channels>
  <classSortCode>Test</classSortCode>
  <classStatus>Test</classStatus>
  <creationDateTime>2013-03-21T22:29:01.58+05:30</creationDateTime>
  <isActive>true</isActive>
  <lastUpdatedDateTime>2013-03-21T22:29:01.58+05:30</lastUpdatedDateTime>
  <locale>Test</locale>
</ns0:Class>

For clarification, this is how you can change the namespace for the document element and leave everything else in the namespace they already had.

<xsl:stylesheet version=1.0 xmlns_xsl=http://www.w3.org/1999/XSL/Transform
                xmlns_ns0=https://api.ladbrokes.com/v1/sportsbook-couchbase/Temp.xsd>
  <xsl:output method=xml indent=yes omit-xml-declaration=yes/>

  <xsl:template match=@* | node()>
    <xsl:copy>
      <xsl:apply-templates select=@* | node()/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match=/*>
    <xsl:element name=ns0:{local-name()}>
      <xsl:copy-of select=namespace::* />
      <xsl:apply-templates select=@* | node() />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

And this is the result from that. Note the small but critical difference in the namespace declarations at the top:

<ns0:Class xmlns_ns0=https://api.ladbrokes.com/v1/sportsbook-couchbase/Temp.xsd 
     xmlns=https://api.ladbrokes.com/v1/sportsbook-couchbase/SportsbookCouchbase.xsd>
  <blurb>Test</blurb>
  <channels>
    <e>I</e>
    <e>J</e>
    <e>K</e>
  </channels>
  <classSortCode>Test</classSortCode>
  <classStatus>Test</classStatus>
  <creationDateTime>2013-03-21T22:29:01.58+05:30</creationDateTime>
  <isActive>true</isActive>
  <lastUpdatedDateTime>2013-03-21T22:29:01.58+05:30</lastUpdatedDateTime>
  <locale>Test</locale>
</ns0:Class>

xml – Add Namespace Prefix To root node

Leave a Reply

Your email address will not be published. Required fields are marked *