1

I was trying to pull some data out of an XML file using xmlstarlet, but

xmlstarlet sel -t  -v '//div[contains(@class,'addresses')]/a'

was getting me a lot more than I expected.

I finally worked out - with the help of replacing -v with -c to debug the xpath - that it was because the single quotes around 'addresses' were vanishing because the whole argument is single-quoted: replacing that with "addresses" worked as expected.

What I'm really confused about is quite what contains(@class,addresses) does - because xmlstarlet didn't flag it as an error or warn about it, even though I presume it's treated as an undefined variable.

I checked the source in https://github.com/fishjam/xmlstarlet/blob/master/src/xml_select.c but I can't spot a 'warnings' flag - should there be something like that, or are xpaths defined to allow this?

1 Answer 1

2

In contains(@class,addresses), addresses selects (possible) child elements named addresses of the context node (i.e. in //div[contains(@class,addresses)] the div element the predicate [contains(@class,addresses)] belongs to); a variable (reference) would be written as $addresses.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response... but my divs didn't hold any <address> elements, so why were any divs being selected? I'm a bit perplexed that contains(@class,"address") means 'if the class attribute contains the word address' but not quoting it means something... weird.
Your question just said you expected an error or warning in terms of an undeclared variable; there was no indication you got something selected you didn't expect to be selected; I think this is an edge case where the definition of contains says for an empty string as the second argument the result is true; and an empty node-set (which e.g. contains(@class, addresses) has as the second argument) is converted to an empty string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.