XPath to select multiple tags

XPath to select multiple tags

Asked on December 28, 2018 in XML.
Add Comment


  • 3 Answer(s)

    A fair solution is:

    /a/b/*[self::c or self::d or self::e]
    

         Note that, this both are too-long and incorrect.

    a/b/*[local-name()='c' or local-name()='d' or local-name()='e']
    

         The XPath expression will choose nodes like given below,

    OhMy:c
     
    NotWanted:d
     
    QuiteDifferent:e
    

     

    Answered on December 28, 2018.
    Add Comment

         Xpath to select multiple tags, avoid the repetition with an attribute test on behalf of that,

    a/b/*[local-name()='c' or local-name()='d' or local-name()='e']
    

    the above is correct in a vacuum where the OP has not defined the interaction with namespaces. The self:: axis is namespace that opposed, local-name() is not. If the OP’s intention is to capture c|d|e regardless of namespace then it is “another solution that still have some positive votes” that is incorrect.  

    Answered on December 28, 2018.
    Add Comment

         Selecting multiple tags, abc.xml is the doc explained by OP, 

    (require '[saxon :as xml])
    (def abc-doc (xml/compile-xml (slurp "abc.xml")))
    (xml/query "a/b/(c|d|e)" abc-doc)
    => (#<XdmNode <c>C1</c>>
       #<XdmNode <d>D1</d>>
       #<XdmNode <e>E1</e>>
       #<XdmNode <c>C2</c>>
       #<XdmNode <d>D2</d>>
       #<XdmNode <e>E1</e>>)
    
    Answered on December 28, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.