Reputation: 138
I have input xml roughly in below format.
<Root>
<PAF>
<Child1 xsi:nil="true" />
<Child2 xsi:nil="true" />
<Child3>BlahBlah</Child3>
</PAF>
</Root>
While transforming it into XML, I wanted to check
if <PAF>
has any child having value (in my case it is <Child3>
)
then do something.
If all child have nil="true"
then do something
I am bit new for XSLT scripting, So far I could get only count of child node of <PAF>
.
Can some one please suggest me the if-else syntax in my context?
Do I need any XPATH expression here?
Thanks for your time.
Upvotes: 0
Views: 2660
Reputation: 1816
You need to do below code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="www.nill.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="PAF">
<xsl:choose>
<!--Checked no data inside PAF and its descendants and not other attributes other than xsi:nill then Drop.-->
<xsl:when test="(count(descendant-or-self::*/@*[not(name() = 'xsi:nil')]) = 0) and (not(normalize-space()))"/>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Reputation: 117165
Assuming you are in the context of PAX
, you could so something like:
<xsl:choose>
<xsl:when test="*[not(@xsi:nil='true')]">
<!-- not all chiLd nodes are empty -->
<!-- DO SOMETHING HERE -->
</xsl:when>
<xsl:otherwise>
<!-- all child nodes are empty -->
<!-- DO SOMETHING ELSE -->
</xsl:otherwise>
</xsl:choose>
If there is no DO SOMETHING ELSE, then use xsl:if
instead of xsl:choose
.
Note that the xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
declaration must be present in both your XML and XSLT documents.
Upvotes: 0