Question

I'm transforming an XML to another XML inside wso2esb (uses saxon). There I have following input example:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <root>
            <MyElement>content</MyElement>
        </root>
    </soapenv:Body>
</soapenv:Envelope>

My Problem is now that the my new content <NEW> should not get any namespace. But I get the following output after my XSLT:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <root>
         <NEW xmlns="http://ws.apache.org/ns/synapse">content</NEW>
      </root>
   </soapenv:Body>
</soapenv:Envelope>

I do not want the xmlns="http://ws.apache.org/ns/synapse" declaration in the new Element . When I test with oXygen it does work, but when I run it inside wso2esb I get the I guess default namespace "http://ws.apache.org/ns/synapse".

So I have tried out 3 different ways of creating the NEW element, and

<xsl:stylesheet xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:avintis="http://www.avintis.com/esb" extension-element-prefixes="avintis" version="2.0" exclude-result-prefixes="#all" xpath-default-namespace="">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/soapenv:Envelope|soapenv:Body">
        <xsl:copy>
            <xsl:apply-templates select="*|text()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="root">
        <xsl:copy>
            <!-- FIRST METHOD - THE METHOD I want to use! this will receive the  xmlns="http://ws.apache.org/ns/synapse" -->
            <NEW>
                <xsl:value-of select="MyElement"/>
            </NEW>
            <!-- Second methodworks - but I need to add xmlns="" to every element and there are a lot-->
            <NEW xmlns="">
                <xsl:value-of select="MyElement"/>
            </NEW>
            <!-- Third method works: But not very readable - I would prefer the first method -->
            <xsl:element name="NEW">
                <xsl:value-of select="MyElement"/>
            </xsl:element>
         </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Is there a way to remove this xmlns attribute when using the first method of element creation? Thanks

Was it helpful?

Solution 2

Just found the solution by adding a xmlns="" to the <xsl:stylesheet. Seems like WSO2 gives a default namespace when none is specified.

<xsl:stylesheet xmlns="" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:avintis="http://www.avintis.com/esb" extension-element-prefixes="avintis" version="2.0" exclude-result-prefixes="#all" xpath-default-namespace="">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

<xsl:template match="/soapenv:Envelope|soapenv:Body">
    <xsl:copy>
        <xsl:apply-templates select="*|text()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="root">
    <xsl:copy>
        <!-- FIRST METHOD - THE METHOD I want to use! this will receive the  xmlns="http://ws.apache.org/ns/synapse" -->
        <NEW>
            <xsl:value-of select="MyElement"/>
        </NEW>
        <!-- Second methodworks - but I need to add xmlns="" to every element and there are a lot-->
        <NEW xmlns="">
            <xsl:value-of select="MyElement"/>
        </NEW>
        <!-- Third method works: But not very readable - I would prefer the first method -->
        <xsl:element name="NEW">
            <xsl:value-of select="MyElement"/>
        </xsl:element>
     </xsl:copy>
</xsl:template>

OTHER TIPS

The problem is not that there is an unwanted namespace declaration in the serialized output; the problem is that the NEW element is in the wrong namespace. You need to think about what the (expanded) names of the elements and attributes are, and the namespace declarations will look after themselves.

I strongly suspect that the code you are executing is different from what you have shown us. I think it must have the default namespace declaration xmlns="http://ws.apache.org/ns/synapse" on some ancestor of the <NEW> literal result element, probably on the xsl:stylesheet element itself. If that's not the case, then wso2esb is doing something very strange when it runs the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top