Reputation: 17893
checking for an Null element
<image><a><img src="abcd"/></a></image>
XSLT template:
<xsl:if test="image!=''">
IMAGE HAS TEXT OR INNER ELEMENT
</xsl:if>
I am getting blank output though the "image" element has child elements. and Ideally it is not null.
I have to check the condition that it should have value or the child elements. The child elements can be empty.
How to rectify this.
Thank you
Upvotes: 3
Views: 1840
Reputation: 107317
You can use this to check for any child nodes (text, elements etc)
<xsl:template match="image">
<xsl:if test="node()">
IMAGE HAS TEXT OR INNER ELEMENT
</xsl:if>
</xsl:template>
Or you can be more specific:
<xsl:template match="image">
<xsl:if test="a | text()">
IMAGE HAS TEXT OR INNER ELEMENT
</xsl:if>
</xsl:template>
Upvotes: 0
Reputation: 243529
Use:
image[string() or node()]
This evaluates to true()
only if there is at least one image
child of the current node, such that its string value is non-empty, or it has children (or both).
This can be simplified just to:
image[node()]
taking into account that in order to have string value, an element must have a text node descendant in its sub-tree.
If you want the string value of image
(if any) to be not all-whitespace, modify the first of the above XPath expressions to:
image[normalize-space() or node()]
Upvotes: 5
Reputation: 167706
Use <xsl:if test="image/node()">...</xsl:if>
to check whether the image
element has any kind of child node or <xsl:if test="image/*">...</xsl:if>
to test whether the image
element has at least one child element.
Upvotes: 1