Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add namespaces to the root element of my XML using XSLT?

I have an input XML

<Request>
  <Info>
    <Country>US</Country>
    <Part>A</Part>
   </Info>
</Request>

My output should be like

<Request
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="http://hgkl.kj.com">
  <Info>
    <Country>US</Country>
    <Part>A</Part>
  </Info>
</Request>

Please let me know how to add the multiple namespaces and a default namespace like the above XML.

like image 295
Ironman Avatar asked Apr 12 '13 21:04

Ironman


People also ask

How do you add a namespace in XSLT?

Provided that you're using XSLT 2.0, all you have to do is add the xpath-default-namespace attribute on the document element of each stylesheet: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://example.com"> ...

What is the root element of XSLT?

An XSLT stylesheet is an XML document that must be valid against the XSLT schema. The root element in this schema is <xsl:stylesheet> and the children of the root are primarily <xsl:template> elements.

What is the use of namespace in XSLT?

In XML a namespace is a collection of names used for elements and attributes. A URI (usually, a URL) is used to identify a particular collection of names.


1 Answers

Here's how I'd do it in XSLT 2.0...

XML Input

<Request>
    <Info>
        <Country>US</Country>
        <Part>A</Part>
    </Info>
</Request>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="*" priority="1">
        <xsl:element name="{local-name()}" namespace="http://hgkl.kj.com">
            <xsl:namespace name="xsi" select="'http://www.w3.org/2001/XMLSchema-instance'"/>
            <xsl:namespace name="xsd" select="'http://www.w3.org/2001/XMLSchema'"/>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

XML Output

<Request xmlns="http://hgkl.kj.com"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <Info>
      <Country>US</Country>
      <Part>A</Part>
   </Info>
</Request>

Here's an XSLT 1.0 option which produces the same output, but requires you to know the name of the root element...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|text()|comment()|processing-instruction()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/Request">
        <Request xmlns="http://hgkl.kj.com" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <xsl:apply-templates select="@*|node()"/>           
        </Request>
    </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{local-name()}" namespace="http://hgkl.kj.com">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>
like image 175
Daniel Haley Avatar answered Oct 30 '22 09:10

Daniel Haley