I have an application which extracts data from an XML file using XPath. If a node in that XML source file is missing I want to return the value ‘N/A’ (much like the Oracle NVL function). The trick is that the application doesn’t support XSLT; I’d like to do this using XPath and XPath alone.
Is that possible?
It can be done but only if the return value when the node does exist is the string value of the node, not the node itself. The XPath
will return the string value of the
bazelement if it exists, otherwise the string ‘N/A’.To generalize the approach:
where
$null-valueis the null value string and$nodethe expression to select the node. Note that if$nodeevaluates to a node-set that contains more than one node, the string value of the first node is used.