Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7947981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:35:25+00:00 2026-06-04T01:35:25+00:00

I would like to use javascript XPaths in a web app using exslt extensions,

  • 0

I would like to use javascript XPaths in a web app using exslt extensions, but I can’t figure out how to do this.

Pretend I’ve got an html doc with some divs in it. 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 that results in an invalid XPath expression exception in evaluate. I’m using chrome.

Is there anything else I need to do to make this stuff work? I see on exslt.org that there are implementations for javascript, but how do I make sure those are available? Do I need to insert my javascript into a namespaced script element in the dom or something insane?

UPDATE

If this isn’t possible directly using browser dom + javascript and xpath, would it be possible to write XSLT using exslt extensions in the browser to simulate document.evaluate (returning a list of elements that match the xpath)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:35:26+00:00Added an answer on June 4, 2026 at 1:35 am

    I don’t think the default browser XPath implementation supports EXSLT. The javascript support mentioned on the EXSLT page is likely about how you can provide your own implementation of the exslt function using in-browser.javascript. Here’s one example I was able to find very quickly.

    In Firefox, for example, you can have Saxon-B as an extension to run XSLT2.0 and Saxon-B has built-in support for exslt (unlike Saxon-HE), though you will likely be better off just using XSLT/XPath 2.0 features. Here’s the regular expression syntax, for example. That said, however, relying on a Mozilla Saxon-B extension isn’t something that will help you with Chrome or other browsers for that matter.

    With that said I don’t think you can find a cross-browser solution to use EXSLT extensions in your XPath. The conformance section of the DOM Level 3 XPath calls for XPath 1.0 support and doesn’t mention EXSLT. The INVALID_EXPRESSION_ERR is said to be thrown:

    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.

    Finally, here’s an open bugzilla ticket for Firefox to open up EXSLT support for their DOM Level 3 XPath implementation. It seems to be sitting there in NEW status since 2007. The ticket says that:

    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. Here’s the test case.

    —

    If you don’t mind me asking, what exactly you wanted to use the regex for? Maybe we can help you get away with a combination of standard XPath string functions?

    —

    UPDATE You can build an XPath runner via XSLT (like you’re asking in the update to your question) but it won’t return the nodes from the source document, it will return new nodes that look exactly the same. XSLT produces a new result tree document and I don’t think there’s a way to let it return references to the original nodes.

    As far as I can tell, Mozilla (and Chrome) both support XSLT not only for XML documents loaded from external sources, but also for DOM elements from the document being displayed. The XSLTProcessor documentation mentions how tranformToFragment(), for example, will only produce HTML DOM objects if the owner document is itself an HTMLDocument, or if the output method of the stylesheet is HTML.

    Here’s a simple XPath Runner that I built testing out your ides:

    1) First you would need an XSLT template to work with.

    <?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>
    

    I started building it in the JavaScript using the document.implementation.createDocument APi but figured it would be easier to just load it. FF still supports document.load while Chrome only lets you load stuff using XHR. You would need to start your Chrome with --allow-file-access-from-files if you want to load files with XHR from your local disk.

    2) Once we have the template loaded we would need to modify the value of the select attribute of the xsl:copy-of instruction to run the XPath we need:

    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); 
    }
    

    You can now run it with something like:

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

    It works great for “regular” XPath like that //div[@id] (and fails to find //div[@not-there]) but I just can’t get it to run the regexp:test extension function. With the //div[regexp:test(string(@id), "a")] it doesn’t error out, just returns empty set.

    Mozilla documentation suggests their XSLT processor support EXSLT. I would imagine they are all using libxml/libxslt behind the scenes anyway. That said, I couldn’t get it to work in Mozilla either.

    Hope it helps.

    Any chance you can get away with jQuery regexp? not likely to be helpful for your XPath builder utility but still a way to run regexp on HTML nodes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use the group_by method, but instead of using a column
I would like to use a JavaScript richtextbox library in my application. What library
I would like to use an external javascript file in another javascript file. For
I would like to use src/main/javascript as the source directory for my javascript files
I would like to know if is better to use application/javascript or application/ecmascript and
I would like to know if it is possible to use canvases and javascript
I would like to use complex numbers as defined in C99, but I need
I would like to use JavaScript to clean up text that’s being copied from
I would like to use Google Image search, however I can't find the proper
I am creating a simple form. I would like to use embedded javascript to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.