SASDigitalHumanitiesTraining / TextEncoding

Text Encoding for Ancient and Modern Literature, Languages and History
9 stars 5 forks source link

How do you ask for multiple specific elements in xpath? #27

Open bitparity opened 2 years ago

bitparity commented 2 years ago

I'm going back over some of the xpath exercises from the last one, and I wanted to write an xpath for question 2.1, "Write an axis expression that finds all sibling elements of second-level divs," but that would exclude other divs.

I know //div/div/preceding-sibling::*[name(.) != "div"] works, but to make it proper, i need a following-sibling version. How do I ask for preceding-sibling::* AND following-sibling::* with my given predicate?

adelle-l-hay commented 2 years ago

I don't have an answer, but this is something I would like more clarification on too.

cmohge1 commented 2 years ago

Good question. So my 'answer' to this is

//div/div[preceding-sibling::div or following-sibling::div]

We're basically asking you to find all second-level divs with siblings (which is simple enough because they are all divs) but it's less simple to formalise that as an axis, so you're on the right track. I just use an 'or' operator within the predicate to join the two filters. As with many of these questions, there are several ways to get the right answer.

cmohge1 commented 2 years ago

As to the general question of looking for multiple elements, you can join multiple XPath expressions with a pipe |. E.g. say you want a list of a line group and ab elements in Bad Hamlet.

//lg | //ab