I would like to get to most inner div in a html document that has the id or class containing “content”.
What I have tried:
//div[@id[contains(.,'content') and not(*)]]
This works for getting the most inner div with an id containing “content”.
Now I want to get the most inner div by id or class (depends on whats the most inner is) containing the id or class “content”.
Sample data:
<body>
<div class="outerContent">
<div id="moreContent">
<div class="anotherContent">
This is what I am looking for.
</div>
</div>
</div>
</body>
or
<body>
<div class="outerContent">
<div id="moreContent">
<div id="anotherContent">
This is what I am looking for.
</div>
</div>
</div>
</body>
Note that “This is what I am looking for” could be inside a div class conainting “content” or a div id containing “content”.
Thank you!
This answer uses only XPath 1.0 expressions. My understanding is that XPath 2.0 isn’t available.
Use:
This selects any
divelement whoseidattribute has string value that contains the string"content", or whoseclassattribute has string value that contains the string"content", and that has no descendantdivelements with this properties.Do note, that such thing as “the most inner div” may not be singular — that is, many
divelements may exist such that they fulfill the conditions set in the question.If this is the case, and you need just one such
divelement (say, the 1st), you can use:XSLT – based verification:
When this transformation is applied on the first provided XML document:
the Xpath expression is evaluated and the result of this evaluation is copied to the output:
With the second document, again the correct result is produced:
Finally, in case the comparisson for “Content” should be case-independet, use: