473,624 Members | 2,288 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSLT: renaming an attribute in whatever element

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 possible, but I had no luck so far with my tries.

Thanks,

Martin

Oct 28 '05 #1
6 3402
I may have found the answer. The following rule seems to work:

<xsl:template match="*[@webclass]">
<xsl:copy>
<xsl:attribut e name="class">
<xsl:value-of select="@webcla ss"/>
</xsl:attribute>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>

Oct 28 '05 #2
Well, the problem with this, is that suppose my XML has

<para webclass="red"> hjk hjk hjk</para>

and that my XSL stylesheet has already, in addition to the proposed
rule above, a rule catching para elements, in my case:

<xsl:template match="para">
<p>
<xsl:for-each select="ligne">
<xsl:apply-templates />
<xsl:if test="position( ) &lt; last()">
<br />
</xsl:if>
</xsl:for-each>
</p>
</xsl:template>

then this will be applied, and the "webclass" attribute of the para
elements will not be renamed. How can I solve this?

Thanks,

Martin

Oct 28 '05 #3
In article <11************ *********@g44g2 000cwa.googlegr oups.com>,
Martin Plantec <ma************ @gmail.com> wrote:
Well, the problem with this, is that suppose my XML has

<para webclass="red"> hjk hjk hjk</para>

and that my XSL stylesheet has already, in addition to the proposed
rule above, a rule catching para elements, in my case:

<xsl:templat e match="para">
<p>
<xsl:for-each select="ligne">
[...]


Presumably you want to put the renamed attribute on the new p element?
You need to call apply-templates on the attributes of the old para
element.

<xsl:template match="para">
<p>
<xsl:apply-templates select="@*"/>
<xsl:for-each select="ligne">
....

-- Richard
Oct 28 '05 #4
Hi Richard,

Thanks for your help. This doesn't work: the attribute is renamed, but
the rule turning para into p is not applied. I have little experience
in XSLT (about 1 day), but I can speculate it is because the first rule
(attribute renaming) catches "all elements with attribute webclass" --
but the para element has already been caught, and the apply-templates
on the attributes doesn't fire it. Maybe the first rule could be
replaced by a rule targetting the webclass attribute directly (vs.
elements with the webclass attribute)?

(I have tried such a thing but it didn't work.)

If this has become confused I could produce a minimal and complete
example.

Thanks!

Martin

Oct 29 '05 #5
Below is the really simple transformation producing the wanted result of
global renaming a given attribute. It uses the most fundamental XSLT design
pattern -- overriding the identity rule.

A word of caution is that you haven't specified what to do in case of a
potential conflict -- such as when an element has both a "webclass" and a
"class" attribute.

When this transformation:

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="ye s" indent="yes"/>

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

<xsl:template match="@webclas s">
<xsl:attribut e name="class">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>

is applied on this source xml:

<t>
<x webclass="a" myclass="b">
<y webclass="c" yClass="d"/>
<z/>
</x>
</t>

the wanted result is produced:

<t>
<x class="a" myclass="b">
<y class="c" yClass="d"></y>
<z></z>
</x>
</t>
Cheers,
Dimitre Novatchev.
"Martin Plantec" <ma************ @gmail.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
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 possible, but I had no luck so far with my tries.

Thanks,

Martin

Oct 29 '05 #6
Thanks Dimitre! It works fine, and I am now using this.

Oct 31 '05 #7

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

Similar topics

2
3397
by: nanookfan | last post by:
Hi all, I'm having a bizarre problem converting XML files to HTML using an XSLT. The problem is only occuring in my Netscape 7.0 browser. What makes it more bizarre is that it is only happening when I put my XML files and the .xsl files on my ISP's system for my home page. If I try to open the XML files in Netscape 7.0 on my own machine (ie, not on the ISP's system), the pages convert file and the result is displayed in HTML.
5
5267
by: Adam Barr | last post by:
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
4
4291
by: Adrian Charteris | last post by:
Hi I'm currently trying to use a lookup table for converting one xml doc to another using a XSLT transformation. Ideally I would like my first xml doc to be converted to my second xml doc below. All that I want is to replace node names with a matching value in the lookup table and place the result into an field attribute pair: Example: id to be renamed instrument_id thus <id type="master">asset #132</id> becomes <field...
2
3394
by: Jordan Willms | last post by:
Hi there. I am working with lom metadata and I am a little confused with how to form the following xml element: <lom xmlns="http://www.imsglobal.org/xsd/imsmd_v1p2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd"/>
3
1560
by: rush | last post by:
I have a DTD that defines new elements "mytextfield" and "mysn", and does it as an extension to XHTML. The idea is that my XML markup is actually valid XHTML according to my DTD. This all works just fine. After getting the DTD to work, I defined a XSL stylesheet that would transform my markup into XHTML that would display correctly in a browser. At first this meant that everywhere I encontered a "mytextfield" element in the markup, I...
1
1790
by: Wayne Lian via .NET 247 | last post by:
Hi all, Just wonder anyone have encountered this problem before?I tried using XMLSPY debugger and I can get the correct outputfor my XSLT transformation, however in .net, aftertransformation the attributes I put inside xsl:element tagcannot be generated. Part of the XML data: <?xml version="1.0" encoding="utf-8"?> <receipients> <receipient> ... <ServerUrl>http://win2003p2/</ServerUrl>...
1
2036
by: arnold | last post by:
Hi, I've been knocking my head against the wall trying to create an XSL transform to perform "normalizations" of a set of XML files that have a common structure. % XML file before transform
2
22765
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values in the attribute of an element. Instead of using the <xsl:attribute> element, you can simply place the xpath in the attribute itself. The most common usage of this is in creating hyperlinks.
0
8240
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
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
8680
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...
1
8336
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2610
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
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.