2

I am trying to generate an XML output and i have created an XSLT that will do so. However the root node is is missing some Name spacing. How do you add Namespace to the root element of an XML structure. This it the XSLT i am using:

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:doc="urn:sapcom:document:sap:rfc:functions" xmlns:r="http://www.castiron.com/response" exclude-result-prefixes="r">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <xsl:element name="imageScene7Request">
            <xsl:element name="productIds">
                <xsl:for-each select="r:productGetAllByIdsResponse/r:payload/r:products">
                    <xsl:value-of select="r:id"/>
                    <xsl:if test="position() != last()">
                        <xsl:text>,</xsl:text>
                    </xsl:if>
                </xsl:for-each>
            </xsl:element>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

The Namespacing i would like to add to the root http://www.castiron.com/response

INPUT XML

<?xml version="1.0" encoding="UTF-8"?>
<productGetAllByIdsResponse xmlns="http://www.castiron.com/response">
    <rcode>0</rcode>
    <rmessage>Success</rmessage>
    <payload>
        <products>
            <id>4022280</id>
        </products>
        <products>
            <id>4022280</id>
        </products>
    </payload>
</productGetAllByIdsResponse>

You will see that when you run this is gives you:

<?xml version="1.0" encoding="utf-8"?>
<imageScene7Request>
    <productIds>4022280,4022280</productIds>
</imageScene7Request>

However i would like this:

<?xml version="1.0" encoding="utf-8"?>
<imageScene7Request xmlns="http://www.castiron.com/response">
    <productIds>4022280,4022280</productIds>
</imageScene7Request>

Reply @dbaseman

That worked however it then gave the second tag a null namespace, as shown below:

<?xml version="1.0" encoding="utf-8"?>
<imageScene7Request xmlns="http://www.castiron.com/response">
    <productIds xmlns="">4022280,4022280</productIds>
</imageScene7Request>

Is there a way to remove that?

3 Answers 3

4

Since you know statically what the names of your result elements are, it's much better to use literal result elements rather than xsl:element:

 <xsl:template match="/">
    <imageScene7Request xmlns="http://www.castiron.com/response">
        <productIds>
            <xsl:for-each select="r:productGetAllByIdsResponse/r:payload/r:products">
                <xsl:value-of select="r:id"/>
                <xsl:if test="position() != last()">
                    <xsl:text>,</xsl:text>
                </xsl:if>
            </xsl:for-each>
        </productIds>
    </imageScene7Request>
</xsl:template>

If you do use xsl:element you need to be sure to use the namespace attribute to make sure the element is in the right namespace.

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

Comments

3

I think you just need to specify the namespace explicitly in the stylesheet:

<xsl:element name="imageScene7Request" namespace="http://www.castiron.com/response">
    <xsl:element name="productIds">
       ...
    </xsl:element>
</xsl:element>

1 Comment

I have added a comment to the bottom of my post. Thank you for your help.
1

Does this work?

<xsl:stylesheet xmlns="http://www.castiron.com/response" ...>

That will set the namespace of all elements within the XSLT to http://www.castiron.com/response

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.