3

I have the following xml:

<content> <p>para 1<an>test 1</an></p> <p>para 2<an>test 2</an></p> <p>para 3<an>test 3</an></p> </content> 

and I have the following expression in xsl:

<xsl:template match="/"> <xsl:text>Count: </xsl:text> <xsl:value-of select="count(/content//an)" /> <xsl:text> Content: </xsl:text> <xsl:value-of select="/content//an[2]" /> </xsl:template> 

Why does /content//an[1] returns "test 1" and /content//an[2] return ""? The count says that there are 3. I'm using libxslt.

Thanks!

2

1 Answer 1

6

This is a FAQ.

One must be careful when using the // abbreviation.

//SomeName[1] 

means: select all SomeName elements in the document that are the first SomeName children of their parent. Often the selected nodes are more than one (or even all the nodes).

The correct way to select only the $k-th SomeName element in the document is:

(//SomeName)[$k] 

In your case, use:

(/content//an)[2]

Remember: The [] operator has a higher precedence (binds stronger) than the // abbreviation.

Sign up to request clarification or add additional context in comments.

1 Comment

@Flack: I wish this were possible :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.