Re: replace attribute of specific elements

Discussion in 'XML' started by Marrow, Jul 4, 2003.

  1. Marrow

    Marrow Guest

    Hi Markus,

    Try something like...

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="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>
    <xsl:eek:therwise>
    <xsl:copy/>
    </xsl:eek:therwise>
    </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" <> wrote in message
    news:...
    > 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.
     
    Marrow, Jul 4, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brunswick
    Replies:
    1
    Views:
    399
    Toni Uusitalo
    Jan 27, 2004
  2. Replies:
    2
    Views:
    1,079
    Henry S. Thompson
    Mar 6, 2006
  3. =?Utf-8?B?SmF2?=

    Is ViwState Page-Specific or UserControl-Specific

    =?Utf-8?B?SmF2?=, Aug 16, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    548
    =?Utf-8?B?SmF2?=
    Aug 16, 2006
  4. Chris Seberino
    Replies:
    3
    Views:
    750
    Stefan Behnel
    Jun 12, 2009
  5. William FERRERES
    Replies:
    7
    Views:
    220
    William FERRERES
    Jul 9, 2007
Loading...

Share This Page