473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

[XSL] Changing namespace definition

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
6 4218
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
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
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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: chris | last post by:
Hi, After going back through the XSL tutorials and the help here I have largely solved the problem of merging two XHTML files, but one small detail remains... The parser outputs <html...
4
by: Jean-Christophe Michel | last post by:
Hi, In a complex merging of two (non ordered) xml files i need to keep track of the elements of the second tree that were already merged with first tree, to copy only unused elements at the end....
3
by: william_hulse | last post by:
The general process i am currently working on is this: STEP 1 xml doc1 is transformed using stylesheet1 to produce xml doc2 - xml doc1 has a namespace declaration as follows... <?xml...
3
by: schaf | last post by:
Hello NG! I would like to transform a XML-File into an new XML-File with additional information. That's no problem, but my source XML-File has a root element with a xmlns definition. Like this ...
4
by: n.phelge | last post by:
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: ...
8
by: Markus Wildgruber | last post by:
Hi! We want to use XSL-FO in the reporting of our application. Before making the decision we have some questions on that topic: Does anyone have experience with XSL-FO in the .NET environment?...
4
by: jjouett | last post by:
I have the following input XML: <?xml version="1.0"?> <ordersubmit xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"...
0
by: Nick | last post by:
I am creating a website for a client and I am using XSL for my email templates and I have that parses the templates and creates an email. I have a custom class called EmailAddress which has one...
2
by: shapper | last post by:
Hello, I created a XSL file to convert a XML file to another XML. I am running this in Asp.Net but this is not working. ----- ORIGINAL XML ----- <?xml version="1.0" encoding="utf-8" ?>...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.