Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 3rd, 2005, 11:01 AM
Authorized User
 
Join Date: Sep 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default Change 1 attribute

Hi,
how can i change only one attribute in XLST ?
Like if i have
<node a="test" b="something" c="hello"></node>

I want to change everything that has a hello, to bye, but mantain the other attributes untouched, so the output would be

<node a="test" b="something" c="bye"></node>

The problem is i don't know how many attributes will the node have, if it'll have only a,b and c, or a,b,c,d,e,f,g....
So i can't just "hardcode" the @a and @b attributes, and only change the c

thank you


 
Old October 3rd, 2005, 12:19 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Write a template rule for any attribute

<xsl:template match="*">
  <xsl:copy/>
</xsl:template>

and another to change the c attribute

<xsl:template match="c">
  <xsl:attribute name="c">bye</xsl:attribute>
</xsl:template>

and then apply-templates to all attributes:

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

Alternatively, you can exploit the fact that if you write the same attribute twice, the second one wins:

<xsl:copy-of select="@*"/>
<xsl:attribute name="c">bye</xsl:attribute>

But that doesn't work if you want to delete one attribute.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
change an attribute value akentanaka XSLT 2 October 17th, 2008 04:52 AM
Change Sort-by attribute based on parent tclancy XSLT 2 March 24th, 2006 05:00 PM
XSLT change class attribute by ID? matallen XSLT 8 March 1st, 2006 05:00 PM
Problem using XSLT to change XML attribute cbeech1980 XSLT 1 October 18th, 2005 06:26 AM
Change style attribute of a WebUserControl YiannisF ASP.NET 1.0 and 1.1 Basics 1 July 10th, 2004 02:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.