How do I select the last N elements with XPath?

前端 未结 2 1686
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-15 23:37

I support a web site which generates content XML that is then translated into web pages using XSLT. I have been asked to create a new stylesheet which will transform the ou

2条回答
  •  时光说笑
    2020-12-16 00:10

    Browsing through the XSLT spec today, I found a note which explains why // behaves this way:

    // is short for /descendant-or-self::node()/. For example, //para is short for /descendant-or-self::node()/child::para and so will select any para element in the document (even a para element that is a document element will be selected by //para since the document element node is a child of the root node); div//para is short for div/descendant-or-self::node()/child::para and so will select all para descendants of div children.

    NOTE: The location path //para[1] does not mean the same as the location path /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents.

    In other words, when using //, the position() is calculated along the child axis, not the descendant-or-self axis. Specifying descendant or descendant-or-self allows you to get the first/last n nodes as you'd expect:

    
    

提交回复
热议问题