Which should I use: preceding:: or preceding-sibling::?

后端 未结 3 620
深忆病人
深忆病人 2021-01-14 08:28

I\'m hesitating whether to write preceding or preceding-sibling in XSL such as an example xml below.


  

        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-14 09:06

    It is better to write a meaningful XPath as it is easier to read and will be easier to maintain. So, I would say I prefer the first way, since it is the closest to the English description of your intention :

    preceding-sibling::team/staff[1]
    

    or even more specific (if necessary) :

    preceding-sibling::team[@area='Tokyo']/staff[1]
    

    Also, element positions is generally less meaningful and tends to break easily upon slight changes on the XML arrangement, so I'd suggest not to rely on element position index too much.

提交回复
热议问题