xslt 2.0: variable as an xpath expression?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

xslt 2.0: variable as an xpath expression?

Tom Voltz

I have been banging my head against the wall on this one problem, and thought saxon experts might have some ideas.  (I’m using XSLT 2.0 for this project..)

 

I have built up a variable that will help me locate a specific node in my document as follows:

 

<xsl:variable name="argument_xpath">/command<xsl:for-each select="remove(tokenize(path,'\s+'),1)">/parm_list/argument[name='<xsl:value-of select="."/>']</xsl:for-each>

 

Given the value of path is “clock set time”,

The result of the code above is:

/command/parm_list/argument[name='set']/parm_list/argument[name='time']”

 

However, I can not for the life of me figure out how to get this to be parsed by Saxon as an xpath expression instead of a string value:

<xsl:for-each select=”’$argument_xpath’”> . . .

Does not work, nor does the variation:

<xsl:for-each select=”’/command/$argument_xpath’”> . . (Assuming I truncated the variable so this is a correct xpath that will find the target node.)

 

I can of course use variables within an xpath expression, in some contexts:

//command[name=’$x’] for example works like a charm.

 

Any hints or ideas on how to get Saxon to treat an entire $variable as an xpath expression to be evaluated against the main document?

 

 

Tom

 

Reply | Threaded
Open this post in threaded view
|

Re: xslt 2.0: variable as an xpath expression?

Wolfgang Hoschek-2
The saxon:evaluate extension function should do the trick.

Wolfgang.

On Dec 5, 2005, at 3:12 PM, Tom Voltz wrote:

> I have been banging my head against the wall on this one problem,  
> and thought saxon experts might have some ideas.  (I’m using XSLT  
> 2.0 for this project..)
>
>
> I have built up a variable that will help me locate a specific node  
> in my document as follows:
>
>
> <xsl:variable name="argument_xpath">/command<xsl:for-each  
> select="remove(tokenize(path,'\s+'),1)">/parm_list/argument
> [name='<xsl:value-of select="."/>']</xsl:for-each>
>
>
> Given the value of path is “clock set time”,
>
> The result of the code above is:
>
> “/command/parm_list/argument[name='set']/parm_list/argument
> [name='time']”
>
>
> However, I can not for the life of me figure out how to get this to  
> be parsed by Saxon as an xpath expression instead of a string value:
>
> <xsl:for-each select=”’$argument_xpath’”> . . .
>
> Does not work, nor does the variation:
>
> <xsl:for-each select=”’/command/$argument_xpath’”> . . (Assuming I  
> truncated the variable so this is a correct xpath that will find  
> the target node.)
>
>
> I can of course use variables within an xpath expression, in some  
> contexts:
>
> //command[name=’$x’] for example works like a charm.
>
>
> Any hints or ideas on how to get Saxon to treat an entire $variable  
> as an xpath expression to be evaluated against the main document?
>
>
>
> Tom
>
>
>



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
<a href="http://ads.osdn.com/?ad_idv37&alloc_id865&op=click">http://ads.osdn.com/?ad_idv37&alloc_id865&op=click
_______________________________________________
saxon-help mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help
Reply | Threaded
Open this post in threaded view
|

RE: xslt 2.0: variable as an xpath expression?

Michael Kay
In reply to this post by Tom Voltz
The saxon:evaluate() extension function takes as input a string containing an XPath expression, and returns the result of evaluating that expression, which seems to be exactly the function that you want. Read the spec carefully to make sure you understand the rules for the context of the expression.
 
I'm not convinced this is the right design in your case. A cleaner (faster and more standard) solution might be a recursive function:
 
<xsl:function name="f:select-args" as="element(argument)*">
  <xsl:param name="plist" as="element(parm_list)*"/>
  <xsl:param name="path" as="xs:string+"/>
  <xsl:variable name="step" select="$plist/argument[name=$path[1]]"/>
  <xsl:choose>
     <xsl:when test="exists($path[2])">
        <xsl:sequence select="f:select-args($step/parm_list, remove($path[1]))"/>
     </xsl:when>
     <xsl:otherwise>
        <xsl:sequence select="$step"/>
     </xsl:otherwise>
   </xsl:choose>
</xsl:function>
 
then
 
<xsl:for-each select="f:select-args(/command/parm_list, remove(tokenize(path, '\s+'), 1))">
    ....
 
Michael Kay
http://www.saxonica.com/


From: [hidden email] [mailto:[hidden email]] On Behalf Of Tom Voltz
Sent: 05 December 2005 23:12
To: [hidden email]
Subject: [saxon] xslt 2.0: variable as an xpath expression?

I have been banging my head against the wall on this one problem, and thought saxon experts might have some ideas.  (I’m using XSLT 2.0 for this project..)

 

I have built up a variable that will help me locate a specific node in my document as follows:

 

<xsl:variable name="argument_xpath">/command<xsl:for-each select="remove(tokenize(path,'\s+'),1)">/parm_list/argument[name='<xsl:value-of select="."/>']</xsl:for-each>

 

Given the value of path is “clock set time”,

The result of the code above is:

/command/parm_list/argument[name='set']/parm_list/argument[name='time']”

 

However, I can not for the life of me figure out how to get this to be parsed by Saxon as an xpath expression instead of a string value:

<xsl:for-each select=”’$argument_xpath’”> . . .

Does not work, nor does the variation:

<xsl:for-each select=”’/command/$argument_xpath’”> . . (Assuming I truncated the variable so this is a correct xpath that will find the target node.)

 

I can of course use variables within an xpath expression, in some contexts:

//command[name=’$x’] for example works like a charm.

 

Any hints or ideas on how to get Saxon to treat an entire $variable as an xpath expression to be evaluated against the main document?

 

 

Tom