English 中文(简体)
• 如何使LT印展功能恢复 no?
原标题:How can make an XSLT Javascript extension function return a node-set?

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>

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>

然后,称职造成错误,因为阵列不会自动转换为 no。

arrays-with-java-xslt-extensions。 但是,这更符合为此设立一个新类别的规定,我现在要避免。

因此,在文字中应加上哪些声明,以便把阵列变成一个节点,使功能在Xpath表述中使用?

最佳回答

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>
问题回答

If you want nodes returned, you ll have to create the nodes yourself, using DOM interfaces. I suspect (from memory) that if you return a DOM NodeList from your javascript function it will be treated by the calling XPath code as an XPath nodeset - though you ll have to check the spec carefully for details about how duplicate nodes and document order are handled.

任何关于SLT java或javascript分机延伸的问题都需要说,你所说的产品是什么,因为这里没有标准。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签