Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if XML value is nil in XSLT

In an XML document I have some address data..

<zip>08001</zip>
<zipPlus xsi:nil="true" />

and

<zip>08002</zip>
<zipPlus>4512</zipPlus>

On only want to bother displaying the zip plus value if there is a value to use. (for the purposes of this example, I don't care if its a correct zip plus format or not)

Trying to use the following snippet in an XSLT never seems to work right, and I think it has to do with how I am checking for the xsl:nil value

<EmployerZipCode>
      <xsl:value-of select="zip"/>
      <xsl:if test="zipPlus != @xsl:nil">
        <xsl:value-of select="'-'"/>
        <xsl:value-of select="zipPlus"/>
      </xsl:if>
      <xsl:value-of select="$sepChar"/> <!--this is a comma -->
</EmployerZipCode>

The results I get are always

08001,
08002,

not

08001,
08002-4512,

What is the proper way to check for nil-led elements in XSLT? Are there any other ways to get around this problem and get the result I want?

like image 475
StingyJack Avatar asked Jun 09 '11 16:06

StingyJack


People also ask

What does nil mean in XML?

If an element has the xsi:nil attribute specified, it indicates that the element is present but has no value, and therefore no content is associated with it. If an XML schema has defined the nillable attribute as true, it is mapped as a required attribute that takes a Boolean value.

What is XSI nil true in XML?

The xsi:nil="true" attribute is defined in the XML instance document. The xsi:nil="true" attribute indicates that, although the element exists, it has no content. Note that the xsi:nil="true" attribute applies to element values, and not to attribute values.

What is Number () in XSLT?

Specifies the format pattern. Here are some of the characters used in the formatting pattern: 0 (Digit) # (Digit, zero shows as absent)


1 Answers

<xsl:if test="not(zipPlus/@xsi:nil='true')">
like image 57
Jim Garrison Avatar answered Sep 21 '22 23:09

Jim Garrison