如何声明返回节点集的用户定义函数? [英] How to declare a user-defined function returning node-set?

查看:54
本文介绍了如何声明返回节点集的用户定义函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要这样的东西:

<msxsl:script language="C#">
   ??? getNodes() { ... return ... }
</msxsl:script>

<xsl:for-each select="user:getNodes()">
    ...
</xsl:for-each>

我应该为 getNodes()使用哪种返回类型?应该在其主体中放入什么?

What return type should i use for getNodes() and what should i put in it's body?

推荐答案

原则上,您需要使用XPathNodeIterator返回节点集(如Samjudson所说).我认为您给出的示例是一个退化的函数,因为您没有为其提供任何参数.但是,我认为这很有启发性,看看您如何可以凭空制造节点.

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>

但是,通常情况下,您可能希望在函数中执行xslt中无法完成的操作,例如根据某些条件过滤节点集.可以通过代码更好地实现或取决于某些外部数据结构的标准.另一个选择就是您将简化一个冗长的表达式(如下面的示例所示).然后,您将一些参数传递给getNodes函数.为简单起见,我使用基于XPath的过滤器,但它可以是任何东西:

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

这篇关于如何声明返回节点集的用户定义函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆