Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 16th, 2015, 03:00 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2015
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default replace attribute value on a node with another specific attribute value

XML:

Code:
<field>
            <property name="data" value="false"/>
            <property name="space" value="false"/>
            <property name="declaration" value="false"/>
</field>
Wanted result:

Code:
<field>
            <property name="data" value="true"/>
            <property name="space" value="false"/>
            <property name="declaration" value="false"/>
</field>
With XSL I want to scan through documents, changing all the attribute values that are "false" and have a name attribute equal to "data", into "true" (so to in this instance only to change the value of the first property element).

I've been looking around but couldn't find the answer. Any suggestions to how would I complete this with XSLT?

*edited to clarify

Last edited by borgulas; September 16th, 2015 at 04:26 AM..
Reply With Quote
  #2 (permalink)  
Old September 16th, 2015, 03:57 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Start by writing a template that copies everything unchanged:

Code:
<xsl:template match="node()|@*">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>
Then add template rules for the things you want to change, for example

Code:
<xsl:template match="property[@name='data']/@value[.='false']">
  <xsl:attribute name="value">true</xsl:attribute>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
The Following User Says Thank You to mhkay For This Useful Post:
borgulas (September 16th, 2015)
  #3 (permalink)  
Old September 16th, 2015, 04:00 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

P.S. There is probably a better strategy for learning a language than "looking around". Wrox publishes some good books...
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #4 (permalink)  
Old September 16th, 2015, 04:11 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2015
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mhkay View Post
P.S. There is probably a better strategy for learning a language than "looking around". Wrox publishes some good books...
Thx for the suggestion! I will try out your solution. And yes, this is probably a good idea. I don't need to write xslt so often, so I usually look around. I've gone through the w3c tuts, but it's too plain to get a good grip of more complex situations. Might I need it more often in the future I will consider reading one of his publications.

PS: It works! Already had the copy part you suggested and was as far as to have 'property[@name='data']', but didn't know you could add the /@value[.='false']. Thx again :)

Last edited by borgulas; September 16th, 2015 at 04:25 AM..
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Added attribute to specific element tron_thomas XSLT 3 September 7th, 2010 11:59 AM
How to find attribute for specific element? QuadFather XML 1 April 7th, 2010 12:41 PM
Finding if an attribute has a specific value mphare XSLT 1 February 2nd, 2009 06:57 PM
Replace an attribute with another attribute georgemeng XSLT 8 June 10th, 2008 11:04 AM
sort on specific attribute kgoldvas XSLT 2 July 17th, 2007 09:42 AM



All times are GMT -4. The time now is 11:34 PM.


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