概念 XML XLST 前兄弟和祖先

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12347412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:40:37  来源:igfitidea点击:

Concept XML XLST preceding-sibling and ancestor

xmlxsltxpath

提问by olo

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.

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

But I still don't understand preceding-sibling

但我还是不明白前辈

<product>
    <inventory>
        <drink>
            <lemonade>
                <price>.50</price>
                <amount>20</amount>
            </lemonade>
            <pop>
                <price>.50</price>
                <amount>10</amount>
            </pop>
        </drink>
          <service>
           <address />
            <phone />
            <delivery> City </delivery>
          </service>      
        <snack>
            <chips>
                <price>.50</price>
                <amount>60</amount>
            </chips>
        </snack>
        <hotfood></hotfood>
         <totalprice> </totleprice>

    </inventory>
</product>

so how do I read this preceding-sibling::pop/ancestor::inventory/totalprice

那么我如何阅读这个previous-sibling::pop/ancestor::inventory/totalprice

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

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

Many thanks

非常感谢

回答by Sean B. Durkin

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-siblingaxis is the reverse document order. Take a look at this document:

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

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

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

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

  1. apple
  2. banana
  1. 苹果
  2. 香蕉

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

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

So the following is true:

所以以下是正确的:

  • 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
  • preceding-sibling::*[ 1]是苹果
  • preceding-sibling::*[ 2]是香蕉吗
  • count( preceding-sibling::*)是 2
  • preceding-sibling::apple[ 1]也是苹果
  • preceding-sibling::banana[ 1]是香蕉吗
  • preceding-sibling::*[ 3]不存在或空序列

preceding-sibling::pop/ancestor::inventory/totalprice Example

前兄弟::流行/祖先::库存/总价格示例

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

我们必须稍微修改您的示例文档以有效地研究此示例

<product>
    <inventory>
        <drink>
            <lemonade>
                <price>.50</price>
                <amount>20</amount>
            </lemonade>
            <pop>
                <price>.50</price>
                <amount>10</amount>
            </pop>
            <focus-item />
         </drink>
        <totalprice></totalprice>  
    </inventory>
</product>

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

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

  1. preceding-sibling::popselects all the preceding popelements to focus-item. This evaluates to a sequence of one node.
  2. For each item in the left hand sequence (just one popelement 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.

  3. 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.

  4. totalpriceis short for child::totalprice. There is only one total price element on the child axis of the temporary focus node, so the final result is a sequence of one node, which is the total price node.
  1. preceding-sibling::pop选择所有前面的pop元素来关注项目。这评估为一个节点的序列。
  2. 对于左侧序列中的每个项目(pop它只是一个元素),将此项目设置为临时焦点项目,并评估 / 运算符右侧的表达式,即...

    ancestor::inventory
    

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

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

  4. totalprice是 的缩写child::totalprice。临时焦点节点的子轴上只有一个总价元素,所以最终结果是一个节点的序列,即总价节点。

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.

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

enter image description here

在此处输入图片说明

回答by OkieOth

Axes useful for navigation through the node tree. So it depends from your problem what kind of axis is useful.

轴可用于在节点树中导航。所以这取决于你的问题什么样的轴是有用的。

The following stylesheet illustrates the difference.

以下样式表说明了差异。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="snack">
        <xsl:variable name="siblings" select="ancestor::node()"/>
        <debug>
        <xsl:for-each select="preceding-sibling::node()">
                <sibling>
                        <xsl:value-of select="local-name()"/>
                </sibling>
        </xsl:for-each>
        <xsl:for-each select="ancestor::node()">
                <ancestor>
                        <xsl:value-of select="local-name()"/>
                </ancestor>
        </xsl:for-each>

        </debug>
   </xsl:template>

   <xsl:template match="*">
         <xsl:apply-templates select="*"/>
   </xsl:template>
</xsl:stylesheet>

回答by RetroCoder

Preceding-sibling gets all element siblings that preceded it in the current node level. Unless you specify one or more of those preceding siblings with an xpath expression. If you specify a specific preceding-sibling with xpath it always starts with 1 in square brackets.

Preceding-sibling 获取当前节点级别中位于它之前的所有元素同级。除非您使用 xpath 表达式指定一个或多个前面的同级。如果您使用 xpath 指定特定的前同级,则它始终以方括号中的 1 开头。

Ancestor is the first matching ancestor that matches the expression. So it goes back up the node tree to look at a matching expression based on where you currently are pointing. So if you were at product/inventory/drink/pop or just /pop then ancestor inventory/totalprice just looks for the frist occurence and it should only return back a pointer to point to that matching case else it will be pointing to nothing and you'll still be pointing at pop.

祖先是与表达式匹配的第一个匹配祖先。因此,它返回节点树以查看基于您当前指向的位置的匹配表达式。因此,如果您在 product/inventory/drink/pop 或只是 /pop 然后祖先库存/totalprice 只查找第一次出现,它应该只返回一个指向该匹配案例的指针,否则它将指向任何内容,而您'仍将指向流行音乐。