1

I'd like to select a number of elements on a page using document.selectQueryAll() or element.selectQueryAll() using a partial name for their attributes.

NOTE: NOT using partial attribute value.

WHY? (You may know a better way to do this using Namespaces directly???)

I want to find all elements where

<div  xmlns:${prefix}='{a particular namespace value I know}' >

etc

I want to figure out what the namespace prefix is, for a particular namespace I know I'm looking for, so then I can go search for sub-elements in that particular namespace.

I'd planned to find the element as described above, figure out the namespace, then go look for my sub-elements

querySelectAll( "[typeof='" + ${prefix} + ":" + "Offering" + "'" )

so I can find all sub-elements matching this

<div typeof="gr:Offering">

but I won't know in advance what the XML namespace prefix will be defined to be.

RightSaidFred
  • 11,209
  • 35
  • 35
Andrew Mackenzie
  • 5,477
  • 5
  • 48
  • 70

1 Answers1

0

Use XPath to query based on names, since it is namespace aware. For example:

document.evaluate( '//@*[contains(local-name(), "data-")]', document, Function, XPathResult.ANY_TYPE, null )

will get all data- attributes in the document.

There is a related question which may help with the syntax.

Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
  • The DOM is in memory and I'm querying it using Javascript and CSS selectors. How can I parse it using XPath? – Andrew Mackenzie Aug 30 '12 at 11:34
  • There are plenty of solutions, including the native [XPathEvaluator](https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript#Implementing_a_User_Defined_Namespace_Resolver) as well as utilities such as [e4x.js](https://github.com/eligrey/e4x.js), [AJAXSLT](http://code.google.com/p/ajaxslt/), or [Sarissa](http://dev.abiss.gr/sarissa/) – Paul Sweatte Aug 30 '12 at 14:17