XPath selecting multiple elements with predicates

前端 未结 3 1621
小蘑菇
小蘑菇 2021-01-04 11:26

I have an XPath query which is doing what I want, namely selecting a union of \'surname\' & \'given-names\' with the given predicates (It\'s actually either/or that I ne

相关标签:
3条回答
  • 2021-01-04 12:19

    In addition to Alejandro's and Seva's good answers, another alternative is

    (/header/authors/surname | /header/authors/given-names)[text() and @id='1']
    

    This is valid in XPath 1.0. It still is a bit redundant, but is more succinct than the long-winded version in your question.

    A further alternative would be to declare a variable for surname elements and a variable for given-name elements, and then

    ($surnames | $given-names)[text() and @id='1']
    

    That would probably be longer in total, but easier to read than the above.

    0 讨论(0)
  • 2021-01-04 12:22

    How about

    /header/authors/*[(name() = "surname" or name()="given-names") and ./text() and @id='1'] 
    
    0 讨论(0)
  • 2021-01-04 12:23

    First, this is valid XPath 2.0:

    /header/authors/(surname|given-names)[./text() and @id='1'] 
    

    Second, this XPath 1.0:

    /header/authors/*[self::surname|self::given-names][text()][@id='1'] 
    
    0 讨论(0)
提交回复
热议问题