Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Re: replace attribute of specific elements

Thread Tools

Re: replace attribute of specific elements

Posts: n/a
Hi Markus,

Try something like...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
<!-- just used to store the node-set -->
<item name="change" attr="text" value="new_value"/>
<item name="change2" attr="label" value="new_value2"/>
<!-- store changes in a global variable -->
<xsl:variable name="changes" select="document('')/*/changes:nodeset/*"/>

<xsl:template match="/">

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

<xsl:template match="@*">
<!-- get the change node - if present -->
<xsl:variable name="this-change" select="$changes[@name =
current()/parent::*/@name and @attr = name(current())]"/>
<!-- see if this attribute needs changing -->
<xsl:when test="$this-change">
<xsl:attribute name="{name()}" namespace="{namespace-uri()}">
<xsl:value-of select="$this-change/@value"/>

<xsl:template match="text() | comment() | processing-instruction()">

Hope this helps
Marrow - home of Xselerator (XSLT IDE and debugger)

"Markus Lausser" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> Hello.
> I have a nodeset
> <nodeset>
> <item name="change" attr="text" value="new_value"/>
> <item name="change2" attr="label" value="new_value2"/>
> ....
> </nodeset>
> Now when parsing an xml file, i'd like to modify an attribute
> of the current node, if its attribute 'name' appears as attribute
> in the given nodeset.
> Example:
> <node name="change2" label="old_label"/>
> should be replaced to
> <node name="change2" label="new_value2"/>
> as the second item in the nodeset has the same 'name' attribute
> and it defines to set the attribute 'label' to "new_value2".
> The <node> hasn't necessarily defined both the 'name' or the 'label'
> attribute.
> Is there a simple way to do this?
> Markus.
> P.S. please cc to my email address if possible.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How *build* new elements and *replace* elements with xml.dom.minidom? Chris Seberino Python 3 06-12-2009 05:08 AM
Parsing DOM to search specific tags with specific custom attribute William FERRERES Javascript 7 07-09-2007 08:11 PM
picking value of one attribute based on a child elements attribute? XPath? XML 2 03-06-2006 05:19 AM
redirect traffic on specific ip to specific interface mimiseh Cisco 3 06-05-2005 09:14 PM
type-specific elements vs type attribute Brunswick XML 1 01-27-2004 11:15 AM