Performing complicated XPath queries in Scala

前端 未结 5 1172
灰色年华
灰色年华 2021-01-04 08:14

What\'s the simplest API to use in scala to perform the following XPath queries on a document?

//s:Annotation[@type=\'attitude\']/s:Content/s:Parameter[@role         


        
5条回答
  •  旧时难觅i
    2021-01-04 08:51

    //s:Annotation[@type='attitude']/s:Content/s:Parameter[@role='type' and not(text())]
    

    Well, I don't understand the s: notation, and couldn't find it on XPath spec either. However, ignoring that this would look like this:

    (
      (xml 
        \\ "Annotation" 
        filter (_ \ "@type" contains Text("x"))
      ) 
      \ "Content" 
      \ "Parameter" 
      filter (el => (el \ "@type" contains Text("type")) && el.isInstanceOf[Text])
    )
    

    Note the necessity of parenthesis because of higher precedence of \ over filter. I have changed the formatting to a multi-line expression as the Scala equivalent is just way too verbose for a single line.

    I can't answer about namespaces, though. No clue how to work with them on searches, if it's even possible. The docs mention @{uri}attribute for prefixed attributes, not does not mention anything about prefixed elements. Also, note that you need to pass an uri which resolves to the namespace you want, as literal namespaces in search are not supported.

提交回复
热议问题