By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,934 Members | 1,692 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,934 IT Pros & Developers. It's quick & easy.

[XSL] Changing namespace definition

P: n/a
Hi,

i have one problem with an xsl trasformation (I'm using Xalan)

The source XML document is the following:

<?xml version = '1.0' encoding = 'ISO-8859-1'?>
<ROOT xmlns:NAME='old_namespace'>
<NAME:element>...</NAME:element>
<NAME:element>...</NAME:element>
</ROOT>

What i want is to change the uri of the namespace from
"old_namespace" to "new_namespace".

To achive this goal i've wrote this document:

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet
version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
xmlns:NAME = "old_namespace">

<xsl:template match = "/">
<xsl:element name = "ROOT">
<xsl:attribute name = "xmlns:NAME">new_namespace</xsl:attribute>
<xsl:copy-of select="/ROOT/NAME:element" />
</xsl:element>
</xsl:template>

</xsl:styleshee>

And the result of the transformation is:

<?xml version="1.0" encoding="UTF-8"?>
<ROOT xmlns:NAME="new_namespace">
<NAME:element xmlns:NAME="old_namespace">...</NAME:element>
<NAME:element xmlns:NAME="old_namespace">...</NAME:element>
</ROOT>

The namespace definition in the root element is changed
but the processor have put the old namespace definition
in every child of the root.

The result that i expected is

<?xml version="1.0" encoding="UTF-8"?>
<ROOT xmlns:NAME="new_namespace">
<NAME:element>...</NAME:element>
<NAME:element>...</NAME:element>
</ROOT>

I hope that the explanation of the problem was clear.

Thanks
Jul 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
In article <f5**************************@posting.google.com >,
insane79 <rr*****@libero.it> wrote:
What i want is to change the uri of the namespace from
"old_namespace" to "new_namespace".


You can't just change the declaration on the top-level element: the
elements will still be in the old namespace and XSLT will generate
declarations to make sure they are.

You need a template that copies each element by creating a new element
with the same local name but the new namespace name. You can modify
the usual identity transform to do this.

-- Richard
Jul 20 '05 #2

P: n/a
ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote in message news:<c8***********@pc-news.cogsci.ed.ac.uk>...
You need a template that copies each element by creating a new element
with the same local name but the new namespace name. You can modify
the usual identity transform to do this.
I'm not sure if i understand in the right way, honwever
i've wrote the following document

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet
version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
xmlns:NAME = "old_namespace">

<xsl:output
method="xml"
version="1.0"
encoding="ISO-8859-1" />

<xsl:template match = "/">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="@xmlns:NAME" >
<xsl:apply-templates />
</xsl:template>

<xsl:template match="ROOT" >
<xsl:element name="ROOT">
<xsl:attribute name="xmlns:NAME">
<xsl:text>new_namespace</xsl:text>
</xsl:attribute>
<xsl:apply-templates />
</xsl:element>
</xsl:template>

<xsl:template match="NAME:element" >
<xsl:element name="NAME:element">
<xsl:attribute name="xmlns:NAME">
<xsl:text>new_namespace</xsl:text>
</xsl:attribute>
<xsl:copy>
<xsl:apply-templates select="@*"/>
</xsl:copy>
</xsl:element>
</xsl:template>

</xsl:stylesheet>

But the resutlt is the same of before: the old namespace
declaration is put into every child of ROOT.

Where i'm doing the wrong thing?
-- Richard


Bye
Roberto
Jul 20 '05 #3

P: n/a
rr*****@libero.it (insane79) writes:
ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote in message news:<c8***********@pc-news.cogsci.ed.ac.uk>...
You need a template that copies each element by creating a new element
with the same local name but the new namespace name. You can modify
the usual identity transform to do this.

<snip/>
But the resutlt is the same of before: the old namespace
declaration is put into every child of ROOT.

Where i'm doing the wrong thing?


See my reply earlier today to Chris Bedford in the thread "how to get
the identity transformation to stop outputting extra 'xmlns:' attrib
?" in this group. It tells you what you need to know.

Ben

--
Ben Edgington
Mail to the address above is discarded.
Mail to ben at that address might be read.
http://www.edginet.org/
Jul 20 '05 #4

P: n/a
In article <f5**************************@posting.google.com >,
insane79 <rr*****@libero.it> wrote:
<xsl:attribute name="xmlns:NAME">


You're still trying to change the namespace declarations (which you
can't: they're not part of the XPath data model), instead of the
elements themselves. You need something like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:old="http://example.org/old">

<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="old:*">
<xsl:element name="{local-name()}" namespace="http://example.org/new">
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:template>

</xsl:stylesheet>

-- Richard
Jul 20 '05 #5

P: n/a
> See my reply earlier today to Chris Bedford in the thread "how to get
the identity transformation to stop outputting extra 'xmlns:' attrib
?" in this group. It tells you what you need to know.
Thanks for the hint, i've tried both your solution and the one from Chris
but without any good result.

I can change the namespace in the root node but the old namespace is still
applied to every child.
Ben


Bye
Roberto
Jul 20 '05 #6

P: n/a
rr*****@libero.it (insane79) wrote in message news:<f5**************************@posting.google. com>...
Hi,
i have one problem with an xsl trasformation (I'm using Xalan)


[CUT]

I've solved the problem (with your help) and i post thee solution
in the hope that can be usefull for somebody.

The xslt document is the following:

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:NAME="old_namespace">

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

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

<xsl:template match="ROOT">
<xsl:element name="ROOT">
<xsl:attribute name="xmlns:NAME">http://example.org/new</xsl:attribute>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>

</xsl:stylesheet>

If you put in input an XML like this:

<?xml version = '1.0' encoding = 'ISO-8859-1'?>
<ROOT xmlns:NAME='old_namespace'>
<NAME:element attr1="xxx" >
<NAME:subElement attr2="yyy">abcdefghi</NAME:subElement>
</NAME:element>
<NAME:element attr1="xxx" >
<NAME:subElement attr2="yyy">abcdefghi</NAME:subElement>
</NAME:element>
</ROOT>

The output will be the following
<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT xmlns:NAME="http://example.org/new">
<NAME:element attr1="xxx">
<NAME:subElement attr2="yyy">abcdefghi</NAME:subElement>
</NAME:element>
<NAME:element attr1="xxx">
<NAME:subElement attr2="yyy">abcdefghi</NAME:subElement>
</NAME:element>
</ROOT>

So only the namespace declaration in the root element is changed

Many for your help

Bye
Roberto
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.