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 June 18th, 2009, 09:07 AM
Registered User
 
Join Date: Jun 2009
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Default Extract value from XML and modify

Hi,

I am new to XSLT, I need a help to extract the value from XML and change that value.
I can able to do for simple XML , but not getting how to do with attributes.

I have a XML like this:

Code:
<document-list>
<documentid="66112"collection="products"primarycategoryid="cat05004"categoryids="cat05004 pcmcat67800050010">
<attribute name="startdate">2004-06-01</attribute>
<attribute name="currentpricesaletype">N</attribute>
<attribute name="onlineonly">false</attribute>
<attribute name="skulongdescription"></attribute>
<attribute name="listprice">2000</attribute>
</document>
</document-list>

I want to get the value of "listprice" (2000) and update that (Ex: listprice + 50)

How can i do this.

Please suggest me.

Thanks for any help.....


-Sharath
 
Old June 18th, 2009, 09:16 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Attributes are referenced using the '@' symbol, so @name refers to the name attribute.

To do a condition based on that attribute value you would do "attribute[@name='listprice']"

I'm not sure what you mean by 'update' as you are essentially producing a brand new XML document every time you run an XSLT process, so you do not update the original, but output a modified version.

The following would match on a listprice attribute and output a new element with a increased price:

Code:
<xsl:template match="attribute[@name='listprice']">
  <attribute name="listprice"></xsl:value-of select="number(text()) + 50"/></attribute>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
sharath123 (June 18th, 2009)
 
Old June 18th, 2009, 09:16 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can't modify "in place" with XSLT. If you want to copy the whole document and just change the price then you'll need the identity template (search online). Then add a template that matches the correct attribute and changes it:
Code:
<xsl:template match="attribute[@name = 'listprice']">
  <attribute name="listprice"><xsl:value-of select="number(.) + 50 /></attribute>
</xsl:template>
__________________
Joe
http://joe.fawcett.name/
The Following User Says Thank You to joefawcett For This Useful Post:
sharath123 (June 18th, 2009)
 
Old June 18th, 2009, 09:41 AM
Registered User
 
Join Date: Jun 2009
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanx a lot...it worked....
 
Old June 18th, 2009, 10:20 AM
Registered User
 
Join Date: Jun 2009
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
Attributes are referenced using the '@' symbol, so @name refers to the name attribute.

To do a condition based on that attribute value you would do "attribute[@name='listprice']"

I'm not sure what you mean by 'update' as you are essentially producing a brand new XML document every time you run an XSLT process, so you do not update the original, but output a modified version.

The following would match on a listprice attribute and output a new element with a increased price:

Code:
<xsl:template match="attribute[@name='listprice']">
  <attribute name="listprice"></xsl:value-of select="number(text()) + 50"/></attribute>
</xsl:template>

Thanx for ypur input...
I need one more help...
Can i assign a new value for one node by calculating the values from other node...

Ex:
My XML is like this:
Code:
<document-list>
  <document id="366112" collection="products" primarycategoryid="cat05004" categoryids="cat05004 pcmcat67800050010">
    <attribute name="currentprice">3000</attribute>
    <attribute name="onlineonly">false</attribute>
    <attribute name="listprice">2000</attribute>
    <attribute name="margin"></attribute>
  </document>
</document-list>
and i want to calculate "margin" as "currentprice - listprice".
Is this possible?
Please suggest me...


Thanks
-sharath
 
Old June 18th, 2009, 10:45 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

How about having a go yourself and showing what you've tried?
__________________
Joe
http://joe.fawcett.name/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Modify XML with XSLT aowen355 XSLT 4 December 26th, 2007 10:19 AM
how to extract THIS node from XML in ASP?? zoreli XML 1 July 21st, 2006 09:51 AM
re: modify the column in a xml file ramya General .NET 1 January 20th, 2005 04:50 AM
Javascript and using the XMLDOM to modify xml TheNinthPlayer Javascript 1 January 5th, 2005 05:56 AM
extract xml using asp daddycool2k Classic ASP XML 0 November 17th, 2003 12:13 PM





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