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 14th, 2006, 06:19 AM
Registered User
 
Join Date: Feb 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default For-each using Variable

Hi,

I am trying to assign a variable to a value which I can use in a for-each loop. Eg.

<xsl:variable name="source" select = "NewDataSet/Relation/Source"/>
(Where NewDataSet/Relation/Source has a value of "..//Table7").

When I try to use this in my for-each it doesn't work. However, if I assign the variable to the value
<xsl:variable name="source" select = "..//Table7"/> it works.

Grateful for any help/advice
 
Old February 14th, 2006, 06:34 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:variable name="source" select = "NewDataSet/Relation/Source"/>
(Where NewDataSet/Relation/Source has a value of "..//Table7").

So the value of $source is the string "..//Table7"

When I try to use this in my for-each it doesn't work.

You mean you're doing

<xsl:for-each select="$source">

Well, in XSLT 1.0, the value of the select attribute has to be a node-set and as we saw earlier the value of $source is a string. Did you imagine that the system would magically recognize that the string "../Table7" looks like a path expression and should therefore be automatically compiled and evaluated to produce a node-set?

Standard XSLT (whether 1.0 or 2.0) doesn't have any built-in capability to evaluate path expressions that are constructed at run-time from strings, or read from source documents. Many products have extensions that do this (see for example saxon:evaluate) though the details of how they handle context, namespaces, etc, varies from one product to another.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 14th, 2006, 06:43 AM
Registered User
 
Join Date: Feb 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael,

Thanks for the quick response. To be honest I wasnt sure if it would work at all, but trying to use my limited XSLT knowledge to come up with a solution.

So presumably assigning the equally string like value "..//Table7" directly to the variable works the as this is picked up as a node set ?

Is there a better way of trying to achieve this ?


Alex

 
Old February 14th, 2006, 06:44 AM
Registered User
 
Join Date: Feb 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael,

Thanks for the quick response. To be honest I wasnt sure if it would work at all, but trying to use my limited XSLT knowledge to come up with a solution.

So presumably assigning the equally string like value "..//Table7" directly to the variable works the as this is picked up as a node set ?

Is there a better way of trying to achieve this ?


Alex
 
Old February 14th, 2006, 06:44 AM
Registered User
 
Join Date: Feb 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael,

Thanks for the quick response. To be honest I wasnt sure if it would work at all, but trying to use my limited XSLT knowledge to come up with a solution.

So presumably assigning the equally string like value "..//Table7" directly to the variable works as this is recognised as a node set ?

Is there a better way of trying to achieve this ?
 
Old February 14th, 2006, 06:45 AM
Registered User
 
Join Date: Feb 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael,

Thanks for the quick response. To be honest I wasnt sure if it would work at all, but trying to use my limited XSLT knowledge to come up with a solution.

So presumably assigning the equally string like value "..//Table7" directly to the variable works the as this is picked up as a node set ?

Is there a better way of trying to achieve this ?
 
Old February 14th, 2006, 08:07 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

When you do

select="../Table7"

the value of the select attribute is an XPath expression, which is evaluated to return a set of nodes

When you do

select="$path"

the value of the select attribute is an XPath expression, which is evaluated to return a string, perhaps the string "../Table7". So what you're looking for is not "evaluate this expression", but "evaluate this expression to produce another expression and then evaluate that expression". This is precisely what saxon:evaluate() does.

If you have XPath expressions in source documents, there are two ways of handling them. You can use extensions such as saxon:evaluate() (or your own extension function written in Javascript if you're in the MSXML/DOM world). Or you can write a stylesheet that generates a stylesheet, and then run that stylesheet. Both are perfectly respectable things to do, but both require more than pure XSLT coding.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 14th, 2006, 08:31 AM
Registered User
 
Join Date: Feb 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael,

Thanks for your help. Cheers.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Object variable or With block variable not set haidee_mccaffrey Classic ASP Professional 5 March 8th, 2007 03:34 PM
ASSIGNING A JAVA SCRIPT VARIABLE TO A XSL VARIABLE SOMANATHAN10 XSLT 1 February 21st, 2007 04:26 AM
object variable or with block variable not set Aoude BOOK: Beginning VB.NET Databases 1 February 24th, 2006 05:21 PM
Object variable or With block variable not set tparrish Pro VB Databases 1 May 25th, 2005 02:08 PM
Object variable or with block variable not set spacy ASP.NET 1.x and 2.0 Application Design 0 September 21st, 2004 12:19 AM





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