Re: select nodes with xpath, having by specific attribute named 'class' the 'subvalue part' set
Marek Mänd <[email protected]>
| Newsgroups | gmane.comp.mozilla.devel.xml |
|---|---|
| Message-ID | <[email protected]> |
Martin Honnen wrote:
> XPath 1.0 is not very strong in dealing with string values which are
> really a list of separate values thus if you are using XPath within
> JavaScript I think it is easier to start with XPath
Thank You for Your superb answer.
Being back for some months in JS-world, I want to learn something new
thatswhy i dropped that approach, but yes, it would be reasonable to do
so as it would give more flexibility over (e.c. select nodes that have
specific two/three etc 'separate-subvalues' set - which probably would
be definately PITA with Xpath ... ) 'pure' allsolving Xpath expression.
> Using only XPath 1.0 it is very convoluted, watch out for line breaks
> the posting introduces:
> function findElementsByClassName (xmlDocument, className) {
> var xpathExpression =
> '//element[normalize-space(@class) = "' + className + '"' +
> ' or starts-with(normalize-space(@class), "' + className + ' ")' +
> ' or contains(normalize-space(@class), " ' + className + ' ")' +
> ' or (substring(normalize-space(@class),
> string-length(normalize-space(@class)) ' +
> ' - string-length(" ' + className + '") + 1) = " ' + className + '")]';
> var elements = [];
Indeed it looks quite awkward. I'd wish there were possibilities to
write regexes in the path, but sadly by the specification things arent
meant to be done so.
I have a small refining question:
why do I have to use
normalize-space(@class) = "className"
too?
Why isnt the
starts-with(normalize-space(@class), "className")
sufficient for both cases?
TIA.