Reputation: 23
I want to check if the next parent is test
or foo
using position(.)+1
(that's the idea).
XML:
<text>
<list>
in list
</list>
<table>
I'M HERE
</table>
</text>
<foo>
<hey>
</hey>
</foo>
Can I do something like that ?
XSL :
<xsl:if test="position(.)+1 = 'foo'">
<xsl:value-of select="'Next tag is foo'" />
</xsl:if>
And I want to know if I can put 2 conditions in <xsl:if>
like :
My output is in html.
Edit : wrong question.
Upvotes: 0
Views: 1210
Reputation: 157947
You have changed your question significantly. First you wanted the first child, now the following sibling, will answer both cases, first the explanation of the above terms:
<text>
<table></table> <-- first child (nested one level)
</text>
<foo /> <-- following sibling (same hierarchy level)
Check if the first child of <text>
is <list>
or <table>
The query you are looking for is:
<xsl:if test="name(*[1]) = 'list' or name(*[1]) = 'table'">
Full example:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/text">
<xsl:if test="name(*[1]) = 'list' or name(*[1]) = 'table'">
<xsl:text>first child of text node is list or table</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Check if the following sibling of <text>
is <list>
or <table>
The query you are looking for is:
<xsl:if test="name(following-sibling::*[1]) = 'list' or name(following-sibling::*[1]) = 'table'">
Full example:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/text">
<xsl:if test="name(following-sibling::*[1]) = 'list' or name(following-sibling::*[1]) = 'table'">
<xsl:text>following sibling of text node is list or table</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Reputation: 338128
Almost.
following-sibling
axis to get the next element (following-sibling::*[1]
) [*]self
axis to check if that element itself is a <list>
([self::list]
)e.g. [+]
<xsl:if test="following-sibling::*[1][self::list]">
<xsl:value-of select="'Next tag is list'" />
</xsl:if>
[*] following-sibling::*
selects all following siblings, you want only the first one, hence the [1]
.
[+] Multiple predicates (sub-expressions in square brackets) must be true in succession. This means the XPath expression either selects a non-empty node set (which evaluates to true
in a Boolean test) or it selects the empty node-set (which evaluates to false
). There is no need for an explicit comparison here.
Upvotes: 2