Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

XML descendant problem #43

Open
11 of 20 tasks
ScottFreeCode opened this issue May 12, 2016 · 3 comments
Open
11 of 20 tasks

XML descendant problem #43

ScottFreeCode opened this issue May 12, 2016 · 3 comments

Comments

@ScottFreeCode
Copy link
Contributor

ScottFreeCode commented May 12, 2016

From discussion of #7, XML documents don't seem to be finding descendants (EDITTED: this list has been updated so that the checkbox indicates whether the native implementation is matched and the + or - afterward indicates whether the native implementation gets a result for the expression):

  • + //Root
  • + //Menu
  • + /descendant::Root
  • + /descendant::Menu
  • + /descendant-or-self::Root
  • + /descendant-or-self::Menu
  • + //self::Root
  • + //self::Menu
  • + //*[self::Root]
  • + //*[self::Menu]
  • - /Root//Root
  • + /Root//Menu
  • - /Root/descendant::Root
  • + /Root/descendant::Menu
  • + /Root/descendant-or-self::Root
  • + /Root/descendant-or-self::Menu
  • + /Root//self::Root
  • + /Root//self::Menu
  • - /Root//*[self::Root]
  • + /Root//*[self::Menu]

This appears to affect all browsers, provided the override is used to force install.

List generated by this code in case anyone wants to clean it up and make some kind of test out of it or tinker with it and see if anything else affects whether it works (EDITTED):

var wgx = {}
wgxpath.install({document: wgx})
var src = '<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE Root><Root><Menu ID="11"></Menu></Root>'
var xml = new DOMParser().parseFromString(src, "application/xml")
var prefixes = ["//", "/descendant::", "/descendant-or-self::", "//self::", "//*[self::"]
prefixes = prefixes.concat(prefixes.map(function(prefix) {
  return "/Root" + prefix
}))
prefixes.forEach(function(prefix) {
  ["Root", "Menu"].forEach(function(tag) {
    var resultWGX = wgx.evaluate(prefix + tag + (/[[]/.test(prefix)?"]":""), xml, wgx.createNSResolver(xml), XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
    var resultNative = xml.evaluate(prefix + tag + (/[[]/.test(prefix)?"]":""), xml, xml.createNSResolver(xml), XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
    document.write("- [" + (resultWGX===resultNative?"x":" ") + "] " + (resultNative?"+":"-") + " `" + prefix + tag + (/[[]/.test(prefix)?"]":"") + "`<br />")
  })
})

(EDITTED: the old list was generated by this code, which works on browsers that have no native implementation, but does not compare against the native implementation to automatically identify the few expression combinations that wouldn't work anyway:)

wgxpath.install(window, true)
var src = '<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE Root><Root><Menu ID="11"></Menu></Root>'
var xml = new DOMParser().parseFromString(src, "application/xml")
var prefixes = ["//", "/descendant::", "/descendant-or-self::", "//self::", "//*[self::"]
prefixes = prefixes.concat(prefixes.map(function(prefix) {
  return "/Root" + prefix
}))
prefixes.forEach(function(prefix) {
  ["Root", "Menu"].forEach(function(tag) {
    var result = xml.evaluate(prefix + tag + (/[[]/.test(prefix)?"]":""), xml, xml.createNSResolver(xml), XPathResult.ANY_TYPE, null)
    document.write("- [" + (result.iterateNext()?"x":" ") + "] `" + prefix + tag + (/[[]/.test(prefix)?"]":"") + "`<br />")
  })
})
@nedjs
Copy link
Contributor

nedjs commented May 13, 2016

Seems to be related to pull request #41.

@ScottFreeCode
Copy link
Contributor Author

Thanks -- in any case, I've updated this slightly to report whether WGX matches the native implementation, for the "Root descendant of Root" cases being auto-generated by this method of testing.

@dpwatrous
Copy link

dpwatrous commented Jul 8, 2016

In case anyone finds it useful, I've been messing around with case sensitivity issues in some of the descendant code. getElementsByTagName doesn't work properly with namespaced XML, and getElementsByTagNameNS needs the original case of the namespace.

It's not a complete (or very good) fix, but it did get the library working well enough for my specific use case.

Here's a link to the commit:

dpwatrous@42d4117

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants