IBM Support

Using XSLT, how to check whether an element has a value or has the attribute xsi:nil = ‘true’?

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"&gt;
<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"&gt;
<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&quot;
xmlns:saxon="http://saxon.sf.net/&quot; xmlns:xml="http://www.w3.org/XML/1998/namespace&quot;
xmlns:xsd="http://www.w3.org/2001/XMLSchema&quot;
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

<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>

[{"Product":{"code":"SSGR73","label":"IBM Cast Iron Cloud Integration"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Not Applicable","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"All Versions","Edition":"","Line of Business":{"code":"LOB45","label":"Automation"}}]

Historical Number

00001203

Product Synonym

Cast Iron Solution
Cast Iron Operating System
Cast Iron Studio

Document Information

More support for:
IBM Cast Iron Cloud Integration

Software version:
All Versions

Document number:
410625

Modified date:
15 June 2018

UID

swg21452871

Manage My Notification Subscriptions