Home > Back-end >  CSS selector or XPath for elements without specific child
CSS selector or XPath for elements without specific child

Time:04-24

Here is the example:

<ul>
    <li>list item one</li>
    <li>list item two
        <ol>
            <li test="video">Nested item one</li>
            <li test="audio">nested item two</li>
        </ol>
        <ol>
            <li test="video">Nested item three</li>
        </ol>
    </li>
    <li>list item three</li>
</ul>

If I only want to select the elements of <ol> which does not have child <li test="audio">, what's the CSS selector or XPath please?

CodePudding user response:

This XPath,

//ol[not(li/@test="audio")]

will select all ol elements that do not have an li child with a test attribute value of "audio".


Update per OP follow-up comment adding the requirement that the ol element must also have a li child with a test attribute value of "video"...

This XPath,

//ol[not(li/@test="audio") and li/@test="video"]

will select all ol elements that

  • do not have an li child with a test attribute value of "audio", and
  • do have an li child with a test attribute value of "video"
  • Related