473,752 Members | 8,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="namespa ce::*">
<xsl:copy/>
</xsl:for-each>

or

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

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 5276
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:styleshe et/> 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:styleshe et 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:templat e 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:templat e 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:templat e 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:templat e 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
1574
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
1556
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 printed version occurs. This is fairly simple to accomplish. The problem is however, the DTD states that the pagebreak cannot occur inside paragraph-element, but must be in between them. Is it possible, using XSLT, to end the paragraph-element...
6
3415
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 an attribute "webclass", rename the attribute as "class", keeping the same value for the attribute (and keeping the same element name). In other words, I would like to rename an attribute, whatever the element it comes in. I gather it is...
1
14611
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
2126
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 "MyElement",why? Any Suggestions
8
1460
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> <s:from>xxxx</s:from> <s:to>xxxx</s:to> <s:content> <!]>
1
1309
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 nodes exactly in the same way the existant nodes. I tried naming the new node as "xs:element" or type the "element" in the namespaceURI, but it when I see creates a namespace in the text file, in both cases. How can I do that?
16
3528
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 and then query for the root element, the result is always null (1). However if I strip the root element of all attributes generated by XmlSpy, then there is no problem to find the root element with .NET XML classes (2). (1) The XML for which...
2
3727
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 "xmlns:jira" namespace part...parser hates that. Thanks, sqad
0
8867
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9624
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9429
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9295
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8295
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6836
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4921
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3354
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2243
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.