What is the correct usage of xml2::find_all_lgl()?

I thought xml2::find_all_lgl() would return a logical vector indicating which nodes an xpath match was found on. However I can't seem to get the function to return a result other than an error. Anyone have examples of correct usage?

# how I expect it to work and what it would return
x %>%
  xml_node("body") %>%
  xml_find_lgl("//div/p")

# [1]  TRUE  TRUE  FALSE

# however the following is returned:
# Error: result of type: 'list', not logical

# body has 3 parent div nodes -- 2 of which have p nodes
x <- read_html(
  "<html>
       <body>
           <div>
               <div>
                   <p>text</p>
                   <p>more text</p>
               </div>
               <div class='here'>
                   <p>second nested div</p>
               </div>
           </div>
           <div>
               <p>no nested divs here</p>
           </div>
           <div>
           </div>
       </body>
   </html>")

There are no example usages of this function in 1.3.2, and the xml2:::xml_find_lgl.xml_node() method calls C code, which I feel is beyond my comprehension because it appears to be the same as xml2:::xml_find_all.xml_node() except it checks that the result is logical -- I don't know of any xpath that would return a logical result since they mostly appear to return a part of the document (node, attribute, text, etc).

A lightbulb went off and I decided to check the /tests/ folder, and sure enough there are coverage examples.

Going off these test examples I came up with:

# using above x
x %>%
    xml_node("body") %>%
    xml_find_lgl("//div/p/li=true()")
# [1] FALSE

x %>%
    xml_node("body") %>%
    xml_find_lgl("//div/p=true()")
# [1] TRUE

It works correctly though I still feel like it should return a logic result matching the number of nodes in the xml_node.

That being said, is there a way to recurse through nodes in an xml_node to return such a result?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.