HELP - can't add namespace using XSL

N

n.phelge

I'm trying to validate XML which doesn't include a namespace, so I've
written a schema and I'm attempting to add a namespace to the original
XML using XSLT. The original XML looks like this:

<orderlist>
<order>
<header>
<ordernum>1313</ordernum>
</header>
</order>
<order>
<header>
<ordernum>1314</ordernum>
</header>
</order>
</orderlist>

and I would like to be transformed to the following:

<orderlist xmlns="urn:MyOrder-schema">
<order>
<header>
<ordernum>1313</ordernum>
</header>
</order>
<order>
<header>
<ordernum>1314</ordernum>
</header>
</order>
</orderlist>

However, every approach I've taken to writing an XSLT to do this
results in a blank namespace for the next-level elements, such as the
following:

<orderlist xmlns="urn:MyOrder-schema">
<order xmlns="">
<header>
<ordernum>1313</ordernum>
</header>
</order>
<order xmlns="">
<header>
<ordernum>1314</ordernum>
</header>
</order>
</orderlist>

This empty namespace results in a failed validation. Is there an
approach to adding a namespace to the top-most element of an XML
fragment using XSL such that the namespace of the next-level elements
aren't affected? I really need to be able to validate this XML, so any
help is greatly appreciated.

Thanks in advance
 
M

Martin Honnen

n.phelge said:
I'm trying to validate XML which doesn't include a namespace, so I've
written a schema and I'm attempting to add a namespace to the original
XML using XSLT. The original XML looks like this:

<orderlist>
<order>
<header>
<ordernum>1313</ordernum>
</header>
</order>
<order>
<header>
<ordernum>1314</ordernum>
</header>
</order>
</orderlist>

and I would like to be transformed to the following:

<orderlist xmlns="urn:MyOrder-schema">
<order>
<header>
<ordernum>1313</ordernum>
</header>
</order>
<order>
<header>
<ordernum>1314</ordernum>
</header>
</order>
</orderlist>

Should simply work with

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns="urn:MyOrder-schema">


<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>

where you then need to decide how you want to transform attributes, if
you want to simply copy them you could add

<xsl:template match="@*">
<xsl:copy-of select="." />
</xsl:template>
 
J

Joris Gillis

Tempore 15:32:19 said:
Is there an
approach to adding a namespace to the top-most element of an XML
fragment using XSL such that the namespace of the next-level elements
aren't affected?

Yes, this is exactly what happens in your example.
The 'xmlns=""' isn't added to the elements; it was already there, by default, hidden.


What you need to do is change the namespace of each and every node in the tree. An identity transform is what you need:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="xml" indent="yes"/>

<xsl:template match="/">
<xsl:apply-templates select="node()" mode="put_in_new_namespace">
<xsl:with-param name="new_namespace">urn:MyOrder-schema</xsl:with-param>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="*" mode="put_in_new_namespace" priority="1">
<xsl:param name="new_namespace"/>
<xsl:element name="{local-name()}" namespace="{$new_namespace}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="node()" mode="put_in_new_namespace">
<xsl:with-param name="new_namespace" select="$new_namespace"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>

<xsl:template match="node()" mode="put_in_new_namespace">
<xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>


regards,
 
J

Joris Gillis

Tempore 15:42:34 said:
Should simply work with

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns="urn:MyOrder-schema">


<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>

Hey, that's clever... never thought that would work;-)

regards,
 
N

n.phelge

Thanks for the quick response, guys! I knew there had to be a
straightforward way of doing it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,000
Messages
2,570,252
Members
46,848
Latest member
CristineKo

Latest Threads

Top