user2617275
user2617275

Reputation:

Unable to understand the XSLT code

I have code something like below in one of my XSLT -

<div>
<ul class="toplevel-Video group Video-coll-left">
<xsl:apply-templates select="//VideoNode[@Video='Yes'][1]" />
</ul>
<ul class="toplevel-Video group Video-coll">
<xsl:apply-templates select="//VideoNode[@Video='Yes'][position()>=2 and last()>position()]" />
</ul>
<ul class="toplevel-Video group Video-coll-right">
<xsl:apply-templates select="//VideoNode[@Video='Yes'][last()]" />
</ul>
</div>

and unable to understand that what it mean to say [1] in the below code

<xsl:apply-templates select="//VideoNode[@Video='Yes'][1]" />

Thanks

Upvotes: 1

Views: 56

Answers (3)

Michael Kay
Michael Kay

Reputation: 163585

There is a difference between //X[1] and (//X)[1]. The first expression selects every X that is the first X child of its parent; the second selects the first X in the document. In effect, the "[]" operator has higher precedence than the "//" operator. More formally, //X[1] expands to root()/(descendant-or-self::node())/(child::X[1]), while (//X)[1] expands to (root()/descendant-or-self::node()/child::X)[1].

Upvotes: 2

Daren Thomas
Daren Thomas

Reputation: 70344

The select expression is an XPATH expression that says:

  • starting from root or anything else (//)
  • find tags names VideoNode
  • but only those having the attribute Vidoe with a value of Yes
  • return the first such node [1]

Now, the [1] part selects the first matching node of a parent, so if you have multiple parents for your VideoNode nodes, then the result should be multiple such nodes...

e.g.:

<root>
    <parent1>
        <VideoNode Video='Yes'/> <!-- this gets selected -->
        <VideoNode Video='Yes'/>
        <VideoNode Video='Yes'/>
        <VideoNode Video='Yes'/>
        <VideoNode Video='Yes'/>
    </parent1>
    <parent2>
        <VideoNode Video='Yes'/> <!-- and so does this! -->
        <VideoNode Video='Yes'/>
        <VideoNode Video='Yes'/>
    </parent2>
</root>

So the [1] is local, not global.

Upvotes: 1

Parker
Parker

Reputation: 1102

It just means the first node in the set. So the first ul contains the first elements, the middle ul contains all nodes except the first and last, and the last ul contains the last node.

Upvotes: 1

Related Questions