2
<application xmlns="http://someurl">
    <Detail1/>
    <Detail2>
    <Property/>
    </Detail2>
</application>

I am trying to add xmlns:xs="http://www.w3.org/2001/XMLSchema" and xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" to the "application root node". I tried to follow a lot of links on stackoverflow but nothing seemed to work in XSLT 1.0 Can someone help me with this.

1 Answer 1

3

Since XSLT 1.0 doesn't have the <xsl:namespace> instruction in XSLT 2.0, the technique I use is to copy such nodes from my stylesheet:

Input:

t:\ftemp>type ns.xml
  <application xmlns="http://someurl">
    <Detail1/>
    <Detail2>
    <Property/>
    </Detail2>
</application>

Execution:

t:\ftemp>call xslt ns.xml ns.xsl
<?xml version="1.0" encoding="utf-8"?><application xmlns="http://someurl" xmlns:
xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchem
a-instance">
    <Detail1/>
    <Detail2>
    <Property/>
    </Detail2>
</application>

Stylesheet:

t:\ftemp>type ns.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                version="1.0">

<xsl:template match="/*">
  <xsl:copy>
    <xsl:copy-of select="document('')/*/namespace::xs"/>
    <xsl:copy-of select="document('')/*/namespace::xsi"/>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick reply. As i am new to xslt can you help me what does the below lines means: <xsl:copy-of select="document('')/*/namespace::xs"/> <xsl:copy-of select="document('')/*/namespace::xsi"/> I tried to run the code in the below tool, but showing me the original xml only. online-toolz.com/tools/xslt-transformation.php
I indicated in my answer that I am copying the namespace nodes from the stylesheet using those instructions. My transcript shows the stylesheet is working with a conforming XSLT processor. If the stylesheet does not work for you, then you have a problem somewhere in your environment. I cannot tell you what is wrong with your environment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.