what do you mean by White-space is Preserved in XML
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
optional attributes, minOccurs="0" elements, and nillable="true" elements.
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.
<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.
<tns:area>
<xsl:value-of select=" /ns0:area"/>
</tns:area>
</xsl:if>
Or
<tns:area>
<xsl:value-of select="."/>
</tns:area>
</xsl:if>
</xsl:for-each>
<area> 123 </area>
<area></area> // then tag will not appear
Q) How can I check if a value is null or empty with XSL?
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?
<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:
<ns1:PaymentDate>
<xsl:value-of select="PaymentDate"/>
</ns1:PaymentDate>
</xsl:if>
No comments:
Post a Comment