Equivalent of selectNodes and selectSingleNode in JS

0

I have a old JS code, but i didn't find the equivalent of some JS old function.

  • selectNodes
  • selectSingleNode

Here is the code :

function getFunctionProto( elem )
{
    var s = "";
    s += elem.getAttribute('name');
    s += "(";

    var oCat = elem.selectSingleNode( "ancestor(CATEGORY)" );
    var lParams = elem.selectNodes( "./PARAM" );
    if ( lParams != null )
    {
        for ( var i = 0 ;  i != lParams.length ; i++ )
        {
            if ( i != 0 ) s += ", ";
            s += lParams[i].getAttribute("name") + oCat;
        }
    }
    s += ")";
    return s;
}

EDIT : I tried with a new function but it doesn't work as well, it tells me :

Error occurred while executing stylesheet 'f_api.xsl'.
Code: 0x80020009
Microsoft JScript runtime error
This object does not handle this property or method
line = 95, col = 3 (the line is broken from the beginning of the script block).
Error returned by the method or property call.

I tried with this code, who is recent on JS :

function getCategoryLabel( elem )
{
  var s = "";

  s += elem.getAttribute("name");
  var oAnc = elem;

  return s;
}

I called the function on the XSL code like this :

<xsl:value-of select="user:getCategoryLabel(.)"/>

Thanks for your help.

javascript
asked on Stack Overflow Oct 24, 2019 by Zahreddine Laidi • edited Oct 28, 2019 by Zahreddine Laidi

1 Answer

0

selectNodes and selectSingleNode are functions exposed in the MSXML DOM API on nodes, since MSXML 3 usually to support XPath 1.0 based node selection. Your example elem.selectSingleNode( "ancestor(CATEGORY)" ); however is not XPath 1.0 (that would say ancestor::CATEGORY I think), so it looks as if you have some very old code using the no longer documented predecessor of XPath 1.0 that pre MSXML 3 versions supported or some code not using the right XPath syntax.

The only way to use that stuff with Javascript within the browser would be by using new ActiveXObject where supported, for instance in Microsoft Internet Explorer. Or inside of MSXML executed XSLT and embedded J(ava)Script, as your earlier question seemed to indicate, but at least with XSLT 1.0 and current IE versions if you call a function directly from XSLT, even if you pass in a single node, on the script side you will receive an IXMLDOMSelection https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms757852(v=vs.85) so a function would rather use

function foo(domSelection) {
  for (var i = 0; i < domSelection.length; i++) {
    var node = domSelection[i];
    var node2 = node.selectSingleNode("ancestor::CATEGORY");
    ...
  }
}

and in XSLT you would use e.g. pf:foo(.) to pass a single node or e.g. pf:foo(//bar) to pass a node-set.

answered on Stack Overflow Oct 25, 2019 by Martin Honnen

User contributions licensed under CC BY-SA 3.0