Wednesday, February 18, 2026

WhiteSpace | Null Value

 

what do you mean by White-space is Preserved in XML

In XML, the term "white-space is preserved" means that any white-space characters (such as spaces, tabs, line breaks, and carriage returns) within the content of an element are treated as significant and are not automatically removed or collapsed by an XML parser. Instead, these white-space characters are maintained in their original form, exactly as they appear in the XML document.

HTML truncates multiple white-space characters to one single white-space:

HTML:

Hello           Tove

Output:

Hello Tove

With XML, the white-space in a document is not truncated.

XMLtruncates multiple white-space characters to one single white-space:

HTML:

Hello           Tove

Output:

Hello           Tove

With XML, the white-space in a document is not truncated.

White-space preservation can be controlled in XML documents using various techniques, including:

XML Schema Definitions: You can specify whether white-space should be preserved or not by defining constraints in an XML Schema.

CDATA Sections: Using CDATA (Character Data) sections, you can indicate that the content within the CDATA section should be treated as raw text, and white-space should be preserved.

Whitespace-Preserve Attribute: Some XML technologies, like XSLT, provide attributes or functions to explicitly preserve white-space when transforming or processing XML data.

How to represent null in XML Schema

For attributes:

•use the attribute use="optional"

For elements:

•use the attribute nillable="true"        or

•use the attribute minOccurs="0"

Summary

There are three ways of representing null fields in XML schema:
 optional attributesminOccurs="0" elements, and nillable="true" elements.
There are cases when you would use each one of these: an optional attribute if you have a simple, nullable type; a minOccurs="0" element if you have a complex nullable type and you want it to take up the least amount of space;
nillable="true" element if a null value must have a placeholder (for instance, when it appears in an array).

Null elements

There are two ways to represent a null value with elements: with either the attribute nillable="true", or with minOccurs="0".

Schema for TypeWithNullElements

<complexType name="TypeWithNullElements">

  <sequence>

    <element name="nillableElem" nillable="true" type="int"/>

    <element name="minOccursElem" minOccurs="0" type="int"/>

  </sequence>

</complexType>

Instances of TypeWithNullElements

Elements with values:

<typeWithNullElements>

  <nillableElem>5</nillableElem>

  <minOccursElem>5</minOccursElem>

</typeWithNullElements>

Elements with null values:

<typeWithNullElements>

  <nillableElem xsi:nil="true"/>

</typeWithNullElements>


When nillable="true" is useful

minOccursElem is so obviously better than nillableElem,

 

 why would you ever want to use nillable Elem?

 a null value for nillableElem has a placeholder for the value.

Where might you need a placeholder?

One example would be an array where each array entry could potentially be null. Imagine an array of four elements, for example, whose values are {0, null, 1, null}. How would you represent that array using an instance of a minOccursElem element? Answer: you cannot.

 There would be no way to distinguish between the four element array described above and a two element array whose values are {0, 1}. With minOccurs="0" elements, there are no placeholders for the null elements. So you must use nillable="true"

 a schema for such an array and the XML instance for {0, null, 1, null}.

 

 

 

 

<complexType name="nullableElementArray">

<sequence>

<element name="elem" type="int" maxOccurs="4" nillable="true"/>

</sequence>

</complexType>

Listing 8. XML instance of nillable array elements

 

 

 

 

 

 

<nullableElementArray>

<elem>1</elem>

<elem xsi:nil="true"/>

<elem>2</elem>

<elem xsi:nil="true"/>

</nullableElement>


Q)  what is nillable?

In an XML schema you can mark an element as nillable meaning it can take an explicit NULL value. See nillable and minOccurs XSD element attributes for a great explanation.


Q) What is XSI nil true?
<FirstName i:nil="true" /> 
This means FirstName is null 
This means FirstName = ""
<FirstName />
<FirstName></FirstName> 
FirstName field can have a NULL or nil value, meaning empty, or unknown.


From Empty Element: 

To test if the value of a certain node is empty

It depends on what you mean by empty.

·         Contains no child nodes: not(node())

·         Contains no text content: not(string(.))

·         Contains no text other than whitespace: not(normalize-space(.))

·         Contains nothing except comments: not(node()[not(self::comment())])


if you're value is not equal to this empty string:

 

 < xsl:if test="value != $empty_string" >

 

To check if the value is numeric, you can use this:

 

 < xsl:if test="value = not (string(.))" >


If you use xslt variables it's important to check for null values

 

First create an empty string:

 < xsl:variable name="empty_string"/ >

 

Use text() function to get text

The <xsl:text> element is used to write literal text to the output. 

<xsl:text> </xsl:text> is the right way of producing pure-white-space nodes 

The main reason for enclosing text within an xsl:text element is to allow whitespace to be output. Whitespace nodes in the stylesheet are ignored unless they appear immediately within an xsl:text element. 

<xsl:if test="/ns0:area/text()"> 
        <tns:area>
          <xsl:value-of select=" /ns0:area"/>
        </tns:area>
   </xsl:if>

Or 

<xsl:for-each select="/ns0:area">
        <xsl:if test="./text()">
          <tns:area>
            <xsl:value-of select="."/>
          </tns:area>
        </xsl:if>
</xsl:for-each>        
 
Ex: <area>   </area>
       <area>   123   </area>
        <area></area>  // then tag will not appear


Q) How can I check if a value is null or empty with XSL? 

For example, if categoryName is empty? I'm using a when choosing construct. 
<xsl:choose>
    <xsl:when test="categoryName !=null">
        <xsl:value-of select="categoryName " />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="other" />
    </xsl:otherwise>
</xsl:choose>

 Q. If these fields are optional in the source and target?

If these fields are optional in the source and target, it is a good practice to insert an xsl:if statement around these mappings to test for the existence of the source node.

If this is not done, and the source node does not exist in the input document, an empty node is created in the target document. For example, if Product Name is optional in both the source and target, map them as follows:
 

 

<xsl:if test="ProductName">
    <ProductName>
      <xsl:value-of select="ProductName"/>
    </ProductName>
</xsl:if>
Or
<xsl:if test="/ns0:current_operator_id/text()">
          <tns:current_operator_id>
              <xsl:value-of select=" /ns0:current_operator_id"/>
          </tns:current_operator_id>
  </xsl:if>

If data is their then map it or if it is empty then don’t show tag itself:

<xsl:if  test="PaymentDate!=''">           
     <ns1:PaymentDate>
<xsl:value-of select="PaymentDate"/>
      </ns1:PaymentDate>
</xsl:if>

No comments:

Post a Comment

SOA Overview Part-1

  Middleware It provides a mechanism for the process to interact with other processes running on multiple network machines. Advantages...