I am editing an XSLT template and my skills are a little rusty.
I would like to write a condition to see if the current node is in the first three child nodes of its parent.
<parent>
<child>
<child>
<child>
<child>
</parent>
So the first three child elements above would return true, but the fourth would return false, to complicate matters the child elements will not all be the same and will have descendants of their own. I am sure there is some simple xpath that will do it.
It depends on the situation. If you are in the middle of
<xsl:apply-templates select="/parent/child" />
Then checking with
<xsl:if test="position() < 4">
will do. If you are in some other context, one that does not affect all <child>
elements, then position()
will refer to the position within that context.
If you want a context-free check, you can use:
<xsl:if test="count(preceding-sibling::child) < 3">
<!-- or -->
<xsl:if test="count(preceding-sibling::*) < 3">
To select only the first three <child>
elements, this would be it:
/parent/child[position() < 4]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With