 |
| 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
|
|
|
|

May 11th, 2009, 10:08 AM
|
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 11th, 2009, 10:12 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

May 11th, 2009, 10:37 AM
|
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 11th, 2009, 11:09 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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?
|
|

May 11th, 2009, 11:13 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

May 12th, 2009, 04:25 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
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
|
|

May 12th, 2009, 04:46 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

May 12th, 2009, 06:03 AM
|
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 12th, 2009, 06:22 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
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
|
|
 |