Question about XPath with functions in it

this is the next question about my previous topic:

Please help me sort this XPath

I have XPath like:

<xsl:value-of select="position()+count(preceding-sibling::*)-18"/>

Currently, I can only understand parts of it, such as position (). In addition, I know that the previous brother should select all the siblings before the current node, but I do not know what this statement means when they are combined, as indicated above.

Can anyone help in understanding this XPath? thanks in advance.

+3
source share
6 answers

All answers except @Alejandro have the same common error :

It is not true that :

preceding-sibling::*

selects all previous nodes.

.

, :

preceding-sibling::node()

XPath:

  • node ( /), document-node() XPath 2.0

  • . <a/>

  • . <a> Hello </a> node a "Hello"

  • . <!-- This is a comment-->

  • . <?someName I am a PI ?>

  • . <a x="1"/> x a.

  • . <a xmlns:my="my:namespace"/> a node "my: namespace" name (prefix) my

5 preceding-sibling:::

preceding-sibling::node()

1-5.

preceding-sibling::*

,

preceding-sibling::someName

"someName"

preceding-sibling::text()

, ( )

preceding-sibling::comment()

node .

preceding-sibling::processing-instruction()

, PIs

preceding-sibling::processing-instruction('someName')

, PI "someName".

+2

, ( ) ( node).

. , :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="list">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*">
        <xsl:copy>
            <xsl:value-of select="concat(position(),' + ',
                                         count(preceding-sibling::*),' = ',
                                         position() +
                                         count(preceding-sibling::*))"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

<list>
    <a/>
    <b/>
    <a/>
    <b/>
</list>

:

<list>
    <a>1 + 0 = 1</a>
    <b>2 + 1 = 3</b>
    <a>3 + 2 = 5</a>
    <b>4 + 3 = 7</b>
</list>

, match="a":

<list>
    <a>1 + 0 = 1</a>
    <a>3 + 2 = 5</a>
</list>

, node

, position() ? match="a[position()=2]":

<list>
    <a>3 + 2 = 5</a>
</list>

? . XPath position() node . : child::a[position()=2] a .

, position() , position() .

, node ? , apply-templates for-each .

apply-templates select, select="a":

<list>
    <a>2 + 2 = 4</a>
</list>
+5

position() node node -set. , <foo>:

<xml>
  <foo /><foo /><foo /><foo />
</xml>

<xsl:apply-templates>:

<xsl:template match="/xml">
  <!-- this selects four nodes -->
  <xsl:apply-templates select="foo" />
</xsl:template>

<!-- this runs four times -->
<xsl:template match="foo">
  <xsl:value-of select="position()" />
</xsl:template>

"1234".

count() node -set.

preceding-sibling::* preceding-sibling, node ( node , ).

<xsl:value-of select="position()+count(preceding-sibling::*)-18"/>

. XSLT, , , , - " node". node - XSLT-. node, node, XSLT/XPath node.

+2

preceding-sibling:: - , . * . count() . , , , node, .

+1

position() , 1 . n.

preceding-sibling::* - , - , (, , n-1 , ).

. -18 :), + - 18. , , , , .

0

, :

node, , 18.

Generally speaking, various operators can be combined into expressions, as you demonstrate in your example.

Note. Use position()with caution because sometimes the current node list is not easy to see.
0
source

Source: https://habr.com/ru/post/1778274/


All Articles