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 February 26th, 2009, 11:36 AM
Authorized User
 
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
Default update element value

Hi,
I want to change the value of a specific element in the xml. Can someone tell me how to do this.

For Example

input XML

<root>
<input1>
<text1>test1<test1>
<test2>test2<test2>
<input1>
<input2>
<test1>test12</test1>
<test2>test22</test2>
</input2>
</root>

Output XML

<root>
<input1>
<text1>New Value<test1>
<test2>test2<test2>
<input1>
<input2>
<test1>New Value</test1>
<test2>test22</test2>
</input2>
</root>

where "New Value" is a variable.
 
Old February 26th, 2009, 11:48 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Create an identity template that copies everything:

Code:
<xsl:template match="*">
  <xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>
and then add a template that changes the nodes you want to change:

Code:
<xsl:template match="test1/text()">
  <xsl:value-of select="$NewValue"/>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 26th, 2009, 11:49 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You should be able to use the identity template, which just copies everything, and add a rule (template) for the specific change:
Code:
<xsl:stylesheetversion="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
<xsl:param name="newValue" />
<xsl:templatematch="@*|node()">
<xsl:copy>
<xsl:apply-templatesselect="@*|node()"/>
</xsl:copy>
</xsl:template>
 
<xsl:templatematch="text1">
<xsl:copy>
<xsl:value-ofselect="$newValue"/>
</xsl:copy>
 
</xsl:template>
</xsl:stylesheet>
If that's not what you need then show some well formed XML and state the rules you need to follow and which XSLT tools you are using as setting xsl:param is processor dependent.
__________________
Joe
http://joe.fawcett.name/
 
Old February 26th, 2009, 12:00 PM
Authorized User
 
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
Smile

thanks for your help





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
adding of element and assigning to one element sushovandatta XSLT 2 November 16th, 2004 07:04 PM
Update a table with XML element values alavastros XML 1 July 16th, 2004 11:36 PM





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