如何使XSLT Javascript扩展函数返回节点集? [英] How can make an XSLT Javascript extension function return a node-set?

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

问题描述

在用javascript编写的XSLT 1.0中是否有一种简单的方法来具有扩展功能,以返回节点集?
我可以为此创建一个新的Java类,但我只想在脚本本身中放入一些代码即可.
如果可以使用所有或大多数XSLT处理器支持的另一种脚本语言(VB脚本,Groovy,C#?)来完成此操作,那当然也可以.

Is there a simple way to have an extension function in XSLT 1.0 written in javascript return a node-set?
I could create a new java class for this, but I would rather just put some code in the script itself.
When this can be done in another scripting language supported by all or most XSLT processors (VB script? Groovy? C#?), then that's OK too of course.

我有以下简单的脚本:

<msxsl:script language="JScript" implements-prefix="custom">
    function xml (input) {
        var x = input.split(";");
        return x.toString();
    }
</msxsl:script>

返回一个字符串,因此在Xpath表达式中调用该函数没有问题.
我想拥有的是节点集结果.但是当我将脚本更改为

which returns a string, and hence no problem calling the function in Xpath expressions.
What I would like to have, is a node-set result. But when I change my script to

<msxsl:script language="JScript" implements-prefix="custom">
    function xml (input) {
        var x = input.split(";");
        return x;
    }
</msxsl:script>

然后调用该函数会产生错误,因为该数组不会自动转换为节点集.

then calling the function gives an error because the array is not automatically converted to a node-set.

我查看了 arrays-with-java-xslt-extensions ,但更多为此,我希望避免这种情况.

I looked at arrays-with-java-xslt-extensions but that's more in the line of creating a new class for this, which I wish to avoid for now.

那么应该将哪些语句添加到脚本中才能将数组转换为节点集,从而允许在Xpath表达式中使用函数调用?

So which statements should be added to the script in order to transform the array into a node-set, allowing the function call to be used in Xpath expressions?

推荐答案

此处是一个示例,只要在允许XSLT中的脚本实现扩展功能的模式下运行,该示例就应与MSXML 6一起使用.样式表代码如下:

Here is an example that should work with MSXML 6 as long as run in a mode allowing script in XSLT to implement extension functions. The stylesheet code is as follows:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  xmlns:ms="urn:schemas-microsoft-com:xslt"
  xmlns:my="http://example.com/my"
  exclude-result-prefixes="ms my">

  <xsl:output method="html" version="5.0"/>

  <ms:script language="JScript" implements-prefix="my">
  <![CDATA[
  function tokenize (input) {
    var doc = new ActiveXObject('Msxml2.DOMDocument.6.0');
    var fragment = doc.createDocumentFragment();
    var tokens = input.split(';');
    for (var i = 0, l = tokens.length; i < l; i++)
    {
      var item = doc.createElement('item');
      item.text = tokens[i];
      fragment.appendChild(item);
    }
    return fragment.selectNodes('item');
  }
  ]]>
  </ms:script>

  <xsl:template match="/">
    <html>
      <head>
        <title>Example</title>
      </head>
      <body>
        <h1>Example</h1>
        <ul>
          <xsl:apply-templates select="my:tokenize('Kibology;for;all')"/>
        </ul>
      </body>
    </html>
   </xsl:template>

   <xsl:template match="item">
     <li>
       <xsl:value-of select="."/>
     </li>
   </xsl:template>

</xsl:stylesheet>

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

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