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 May 11th, 2009, 10:08 AM
Authorized User
 
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to get value from xpath in value-of tag

HI,

I am using xml version="1.0", I am creating node name dynamical by concatenating other values, but as a result i am getting node name insted of node value

<xsl:variable name="TotalCustomerAmount">
<xsl:value-of select="concat('Cust_Total_',Totals_Tran_Hour,'_AM OUNT')"></xsl:value-of>
</xsl:variable>


Please suggest me what to do

Thanks
Dishant
 
Old May 11th, 2009, 10:12 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You've shown your code and told us it's wrong, but we don't know what your input or desired output are like so it's rather difficult to help you.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old May 11th, 2009, 10:37 AM
Authorized User
 
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mhkay,

Following is input and desired output

I am creating node "Cust_Total_1_AMOUNT" in Context , in this scenario in the node name value at postion 1 will be changing [e.g Cust_Total_24_AMOUNT, Cust_Total_10_AMOUNT]

Totals_Tran_Hour node will contains the values , so I have used concat to create the node name and put it under value-of Select tag.

Here my Ouptput should be value of node Cust_Total_1_AMOUNT [Cust_Total_1_AMOUNT=5] in variable TotalCustomerAmount but insted of value my value of variable is TotalCustomerAmount= Cust_Total_1_AMOUNT.


Thanks,
Dishant
 
Old May 11th, 2009, 11:09 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

What you seem to want is not possible in pure XSLT. You cannot create XPath dynamically and evaluate it. Some processors have extensions that cope with this, which processor are you using?
__________________
Joe
http://joe.fawcett.name/
 
Old May 11th, 2009, 11:13 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, I still don't understand what you are trying to do. Please show the input and output IN XML. Your English description is unclear.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old May 12th, 2009, 04:25 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

If you are using Saxon processor, then you can use saxon:evaluate function.
The below code
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://icl.com/saxon">
<xsl:output method="xml" indent="yes"></xsl:output>
<xsl:template match="root">

<xsl:variable name="tth" select="Totals_Tran_Hour"></xsl:variable>
<xsl:variable name="tca" select="concat('Cust_Total_',$tth,'_AMOUNT')"></xsl:variable>
<xsl:variable name="TotalCustomerAmount">
<xsl:value-of select="saxon:evaluate($tca)"/>
</xsl:variable> 
<xsl:value-of select="$TotalCustomerAmount"/>

</xsl:template>
</xsl:stylesheet>


for an input
Code:
<root>
<Totals_Tran_Hour>1</Totals_Tran_Hour>
<Cust_Total_1_AMOUNT>5</Cust_Total_1_AMOUNT>
</root>


gives 5 as output. If you want a different output, then post the input xml and required output xml.

__________________
Rummy
 
Old May 12th, 2009, 04:46 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Looks like mrame came closer to understanding the requirement than I did. But if this is indeed the requirement, then saxon:evaluate is overkill: you can do

Code:
<xsl:value-of select="*[name()=$tca]"/>
Oh, and NEVER do

Code:
<xsl:variable name="x><xsl:value-of select="EXP"/></xsl:variable>
when you could do

Code:
<xsl:variable name="x" select="EXP"/>
It's not only verbose, it's also incredibly inefficient, because it involves unnecessary copying of data.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old May 12th, 2009, 06:03 AM
Authorized User
 
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael Kay,

First of all i apologias for my English description.

My Input is

<Context>
<Totals_Tran_Hour>5</Totals_Tran_Hour>
<Cust_Total_5_AMOUNT>100</Cust_Total_5_AMOUNT>
<Amount>40</Amount>
<Context>

----------------------------------------------------------
Code:
<xsl:template match="Context">
<xsl:variable name="Amount">
<xsl:value-of select="Amount"></xsl:value-of>
</xsl:variable>
<xsl:variable name="TotalCustomerAmount">
<xsl:value-of select="concat('Cust_Total_',Totals_Tran_Hour,'_AM OUNT')"></xsl:value-of>
</xsl:variable>

<xsl:element name="Result">
<xsl:choose>
<xsl:when test="$TotalCustomerAmount > $Amount">
<xsl:value-of select=" 'FAIL' "/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select=" 'PASS' "/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>

</xsl:template>

------------------------------------------------------------
Excepted Output:
<Result>FAIL</Result>

-----------------------------------------------------------
OutPut getting
<Result>PASS</Result>


# I am getting this result because my Variable TotalCustomerAmount=Cust_Total_5_AMOUNT instead of TotalCustomerAmount=5
# I am using xalan processor.


Thanks,
Dishant
 
Old May 12th, 2009, 06:22 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try this:
Code:
<xsl:template match="Context">
<xsl:variable name="Amount" select="Amount"></xsl:variable>
<xsl:variable name="tth" select="concat('Cust_Total_',Totals_Tran_Hour,'_AMOUNT')"></xsl:variable>
<xsl:variable name="TotalCustomerAmount" select="*[name() = $tth]"></xsl:variable>

<Result>
<xsl:choose>
<xsl:when test="$TotalCustomerAmount > $Amount">
<xsl:value-of select=" 'FAIL' "/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select=" 'PASS' "/>
</xsl:otherwise>
</xsl:choose>
</Result>

</xsl:template>

__________________
Rummy





Similar Threads
Thread Thread Starter Forum Replies Last Post
Tag Search and Tag Cloud Search ilayaraja SQL Server 2005 1 April 15th, 2008 05:11 AM
HTML tag from C# or ASP.NET tag from javascript angshujit ASP.NET 2.0 Basics 3 February 16th, 2007 10:07 AM
How to use XPath to retrieve a node tag rs:data? y10k XML 1 May 5th, 2006 01:11 PM
HTML tag vs Body Tag CFGerry BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 October 7th, 2005 07:13 AM
Trying to output a tag within a tag jaucourt XSLT 3 January 12th, 2005 11:57 AM





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