question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parsed XML doc's XPath queries fail with upper case attributes

See original GitHub issue

Basic info:

  • Node.js version: 8.9.1
  • jsdom version: 14.0.0

When using XPath to query/evaluate an XML document generated by JSDOM’s DOMParser, the attribute part of the query seems to be forced to lower case, making it impossible to query for attributes that contain upper cased characters.

For example, given the following document:

<?xml version="1.0" encoding="utf-8"?><example Foo="bar"></example>
                                               ^-- capital F

A query for //*[@Foo="bar"] (or //*[@foo="bar"] for that matter) returns no matches, however given this document instead with lower case attributes:

<?xml version="1.0" encoding="utf-8"?><example foo="bar"></example>
                                               ^-- lower case F

Now with this new document, //*[@foo="bar"] (and the upper case equivalent //*[@Foo="bar"]) successfully find the match.

I have found a similar issue from a long time ago (https://github.com/jsdom/jsdom/issues/651) that was fixed by introducing Saxes to parse XML documents separately from HTML documents, however currently at the parsing level everything seems to be parsed correctly (e.g. the attributes retain their case). It is at the XPath evaluation level that the query seems to be lower cased.

I have tried narrowing down where the error might be occurring, and have reached https://github.com/jsdom/jsdom/blob/b83783da63deeb7c5602b024a92e214df423a412/lib/jsdom/level3/xpath.js#L1659

Setting that shouldLowerCase value to false fixes for my use case, but I am not aware of what implications that has for the rest of the XPath implementation.

Minimal reproduction case

const { JSDOM } = require("jsdom");

const dom = new JSDOM();

const domParser = new dom.window.DOMParser();
const doc = domParser.parseFromString('<?xml version="1.0" encoding="utf-8"?><example Foo="bar"></example>', 'text/xml');
const result = doc.evaluate('//*[@Foo="bar"]', doc, null, XPathResult.ANY_TYPE, null);
const exampleNode = result.iterateNext();
console.log('Result:', exampleNode); // exampleNode is null

How does similar code behave in browsers?

https://jsbin.com/qegifaqumi/2/edit?js,console

The XPath query is case sensitive and can match against nodes with attributes with upper case characters.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

0reactions
dprentiscommented, Jan 11, 2022

Thanks for the quick replies - I thought there must be a catch. Probably won’t have enough spare time for this, but I can take a look.

Read more comments on GitHub >

github_iconTop Results From Across the Web

XQuery, XSLT, and XPath Error Codes Namespace Document
It is a static error if a value other than #default is specified for either the stylesheet-prefix or the result-prefix attributes of the...
Read more >
XPath fails on an XML document in R - Stack Overflow
I'd like to extract all the attribute's values beginning with "Actr" inside <name> tags. I've tried this XPath in an XMLeditor //tei:name/@ref[ ...
Read more >
Evaluating XPath Expressions | Using XML Tools
Describes how to evaluate XPath expressions by using %XML.XPATH.Document. ... This step parses the XML document, using a built-in XSLT processor.
Read more >
Chapter 9. Testing and XML - JUnit Recipes
Comparing XML documents with XMLUnit; Ignoring superficial differences in XML ... API that you can use to execute XPath queries on a parsed...
Read more >
XML Syntax and Parsing Concepts - InformIT
Notice that this is not merely a convention; it is an absolute requirement. An XHTML document that contains capital letters in element or ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found