Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#4509·cheerio

:scope selector not working on root node

Author: ShadyShenAliCreated Apr 3, 2025Updated Apr 3, 2025

To select the direct child of the root node, I'm using :scope pseudo class in the selector. For example,

const html=`
<collection>
  <id value="a"/>
  <relatedCollections>
    <collection>
      <id value="a1"/>
    </collection>
  </relatedCollections>
</collection>
<collection>
  <id value="b"/>
  <relatedCollections>
    <collection>
      <id value="b1"/>
    </collection>
  </relatedCollections>
</collection>
`
let $ = cheerio.load(html,{xmlMode:true},false);

to get the root collection id, I'm using

  $(":scope> collection > id").map(function(){ return $(this).attr("value");}).get()

, which should return ["a","b"] however, it is currently returning empty result []

Is it a bug? Any solution? or any workaround?

Thanks and Best Regards, Shady

Source: cheeriojs/cheerio

View original on GitHubView discussion on GitHub