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

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 3382
I may have found the answer. The following rule seems to work:

<xsl:template match="*[@webclass]">
<xsl:copy>
<xsl:attribute name="class">
<xsl:value-of select="@webclass"/>
</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*********************@g44g2000cwa.googlegroups. 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:template 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:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

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

<xsl:template match="@webclass">
<xsl:attribute 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.googlegr oups.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
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...
5
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>...
4
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. ...
2
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"...
3
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...
1
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...
1
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
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.