Simultaneously escape double and single quotes in Xpath

前端 未结 4 1000
梦谈多话
梦谈多话 2021-01-12 16:57

Similar to How to deal with single quote in xpath, I want to escape single quotes. The difference is that I can\'t exclude the possibility that a double quote might also app

4条回答
  •  灰色年华
    2021-01-12 17:39

    use quote() for xpath query

    library(XML)
    

    only single quote inside string

    target1 <- "Father's son"
    doc1 <- XML::newHTMLDoc()
    newXMLNode("div", 1, parent = getNodeSet(doc1, "//body"), doc = doc1)
    newXMLNode("div", target1, parent = getNodeSet(doc1, "//body"), doc = doc1)
    xpath_query1 <- paste0('//*[ contains(text(), ', '"', target1, '"', ')]')
    getNodeSet(doc1, xpath_query1)
    

    both single and double quote inside string

    target2 <- "Fat\"her's son"
    doc2 <- XML::newHTMLDoc()
    newXMLNode("div", 1, parent = getNodeSet(doc2, "//body"), doc = doc2)
    newXMLNode("div", target2, parent = getNodeSet(doc2, "//body"), doc = doc2)
    xpath_query2 <- quote('//body/*[contains(.,concat(\'Fat"\',"her\'s son"))]')
    getNodeSet(doc2, xpath_query2)
    

    Output:

    getNodeSet(doc1, xpath_query1)
    # [[1]]
    # 
    Father's son
    # # attr(,"class") # [1] "XMLNodeSet" getNodeSet(doc2, xpath_query2) # [[1]] #
    Fat"her's son
    # # attr(,"class") # [1] "XMLNodeSet"

提交回复
热议问题