Ben
Ben

Reputation: 5182

Checking emptiness of an element in hpricot

Let's say this is the location element: <.location>blah...<./location>

It can be empty like this: <.location/>

Is there a way to detect the backslash in the empty element in order to not return it?

Upvotes: 0

Views: 228

Answers (2)

dismal_denizen
dismal_denizen

Reputation: 87

<location/> is semantically identical to <location></location>, and should be treated as such. To find all empty tags, just skip elements which do not have any child nodes (including text).

Upvotes: 0

Pesto
Pesto

Reputation: 23900

If what you really want is the text inside location tags, you can find those easily with the right XPath:

doc.search('//location/text()')

If, for some reason, you actually need the location element itself, use this:

doc.search('//location/text()/..')

Upvotes: 1

Related Questions