Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
Namespace resolver supporting default namespace in xml to evaluate xpath

My XML document starts like :

<?xml version='1.0' encoding='UTF-8'?>
<article  xmlns:d="http://docbook.org/ns/docbook"
          xmlns="http://docbook.org/ns/docbook"
          version="5.0">
<info>
 <title>My title</title>
 <annotation>
   <segmentedlist>
     <segtitle>role</segtitle>
     <segtitle>id</segtitle>
     <segtitle>description</segtitle>
     <seglistitem>
       <seg>input</seg>
       <seg>hash123</seg>
       <seg>INPUT</seg>
     </seglistitem>
   </segmentedlist>
  </annotation>
 </info>
 <simpara/>
</article>

I need to find elements with xpath queries like :

var inputList = currentDocument.evaluate("//annotation/segmentedlist/seglistitem/seg",
                                          currentDocument,
                                          null,
                                          XPathResult.ANY_TYPE,null);

Because of default namespace xmlns="http://docbook.org/ns/docbook" associated to another explicit namespace, the namespace resolver fails when evaluating xpath expressions.

As I understand the note in https://developer.mozilla.org/en-US/docs/Web/XPath/Introduction_to_using_XPath_in_JavaScript#implementing_a_default_namespace_resolver , there is no simple pattern to support "normal xpath expression" with such a declaration of namespaces. Proposed solution using "namespace-uri()=[...]" seems to complexify xpath expressions.

Anyone has a pattern to propose ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use XPath 3.1 with Saxon-JS 2 (https://www.saxonica.com/saxon-js/documentation2/index.html):

const xml = `<?xml version='1.0' encoding='UTF-8'?>
<article  xmlns:d="http://docbook.org/ns/docbook"
          xmlns="http://docbook.org/ns/docbook"
          version="5.0">
<info>
 <title>My title</title>
 <annotation>
   <segmentedlist>
     <segtitle>role</segtitle>
     <segtitle>id</segtitle>
     <segtitle>description</segtitle>
     <seglistitem>
       <seg>input</seg>
       <seg>hash123</seg>
       <seg>INPUT</seg>
     </seglistitem>
   </segmentedlist>
  </annotation>
 </info>
 <simpara/>
</article>`;

const xmlDoc = new DOMParser().parseFromString(xml, 'application/xml');

const segElements = SaxonJS.XPath.evaluate('//annotation/segmentedlist/seglistitem/seg', xmlDoc , { 'xpathDefaultNamespace' : 'http://docbook.org/ns/docbook' });

console.log(segElements.length, segElements);
<script src="https://www.saxonica.com/saxon-js/documentation2/SaxonJS/SaxonJS2.rt.js"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!