XSL: replace attribute of specific elements

XSL: replace attribute of specific elements

Post by Markus Lauss » Fri, 04 Jul 2003 23:56:00



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.

 
 
 

XSL: replace attribute of specific elements

Post by Marro » Sat, 05 Jul 2003 18:38:23


Hi Markus,

Try something like...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.veryComputer.com/;
  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:copy>
</xsl:template>


  <!-- get the change node - if present -->


  <!-- see if this attribute needs changing -->
  <xsl:choose>
    <xsl:when test="$this-change">
      <xsl:attribute name="{name()}" namespace="{namespace-uri()}">

      </xsl:attribute>
    </xsl:when>
    <xsl:otherwise>
      <xsl:copy/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

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

Hope this helps
Marrow
http://www.veryComputer.com/ - home of Xselerator (XSLT IDE and de*)
http://www.veryComputer.com/


Quote:> 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.