我应该使用哪个:previous:: 或previous-sibling::? [英] Which should I use: preceding:: or preceding-sibling::?

查看:22
本文介绍了我应该使用哪个:previous:: 或previous-sibling::?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在犹豫是用XSL写preceding还是preceding-sibling,比如下面的xml示例.

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

<sales_division>
  <team area="Tokyo">
    <staff>Sato</staff>
    <staff>Tanaka</staff>
  </team>
  <team area="Osaka">
    <staff>Ueda</staff>
    <staff>Suzuki</staff>
  </team>
</sales_division>

如果当前节点是team(@Osaka),我想得到team(@Tokyo)<的第一个子节点staff(Sato)/code>,我应该怎么写 Xpath 合适?

If current node is team(@Osaka), and I want to get first-child node staff(Sato) of team(@Tokyo), what should I write Xpath appropriately?

我猜有以下三个选项.

I guess three options below.

1) preceding-sibling::team/staff[1]<br>
2) preceding::staff[2]<br>
3) ../team[1]/staff[1]

请给我您的建议和意见.

Please give me your advice and opinions.

推荐答案

最好编写一个有意义的 XPath,因为它更易于阅读且更易于维护.所以,我会说我更喜欢第一种方式,因为它最接近您意图的英文描述:

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]

此外,元素位置通常意义不大,并且在 XML 排列上稍有变化时很容易中断,因此我建议不要过分依赖元素位置索引.

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.

这篇关于我应该使用哪个:previous:: 或previous-sibling::?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆