Using exslt extensions will be used in javascript xpaths

I would like to use javascript XPaths in a web application using exslt extensions, but I cannot figure out how to do this.

Pretend that I have an html document with some div files. I want to run this:

namespaces={'regexp':'http://exslt.org/regular-expressions'};
result = document.evaluate( 
             "//div[regexp:test(.,'$')]", 
             document, 
             function(ns){ 
                 return namespaces.hasOwnProperty(ns) ? namespaces[ns] : null;
             }, 
             XPathResult.ANY_TYPE, 
             null);

Only this leads to an invalid XPath expression exception during evaluation. I use chrome.

Is there anything else that needs to be done to get this stuff to work? I see on exslt.org that there are implementations for javascript, but how can I make sure they are available? Do i need to embed javascript in an element named script in dom or something crazy?

UPDATE

dom + javascript xpath, XSLT exslt- document.evaluate( , xpath)?

+3
1

, XPath EXSLT. javascript, EXSLT, , exslt, in-browser.javascript. , .

Firefox, , Saxon-B XSLT2.0 Saxon-B exslt ( Saxon-HE), , , XSLT/XPath 2.0. , , . , , Mozilla Saxon-B, , Chrome .

, - EXSLT XPath. DOM 3 XPath XPath 1.0 EXSLT. , INVALID_EXPRESSION_ERR :

if the expression has a syntax error or otherwise is not a legal expression according to the rules of the specific XPathEvaluator or contains specialized extension functions or variables not supported by this implementation.

, bugzilla Firefox, EXSLT DOM 3 XPath. , NEW 2007 . , :

Currently Mozilla gives an exception "The expression is not a legal expression." even if a namespace resolver correctly resolving the EXSLT prefixes to the corresponding URLs is passed in. .

-

, , ? , XPath?

-

UPDATE. XPath XSLT ( ), , , . XSLT , , .

, Mozilla ( Chrome) XSLT XML, , DOM . XSLTProcessor , tranformToFragment(), , will only produce HTML DOM objects if the owner document is itself an HTMLDocument, or if the output method of the stylesheet is HTML.

XPath, , :

1) XSLT .

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:regexp="http://exslt.org/regular-expressions" 
    extension-element-prefixes="regexp">

    <xsl:template match="/">
        <xsl:copy-of select="."/>
    </xsl:template> 
</xsl:stylesheet>

JavaScript document.implementation.createDocument APi, , . FF - document.load, Chrome XHR. Chrome --allow-file-access-from-files, XHR .

2) , select xsl:copy-of XPath, :

function runXPath(xpath) {
    var processor = new XSLTProcessor(); 
    var xsltns = 'http://www.w3.org/1999/XSL/Transform';

    var xmlhttp = new window.XMLHttpRequest();
    xmlhttp.open("GET", "xpathrunner.xslt", false);
    xmlhttp.send(null);

    var transform = xmlhttp.responseXML.documentElement;

    var copyof = transform.getElementsByTagNameNS(xsltns, 'copy-of')[0];
    copyof.setAttribute('select', xpath);

    processor.importStylesheet(transform);          

    var body = document.getElementById('body'); // I gave my <body> an id attribute
    return processor.transformToFragment(body, document); 
}

- :

var nodes = runXPath('//div[@id]');
console.log(nodes.hasChildNodes());
if (nodes.firstChild) {
    console.log(nodes.firstChild.localName);
}

"" XPath, //div[@id] ( //div[@not-there]) , regexp:test. //div[regexp:test(string(@id), "a")] , .

Mozilla XSLT- EXSLT. , libxml/libxslt . , Mozilla.

, .

, jQuery regexp? XPath builder, HTML.

+1

All Articles