marcaddeo
marcaddeo

Reputation: 177

XPath - Get node that only has children with specific attribute

I have an SVG document that contains nodes similar to the following:

<item>
    <g>
        <path fill="#FFFFFF" ... ></path>
        <path fill="none" ... ></path>
        <ellipse fill="none" ... ></path>
    </g>
    <g>
        <path fill="none" ... ></path>
        <path fill="none" ... ></path>
        <ellipse fill="none" ... ></path>
    </g>
    <g>
       <path fill="#FFFFFF" ... ></path>
       <path fill="#FFFFFF" ... ></path>
       <ellipse fill="#FFFFFF" ... ></path>
    </g>
</item>

What I'm trying to do is only select the <g> nodes that ONLY contain children that have the fill="none" attribute. So I'm trying to find an XPath that only matches the second <g> node in my example.

I've done my share of googling and searching on stack overflow and nothing seems to work.

The closest one that I thought SHOULD work is giving me an error saying it's not a valid XPath Expression in Google Chrome's developer tools using the $x() helper.

*[name()="g" and descendants::*[@fill="none"]]

Any help is appreciated

Thanks

Upvotes: 0

Views: 326

Answers (1)

paul trmbrth
paul trmbrth

Reputation: 20748

Try:

*[    name()="g"
  and descendant::*[@fill="none"]
  and count(descendant::*[@fill="none"]) = count(descendant::*)
 ]

Upvotes: 3

Related Questions