概念 XML XLST 前兄弟和祖先 [英] Concept XML XLST preceding-sibling and ancestor

查看:24
本文介绍了概念 XML XLST 前兄弟和祖先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 xslt 很陌生,发现它可以很简单也可以很复杂.我想澄清一些概念.什么是前兄弟,什么是祖先,在google上搜索后,找到了祖先的解释.他们网站上的图表更容易理解.

I am very new to xslt, and found it can be easy or complex. I want to make clear some concepts. What is preceding-sibling and what is ancestor, after searching from google, I found ancestor explanation. and the chart from their website makes easier to understand.

可是我还是不懂前辈

<product>
    <inventory>
        <drink>
            <lemonade>
                <price>$2.50</price>
                <amount>20</amount>
            </lemonade>
            <pop>
                <price>$1.50</price>
                <amount>10</amount>
            </pop>
        </drink>
          <service>
           <address />
            <phone />
            <delivery> City </delivery>
          </service>      
        <snack>
            <chips>
                <price>$4.50</price>
                <amount>60</amount>
            </chips>
        </snack>
        <hotfood></hotfood>
         <totalprice> $15</totleprice>

    </inventory>
</product>

那么我怎么读这个preceding-sibling::pop/ancestor::inventory/totalprice

祖先::库存/总价=产品\库存\总价previous-sibling::pop - 我不明白这个那么如何一起阅读呢?

ancestor::inventory/totalprice = product\inventory\totalprice preceding-sibling::pop - I dont understand this one then how to read all together?

非常感谢

推荐答案

Previous-sibling::axis

preceding-sibling:: 轴是一个导航轴,包括焦点元素之前的所有同级元素.兄弟"是指与参考项具有相同父项的不同元素.在前"是指在参考节点之前出现的节点.preceding-sibling 轴的顺序是反向文档顺序.看看这个文件:

The preceding-sibling:: axis

The preceding-sibling:: axis is an axis of navigation that includes all the preceding sibling elements to the focus element. By "sibling" we mean a different element which has the same parent to the reference item. By "preceding" we mean a node that occurs before the reference one. The order of the preceding-sibling axis is the reverse document order. Take a look at this document:

<fruit>
  <banana>
   <lady-finger-banana/>
  </banana> 
  <apple/>
  <pear/>
  <kiwi/>
</fruit>

如果焦点节点是梨形,那么序列preceding-sibling::*是...

If the focus node is pear, then the sequence preceding-sibling::* is ...

  1. 苹果
  2. 香蕉

注意:水果、梨、手指香蕉和猕猴桃不在序列中.

Note: fruit, pear, lady-finger-banana and kiwi are not in the sequence.

所以以下是正确的:

  • preceding-sibling::*[ 1] 是苹果
  • preceding-sibling::*[ 2] 是香蕉
  • count(previous-sibling::*) 是 2
  • preceding-sibling::apple[1] 也是苹果
  • preceding-sibling::banana[1] 是香蕉
  • preceding-sibling::*[ 3] 不存在或空序列
  • preceding-sibling::*[ 1] is the apple
  • preceding-sibling::*[ 2] is the banana
  • count( preceding-sibling::*) is 2
  • preceding-sibling::apple[ 1] is also the apple
  • preceding-sibling::banana[ 1] is the banana
  • preceding-sibling::*[ 3] is absent or the empty sequence

我们必须对您的示例文档稍作修改,才能有效地研究此示例

We have to alter your sample document a little bit to usefully study this example

<product>
    <inventory>
        <drink>
            <lemonade>
                <price>$2.50</price>
                <amount>20</amount>
            </lemonade>
            <pop>
                <price>$1.50</price>
                <amount>10</amount>
            </pop>
            <focus-item />
         </drink>
        <totalprice>$15</totalprice>  
    </inventory>
</product>

假设焦点在元素 focus-item 上.要计算表达式 preceding-sibling::pop/ancestor::inventory/totalprice,请执行以下步骤:

Let us say the focus is on the element focus-item. To evaluate the expression preceding-sibling::pop/ancestor::inventory/totalprice follow these steps:

  1. preceding-sibling::pop 选择所有前面的 pop 元素到 focus-item.这计算为一个节点的序列.
  2. 对于左手序列中的每个项目(只是一个 pop 元素,它确实发生了),将此项目设置为临时焦点项目,并评估/右侧的表达式运算符是 ...

  1. preceding-sibling::pop selects all the preceding pop elements to focus-item. This evaluates to a sequence of one node.
  2. For each item in the left hand sequence (just one pop element it so happens), set this item as a temporary focus item, and evaluate the expression of the right of the / operator which is ...

ancestor::inventory

只有一个这样的节点,就是祖先库存节点.因此,第一个/运算符评估为一个库存节点的序列.

There is only one such node, which is the ancestral inventory node. Thus the first / operator evaluates to a sequence of one inventory node.

现在我们评估第二个/及其右侧操作数表达式总价的影响.对于左侧序列中的每个项目(只有一个库存节点,因此它发生),将其设置为临时焦点项目并评估 totalprice.

Now we evaluate the effect of the second / and its right-hand operand expression total price. For each item in the left hand sequence (just one inventory node so it happens), set this as a temporary focus item and evaluate totalprice.

通过图表理解

这是preceding-sibling:: 的图表.其中,参考节点是 Charlie,preceding-sibling:: 轴上的节点是绿色的.它是唯一一个这样的节点.

Understanding by Diagrams

Here is a diagram for preceding-sibling::. In it the reference node is Charlie and the node on the preceding-sibling:: axis is in green. It is the only such node.

这篇关于概念 XML XLST 前兄弟和祖先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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