tags:

views:

114

answers:

2

I've got the following ...

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                (etc.) >
    <xsl:param name="Query"/>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <r:results>
            <xsl:for-each select="$Query">
                (etc.)
            </xsl:for-each>
        </r:results>
    </xsl:template>

</xsl:stylesheet>

I want the value of the Query parameter to be the select of the for-each.

When I execute this transformation in C# I get ... Expression must evaluate to a node-set.

I know it's coming from the select because if I replace $Query with a hard-coded expression (the same value that I'm passing as the parameter value) it works fine.

Any ideas? Is this even possible?

Frank

+1  A: 

XSLT is compiled, and does not (according to the standard) support XPath re-evaluation at runtime - only at compile-time.

Some processors, such as Saxon, do have extensions to allow this, although they run with some limitations. You might want to check out the .Net version of the Saxon XSLT processor (.Net API page). Saxon has an extension function called saxon:evaluate which lets you do runtime XPath construction and evaluation.

The built-in Microsoft XSLT engine does not support calculated select XPath expressions.

One work-around is to build the XSLT as a string, substitute in your path, and then run it that way.

lavinio
A: 

It is possible only if your query is in an xml format. For e.g. (the eg below is taken from the input format reqd by SharePoint APIs )

<Query>
  <OrderBy>
    <FieldRef Name="Modified" Ascending="FALSE"></FieldRef>
  </OrderBy>
  <Where>
    <Or>
      <Neq>
        <FieldRef Name="Status"></FieldRef>
        <Value Type="Text">Completed</Value>
      </Neq>
      <IsNull>
        <FieldRef Name="Status"></FieldRef>
      </IsNull>
    </Or>
  </Where>
</Query>

Here in your xslt you can write:

<xsl:for-each select="$Query/OrderBy">

or

<xsl:for-each select="$Query/descendant-or-self::node()">

or

<xsl:for-each select="$Query/child::node()">

The error " Expression must evaluate to a node-set" means that it should be in a proper xml format so that select can work on the nodes returned by the expression.

Rashmi Pandit