0

i am beginner into XSLT. I am using it to transform XML to XML.

Source XML:

<Response>
    <Text>Hello</Text>
</Response>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://myexample.org/a"
xmlns:b="http://myexample.org/b"
version="1.0">

    <xsl:output method="xml" indent="yes" />

    <xsl:template match="Response" namespace="http://myexample.org/a">
        <xsl:element name="Root">
            <xsl:element name="a:Parent">
                <xsl:element name="b:Child">
                    <xsl:value-of select="Text"/>
                </xsl:element>
            </xsl:element>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Output:

<Root>
  <a:Parent xmlns:a="http://myexample.org/a">
    <b:Child xmlns:b="http://myexample.org/b">Hello</b:Child>
  </a:Parent>
</Root>

I would like to transform the XML into below XML using XSLT.

Expected Outpout:

<Root xmlns:a="http://myexample.org/a">
    <a:Parent xmlns:b="http://myexample.org/b">
        <b:Child/>
    </a:Parent>
<Root>

I have successfully created the XSLT to transform the data but here i am confused with namespaces. I am not able to generate it as above.

Please help. Thanks.

2
  • First Read this Why XSLT Commented Nov 27, 2012 at 15:09
  • @vels4j: i know it is use to transform XML to XML. I am also doing the same but here the output XML should have mentioned structure. I have updated the question. Please let me know if that make sense. Commented Nov 27, 2012 at 15:15

1 Answer 1

1

It's a bit awkward to create namespace declarations at a specific place using XSLT 1.0 (it's much easier in 2.0 which has <xsl:namespace>) but it can be done with a trick to copy the namespace nodes from the stylesheet document itself:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://myexample.org/a"
xmlns:b="http://myexample.org/b"
version="1.0">

    <xsl:output method="xml" indent="yes" />

    <xsl:template match="Response">
        <xsl:element name="Root">
            <xsl:copy-of select="document('')/*/namespace::a" />
            <xsl:element name="a:Parent">
                <xsl:copy-of select="document('')/*/namespace::b" />
                <xsl:element name="b:Child">
                    <xsl:value-of select="Text"/>
                </xsl:element>
            </xsl:element>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

The document('') parses the stylesheet document and gives you its root node, so document('')/* is the <xsl:stylesheet> element. We then extract from that element the namespace node bound to the specified prefix and copy that to the output document.

Alternatively, take out the namespace declarations from the <xsl:stylesheet> and use literal result elements:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

    <xsl:output method="xml" indent="yes" />

    <xsl:template match="Response">
        <Root xmlns:a="http://myexample.org/a">
            <a:Parent xmlns:b="http://myexample.org/b">
                <b:Child>
                    <xsl:value-of select="Text"/>
                </b:Child>
            </a:Parent>
        </Root>
    </xsl:template>
</xsl:stylesheet>

though this won't work if you need the a and b prefixes elsewhere in the stylesheet.

Sign up to request clarification or add additional context in comments.

Comments

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.