Passing a param for a apply-template select in xlst, .net
I have the following in my xslt开发者_开发技巧 file:
<xsl:param name="predicate" select="//Event" />
<xsl:apply-templates select="$predicate" />
And this works fine, but now I'd like to change the param from my .net code.
var args = new XsltArgumentList();
args.AddParam("predicate", "", "//Event[@valid]");
xmlviewer.TransformArgumentList = args;
but no matter what i pass in for predicate, I get the error "Expression must evaluate to a node set."
Is there a way to pass the xpath selector into the transform?
args.AddParam("predicate", "", "//Event[@valid]");
You are passing just a string to the stylesheet, but the stylesheet is using the predicate
parameter as a node-set -- it performs an <xsl:apply-templates>
on it.
The solution
Evaluate the XPath expression you are now passing as a string. For example, use the Select()
method of XPathNavigator. Then pass as parameter to the transformation the resulting XPathNodeIterator.
Ultimately, $predicate
here is the string "//Event[@valid]"
- you have essentially done:
<xsl:apply-templates select="'//Event[@valid]'"/>
i.e. tried to evaluate a template on a string. That can't work. You can, however, test for values of @valid
, for example
<xsl:param name="valid" select="" />
<xsl:apply-templates select="//Event[@valid=$valid]" />
Of course, since xslt is xml, another way to approach this would be to load the xslt into a DOM and replace the xpath expression prior to execution.
精彩评论