tags:

views:

240

answers:

1

I am passing parameter from C# to xsl in <xsl: for each> but I am not getting the output. Here is my code till now

<xsl:param name="xpath" select="sessions/session"/>
<xsl:template match="/">
  <xsl:value-of select="$xpath"/>
  <xsl:for-each select="exsl:node-set($xpath)">

And transformed whith

XslCompiledTransform xslt = new XslCompiledTransform();
XsltArgumentList xsArgs = new XsltArgumentList();
xslt.Load(strXstFile);

//creating xpath through some logic , it is working fine 
xsArgs.AddParam("xpath", "", xpath);

MemoryStream ms = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(ms, Encoding.ASCII);
StreamReader rd = new StreamReader(ms);
xslt.Transform(doc, xsArgs, writer);

I am checking the values through and values are passing perfectly as I want but when I am using t hem in xsl:foreach it is not displaying the results I expected.Earlier when I was not using exsl:node-set it was throwing error so I used it but I guess it is making my string something else.

Any idea how to resolve this problem?

+2  A: 

I suspect that you are under the misapprehension that you can set a parameter or variable to a string and then use that string as an XPath query. You cannot.

This:

<xsl:param name="xpath" select="sessions/session"/>

creates a parameter named xpath and sets it, by default, to a node-set. Since the context node is the root, the node-set will only contain anything if the top-level element of the input document is named sessions and it has at least one session child element.

Here's what $xpath doesn't contain: an XPath expression.

If, in your C# code, you set the parameter to a string containing an XPath expression, then instead of containing a node-set, it will contain a string. This:

<xsl:value-of select='$xpath'/>

will emit that string, and this:

<xsl:for-each select='exsl:node-set($xpath)'>

will do nothing, since the node-set function expects its argument to be a result tree fragment, and $xpath contains a string.

I'd bet that what you really want to do is something more like this: change the name of the parameter from xpath to something less misleading, like nodeset, and create the node-set in your C# code:

xsArgs.AddParam("nodeset", "", doc.SelectNodes(xpath));
Robert Rossney
Dynamically evaluating an XPath expression seems possible with the ("experimental") extension function `dyn:evaluate(strXPathExpression)`. See http://www.exslt.org/dyn/functions/evaluate/index.html
0xA3
yah you are rite Robert thats what I was doing wrong . I was assigning string to the parameter but you are rite I need to create the node-set in my C# code..its working fine now ....Thanks a lot....
TSSS22