XPath: Default to 'Node A', select 'Node B' instead if 'Node B' is not empty

前端 未结 3 1428
忘了有多久
忘了有多久 2021-02-20 00:15

I need to create an XPath expression that does the following:

  • Returns the element inside of \'NodeA\' by default
  • Returns the element inside of \'NodeB\' i
3条回答
  •  星月不相逢
    2021-02-20 00:42

    This XPath expression returns NodeB if it exists (and has text content) and NodeA in the other case:

    //my:NodeB[text()] | //my:NodeA[text() and not(//my:NodeB[text()])]
    

    If you want to get all sub-elements you can append /* after the selected node, like this

    //my:NodeB[text()]/* | //my:NodeA[text() and not(//my:NodeB[text()])]/*
    

提交回复
热议问题