473,395 Members | 1,581 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,395 software developers and data experts.

renaming an element -- how to copy namespace nodes?

I have a tag foo that I want to copy unchanged when it is a subtag of
bar, so I have a template (x is the namespace for the document):

<xsl:template match="x:bar/x:foo">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

BUT, I discovered that someone has been mis-speling foo as foop in the
source files. So I add another template to fix this up:

<xsl:template match="x:bar/x:foop">
<foo>
<xsl:apply-templates/>
</foo>
</xsl:template>

The problem is that the output now has xmlns attributes added to the
foo tag that I output. If the input spells "foo" correctly, matching
the first template above, this doesn't happen.

I read in the docs that <xsl:copy> copies the namespace nodes, and I
surmise that the fact that this is not done when I specify the
explicit "<foo>" output is the reason for the xmlns attributes showing
up. However, I can't figure out how to copy the namespace nodes. I try
things such as

<xsl:for-each select="namespace::*">
<xsl:copy/>
</xsl:for-each>

or

<xsl:copy-of select="namespace::*"/>

but nothing seems to work (and it's not clear where exactly to put
these). Changing <foo> to <xsl:element name="foo"> doesn't help
either.

The actual xmlns attribute varies between Microsoft's .Net XSLT (which
outputs xmlns="") and Xalan (which outputs xmlns:x="[the URI for x]"
xmlns="[the URI for x]". But what I want is no xlmns attributes at
all, as it appears when the "foo" tag is copied by <xsl:copy>. Can
this be done?

Thanks.
- adam
Jul 20 '05 #1
5 5249
ad****@gte.net (Adam Barr) writes:
I have a tag foo that I want to copy unchanged when it is a subtag of
bar, so I have a template (x is the namespace for the document):

<xsl:template match="x:bar/x:foo">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

BUT, I discovered that someone has been mis-speling foo as foop in the
source files. So I add another template to fix this up:

<xsl:template match="x:bar/x:foop">
<foo>
<xsl:apply-templates/>
</foo>
</xsl:template>

The problem is that the output now has xmlns attributes added to the
foo tag that I output. If the input spells "foo" correctly, matching
the first template above, this doesn't happen. <snip>

I think you need to make the default output namespace the same as the
x namespace here with a 'xmlns="..."' declaration in the
<xsl:stylesheet/> element. This will result in the new <foo/>
elements being created in the same output namespace, and thus they
*shouldn't* have an extra namespace declaration.

So, with this XML

----
<test xmlns="http://example.com/foo">
<bar>
<foo>Wibble</foo>
<foop>Wobble</foop>
</bar>
</test>
----

and this XSLT

----
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://example.com/foo"
xmlns="http://example.com/foo"


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

<xsl:template match="x:bar/x:foo">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="x:bar/x:foop">
<foo>
<xsl:apply-templates/>
</foo>
</xsl:template>

</xsl:stylesheet>
----

I get

----
<output xmlns="http://example.com/foo" xmlns:x="http://example.com/foo">
<foo>Wibble</foo>
<foo>Wobble</foo>
</output>
----

Fiddle around with the exclude-result-prefixes attribute if you want
to omit the redundant xmlns:x declaration.

--
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 #2

<foo>

generates a foo element in the default namespace at that point (in the stylesheet).
If that isn't the namespace used in te hrest of your source document
then the serialiser will inser a namespace decaration to make sure that
foo stays in this namespace.

If you want to generate a foo element in the namespace you have bound to
x: then you could go

<x:foo>

or perhaps you would prefer

<foo xmlns=" whatever you have bound to x">

normally you'd put xmlns=" whatever you have bound to x" on the
xsl:stylesheet element rather than on each literal result element, so it
is in scope over the whole stylesheet.
but nothing seems to work

both of those work, as in copy namespace nodes but probably have no
effect on the serialised result. Namespace nodes don't affect the name
of the element (which is a pair consisting of namespace uri and local
name) so once you have generated an element in no-namespace adding
namespace nodes never affects that element it might cause namespace
declaarations to be generated, but the system must make sure that these
don't change the namespace of the elements being serialised.
Don't think of namespaces as a type of attribute, think of them as part
of the element name. the fact that they get serialised using attribute
syntax is just a historical quirk.

David
Jul 20 '05 #3
In article <ce**************************@posting.google.com >,
Adam Barr <ad****@gte.net> wrote:
<xsl:template match="x:bar/x:foo">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
This will output a foo in the namespace x is bound to.
<xsl:template match="x:bar/x:foop">
<foo>
<xsl:apply-templates/>
</foo>
</xsl:template>


This will output a foo in no namespace, unless you have a default
namespace declaration in the stylesheet. You probably want:

<xsl:template match="x:bar/x:foop">
<x:foo>
<xsl:apply-templates/>
</x:foo>
</xsl:template>

-- Richard
Jul 20 '05 #4
David Carlisle <da****@nag.co.uk> wrote in message news:<yg*************@penguin.nag.co.uk>...
<foo>

generates a foo element in the default namespace at that point (in the stylesheet).
If that isn't the namespace used in te hrest of your source document
then the serialiser will inser a namespace decaration to make sure that
foo stays in this namespace.

If you want to generate a foo element in the namespace you have bound to
x: then you could go

<x:foo>

or perhaps you would prefer

<foo xmlns=" whatever you have bound to x">

normally you'd put xmlns=" whatever you have bound to x" on the
xsl:stylesheet element rather than on each literal result element, so it
is in scope over the whole stylesheet.
David

Thank you! The xmlns= did the trick (actually I did an <xsl:element
name="foo" xmlns="...">).

The .Net XslTransform was happy with just that; I had to add an
exclude-result-prefixes="x" to my <xsl:stylesheet> to get Xalan to
stop emitting the xlmns= attribute (in one place anyway...there were
actually several places I did this renaming, and they seem to behave
slightly differently based on whether the action inside of the
renaming <xsl:element> was an <xsl:apply-templates> or <xsl:value-of>.
More XSLT mystery...)

- adam
Jul 20 '05 #5
ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote in message news:<ck***********@pc-news.cogsci.ed.ac.uk>...
In article <ce**************************@posting.google.com >,
Adam Barr <ad****@gte.net> wrote:
<xsl:template match="x:bar/x:foo">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>


This will output a foo in the namespace x is bound to.
<xsl:template match="x:bar/x:foop">
<foo>
<xsl:apply-templates/>
</foo>
</xsl:template>


This will output a foo in no namespace, unless you have a default
namespace declaration in the stylesheet. You probably want:

<xsl:template match="x:bar/x:foop">
<x:foo>
<xsl:apply-templates/>
</x:foo>
</xsl:template>

-- Richard


I tried <x:foo> but then it output the "x:" also which I didn't want.
The only reason I was using a namespace to begin with was because the
input XML had an xmlns= attribute in it (and various web searching
revealed the "x:" solution to that). Instead I did <xsl:element
name="foo" xmlns="[what x is bound to]"> and that worked (see other
post).

Thanks.

- adam
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Tommy Frost | last post by:
Hi, I'm trying to figure out the xpath-expression for getting all relevant namespaces of an element, but I don't wangle it. When I have this document: <A xmlns1> <B xmlns2></B> <C xmlns3>
7
by: Eivind | last post by:
Hi, I'm creating XML-files from printed documents. According to the DTD I have to use, there has to be pagebreaks in the XML-file. These pagebrakes must be located whenever a new page in the...
6
by: Martin Plantec | last post by:
Hi again, If I may, I have another, slightly more complex, XSLT question. (I'm learning, but it's not as easy as I would have thought!) I would like a rule that says: for every element that has...
1
by: pjeung | last post by:
Say that I have an element <elementA> that has several layers of subelements. In System.Xml.XmlDocument and related classes, how do I rename <elementA> to <elementB> leaving the subelements intact?
4
by: Raed Sawalha | last post by:
I'm trying to create Element as following name MyElement:InitialName XmlElement elem = doc.CreateElement("MyElement:InitialName"); when generate the XML the tag is truncated as ONLY...
8
by: Knighterrant | last post by:
I want to copy elements from one namespace to anothor, how to create the xslt? for example, the source data is: <s:mail xmlns:s="urn:source-namespace"> <s:subject>xxxx</s:subject>...
1
by: ronchese | last post by:
Hello All. I need to complement some information in a xml file, using the Xml.XmlDocument object. This xml contains several <xs:element ....> nodes (is a saved dataset), and I need to write new...
16
by: TT (Tom Tempelaere) | last post by:
Hi all, I created an XSD to define the structure of an XML file for my project. I made an XML file linked to the XSD using XmlSpy. The problem is that if I read the file using .NET XmlDocument...
2
by: sqad | last post by:
<JiraJelly xmlns:jira="jelly:com.atlassian.jira.jelly.enterprise.JiraTagLib"> ....something </JiraJelly> Anyone know how to create this xml tag using the xsl:element? Having a problem with the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.