I have an XSLT stylesheet that transforms an XML file to JSON format and a part of the schema for the XML allow any XML what-so-ever, which I need to just encode as a string in the corresponding JSON.
I have the elements getting created OK I am just having trouble getting the namespaces to behave correctly.
The problem is that every single namespace in scope of the current element is being output for every single element. I only want to output namespaces as they are first declared in the original XML.
This is what I am currently trying to do, I have tried a few things and cannot figure out a way to do what I need.
I call a template that is responsible for transforming attributes for the current node with:
<xsl:call-template name="elementAttributes">
<xsl:with-param name="element" select="."/>
</xsl:call-template>
With the following template:
<xsl:template name="elementAttributes">
<xsl:param name="element"/>
<xsl:for-each select="$element/namespace::*">
<!--dont include default namespace-->
<xsl:if test="boolean(name())">
<xsl:text> </xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>='</xsl:text>
<xsl:value-of select="."/>
<xsl:text>'</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
So for instance with the XML:
<valueString>
<dms:animal xmlns:dms="http://test.net/2012/dms/">
<dms:dog>woof</dms:dog>
<dms:cat>meow</dms:cat>
<reptile:crocodile xmlns:reptile="http://reptile.test.net/2012/dms/"></reptile:crocodile>
</dms:animal>
</valueString>
I am ending up with:
<dms:animal dcds='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' dms='http://test.net/2012/dms/' ns0='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' xml='http://www.w3.org/XML/1998/namespace'>
<dms:dog dcds='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' dms='http://test.net/2012/dms/' ns0='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' xml='http://www.w3.org/XML/1998/namespace'>woof</dms:dog>
<dms:cat dcds='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' dms='http://test.net/2012/dms/' ns0='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' xml='http://www.w3.org/XML/1998/namespace'>meow</dms:cat>
<reptile:crocodile dcds='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' dms='http://test.net/2012/dms/' ns0='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' reptile='http://reptile.test.net/2012/dms/' xml='http://www.w3.org/XML/1998/namespace'/>
when I only basically want to get back the same XML.
I know it is possible to do copy-of to easily copy XML to an output but I since I am outputting the results as text due to the JSON format I could not get it to work that way, as only the text nodes would be copied and not the XML structure.