Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: replace attribute of specific elements

 
 
Marrow
Guest
Posts: n/a
 
      07-04-2003
Hi Markus,

Try something like...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform"
xmlns:changes="urn:my-changes">
<!-- just used to store the node-set -->
<changes:nodeset>
<item name="change" attr="text" value="new_value"/>
<item name="change2" attr="label" value="new_value2"/>
</changes:nodeset>
<!-- store changes in a global variable -->
<xsl:variable name="changes" select="document('')/*/changes:nodeset/*"/>

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

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

<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:choose>
<xsl:when test="$this-change">
<xsl:attribute name="{name()}" namespace="{namespace-uri()}">
<xsl:value-of select="$this-change/@value"/>
</xsl:attribute>
</xsl:when>
<xsltherwise>
<xsl:copy/>
</xsltherwise>
</xsl:choose>
</xsl:template>

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

Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator

"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
 
 
 
Reply

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? vjethava@gmail.com 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



Advertisments