I came across http://xpather.com, which lets you write XPath expressions. It comes with a sample XML document which is similar to this one:
<app>
<abstract>a</abstract>
<description>
<subject>s1</subject>
<subject>s2</subject>
</description>
<extra-notes>
<note>n1</note>
<note>n2</note>
<note>n3</note>
<note>n4</note>
</extra-notes>
</app>
and has a sample XPath expression: .//*[self::abstract or self::subject or self::note][position() <= 2]
The result sequence is
<abstract>a</abstract>
<subject>s1</subject>
<subject>s2</subject>
<note>n1</note>
<note>n2</note>
Now I am trying to understand why the result is like this: The first predicate selects all abstract, subject and note elements and the second predicate limits this to position() <= 2
. My intuitive expectation would be that the XPath expression only selects two nodes (abstract and subject1), but it actually selects the first two elements from each of the selected nodes. What is exactly going on?
Just for testing: here is my simple stylesheet:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output indent="yes"/>
<xsl:template match="/">
<e>
<xsl:copy-of select=".//*[self::abstract or self::subject or self::note][position() <= 2]"/>
</e>
</xsl:template>
</xsl:stylesheet>