Troubleshooting
Problem
Using XSLT, how to check whether an element has a value or has the attribute xsi:nil = ‘true’?
Resolving The Problem
To check whether an element has a value or has the attribute xsi:nil = ‘true’, the condition should look like the following:
xsl:if test="not (element/@xsi:nil = true())”
For example, if the XML document is as below and we want to check whether the element ORDER_DATE has a value or has an empty attribute, we need to use the below XSLT:
Sample XML document:
<rows>
<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ORDER_DATE>2004-11-18T00:00:00.000</ORDER_DATE>
<SO_ID>020942 </SO_ID>
<SO_LINE_NO>7.00</SO_LINE_NO>
<SO_TYPE>NS</SO_TYPE>
<row>
<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ORDER_DATE xsi:nil=”true”></ORDER_DATE>
<SO_ID>020942 </SO_ID>
<SO_LINE_NO>7.00</SO_LINE_NO>
<SO_TYPE>NS</SO_TYPE>
<row>
</rows>
Sample XSLT:
<xsl:stylesheet exclude-result-prefixes="exslt saxon bpws cis ihmap" version="2.0"
xmlns:exslt="http://exslt.org/common"
xmlns:saxon="http://saxon.sf.net/" xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="yes" method="xml"/>
<xsl:template match="/">
<xsl:element name="Record">
<xsl:for-each select="/rows/row">
<xsl:if test="count(.) > 0">
<xsl:element name="Rows">
<xsl:if test="not(ORDER_DATE/@xsi:nil=true())">
<xsl:element name="OrderDate">
<xsl:value-of select="ORDER_DATE"/>
</xsl:element>
</xsl:if>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Historical Number
00001203
Product Synonym
Cast Iron Solution
Cast Iron Operating System
Cast Iron Studio
Was this topic helpful?
Document Information
More support for:
IBM Cast Iron Cloud Integration
Software version:
All Versions
Document number:
410625
Modified date:
15 June 2018
UID
swg21452871