How to declare a user-defined function returning node-set?

.net, msxml, xslt

Solution

In principle you need to use the XPathNodeIterator to return node sets (as Samjudson says). I take it that the example you gave is a degenerated function, as you do not supply it with any parameters. However, I think it is instructive the see how you could fabricate nodes out of thin air.

<msxsl:script language="C#">
   XPathNodeIterator getNodes() 
   { 
      XmlDocument doc = new XmlDocument();
      doc.PreserveWhitespace = true;
      doc.LoadXml("<root><fld>val</fld><fld>val2</fld></root>");
      return doc.CreateNavigator().Select("/root/fld");
   }
</msxsl:script>

However, typically you would want to do something in your function that is not possible in xslt, like filtering a node set based on some criteria. A criteria that is better implemented through code or depends om some external data structure. Another option is just that you would to simplify a wordy expression (as in the example bellow). Then you would pass some parameters to you getNodes function. For simplicity I use a XPath based filtering but it could be anything:

   <msxsl:script language="C#">
       XPathNodeIterator getNodes(XPathNodeIterator NodesToFilter, string Criteria)
      {
         XPathNodeIterator x = NodesToFilter.Current.Select("SOMEVERYCOMPLEXPATH["+Criteria+"]");
         return x;
      }
   </msxsl:script>
   <xsl:for-each select="user:getNodes(values/val,'SomeCriteria')">
    ...
  </xsl:for-each>

Hopes this helps, Boaz

Problem

I want something like this: ``` <msxsl:script language="C#"> ??? getNodes() { ... return ... } </msxsl:script> <xsl:for-each select="user:getNodes()"> ... </xsl:for-each> ``` What return type should i use for `getNodes()` and what should i put in it's body?

Original source