1
votes

Problem:

I am having trouble generating an xpath expression that selects a node in a different section of the XML document by position. I am using xsl:variable to create the expression, but when I use a xsl:for-each with the value of the xsl:variable as my select statement I get an error.

<xsl:variable name="input_params_query">
  <xsl:text disable-output-escaping="yes">/example/inputs/dataset[</xsl:text>
  <xsl:number value="position()" format="1" />
  <xsl:text disable-output-escaping="yes">]/parameter</xsl:text>
</xsl:variable>

<xsl:for-each select="$input_params_query">
  <input rdf:resource="#{@name}"/>
</xsl:for-each>

Results in the error:

The 'select' expression does not evaluate to a node set.

When I print out the value of the xsl:variable I am using I get:

/example/inputs/dataset[1]/parameter

which is a valid and correct Xpath expression for the nodes I am trying to select in the for-each call.

Is my usage of the xsl:variable as xsl:for-each select attribute incorrect?

Background and Full Explanation:

I am using XSLT to generate a RDF/XML representation of information available in the following XML structure.

In this case what the XML really means is that a process was run twice; the first time generating output file "a" and the second time generation output file "b". The parameters "p1" and "p2" were inputs for the execution that generated file "a" and the parameter "p3" was an input to the execution that generated file "b".

For each output of 'process' I am generating a RDF individual and defining the inputs and outputs for that process execution. Basically, I want to define all values from /example/inputs/dataset[n]/parameters as inputs to the process which generates the output /example/process/outputs/file[n].

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 version="1.0">
 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
 <xsl:template match="//process">
  <xsl:for-each select="outputs/*">

  <!-- declare rdf resource of type Process, define Process outputs -->
  <!-- ... this I already have working so I have withheld for brevity -->

  <!-- define input parameters -->

   <xsl:variable name="input_params_query">
    <xsl:text disable-output-escaping="yes">/example/inputs/dataset[</xsl:text>
    <xsl:number value="position()" format="1" />
    <xsl:text disable-output-escaping="yes">]/parameter</xsl:text>
   </xsl:variable>

   <xsl:for-each select="$input_params_query">
    <input rdf:resource="#{@name}"/>
   </xsl:for-each>

  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
1
what xslt engine are you using?Rubens Farias

1 Answers

4
votes

"Is my usage of the xsl:variable as xsl:for-each select attribute incorrect?"

yes. the value specified in select attributes must be a valide node-set expression, that is, it must be an Xpath expression that evaluates to a (possibly empty) node set.

The variable you defined is of a string type. the string happens to be a valid Xpath expression, but it is still 'just' a string.

I think you can achieve your intended result by writing it like this:

<xsl:template match="//process">
  <xsl:for-each select="outputs/*">
    <!-- declare rdf resource of type Process, define Process outputs -->
    <!-- ... this I already have working so I have withheld for brevity -->

    <!-- define input parameters -->
    <xsl:variable name="position" select="position()"/>

    <xsl:for-each select="/example/inputs/dataset[$position]">
      <input rdf:resource="#{@name}"/>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>