开发者

XSLT and position()

I have

<xsl:for-each select="//article[articletype/@id=15 and position() =1]">

which pulls back nothing. I then change it to

<xsl:f开发者_C百科or-each select="//article[articletype/@id=15]">
<p><xsl:value-of select="position()"/></p>

which pulls back

1 2 3 4

Any reason why this is happening? I've even tried number(position()). It only works if I use a large numbers and not the position I'm expecting

<xsl:for-each select="//article[articletype/@id=15 and position() &lt; 100]">


This question reduces to: what does //article[1] mean? The answer is that it means

/descendant-or-self::node()/child::article[1]

which selects the first "article" child of every node in the document. This is different from (//article)[1], which selects the first article in the document.

So the expression you want is

<xsl:for-each select="(//article[articletype/@id=15])[1]">


With the first XPath you listed: //article[articletype/@id=15 and position() =1], you are asking for the first article element at every axis (in document order) that must have an articletype/@id=15.

For each axis, if the first article element does not have an articletype element with @id=15, then nothing matches.

With the second XPath: //article[articletype/@id=15] you are asking for all of the article elements that have an articletype child that has @id=15. Apparently, there are 4 of them in your document.

You should use the following XPath:

(//article[articletype/@id=15])[1]

Dr. Michael Kay's answer better explains why it behaves this way. Also refer to this answer from Dimitri Novatchev


I think you want the following xpath expression:

<xsl:for-each select="//article[articletype/@id=15][1]">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜