How can make an XSLT Javascript extension function return a node-set?

前端 未结 2 1763
無奈伤痛
無奈伤痛 2020-12-12 01:56

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 jus

相关标签:
2条回答
  • 2020-12-12 02:23

    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>
    
    0 讨论(0)
  • 2020-12-12 02:24

    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.

    Any questions about XSLT java or javascript extensions need to say what product(s) you are talking about, because there are no standards here.

    0 讨论(0)
提交回复
热议问题