home contribute faq download

FunctX XSLT Functions

fn:nilled

Whether an element is nilled (has xsi:nil="true")

Google
Webxsltfunctions.com

Description

The fn:nilled function determines whether an element is nilled, in the sense of W3C XML Schema. In a schema, element declarations can designate elements as nillable. This allows them to appear in an instance document empty, even if their type would otherwise require them to have some content (either character data or children or both).

An element is not considered to be nilled just because it is empty. For an element to be nilled, it must have an attribute xsi:nil whose value is true. Nilled elements are always empty; it is not valid for an element to have content and also have the xsi:nil attribute set to true.

On the other hand, some elements may be validly empty, but not be nilled. This may occur if an element has a complex type that specifies all optional children, or a simple type that allows blank values, such as xs:string. To test for an empty (but not necessarily nilled) element, you can use the expression string($node) = "".

It is useful to be able to check for a nilled element using the fn:nilled function to avoid unexpected results. For example, suppose you want to subtract the value of a discount element from the value of a price element. If the discount element is nilled, its typed value will be the empty sequence, and the result of the expression price - discount will be the empty sequence. You can avoid this using the expression price - (if nilled(discount) then 0 else discount).

This description is © Copyright 2007, Priscilla Walmsley. It is excerpted from the book XQuery by Priscilla Walmsley, O'Reilly, 2007. For a complete explanation of this function, please refer to Appendix A of the book.

Arguments and Return Type

NameType
$arg node()?
return value xs:boolean?

Examples

<xsl:variable name="in-xml" as="item()*">
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <child>12</child>
  <child xsi:nil="true"></child>
  <child></child>
  <child/>
  <child xsi:nil="false"></child>
</root>
</xsl:variable>
XPath ExampleResults
These examples assume that the input has been schema validated.
nilled($in-xml//child[1])
false
nilled($in-xml//child[2])
true
nilled($in-xml//child[3])
false
nilled($in-xml//child[4])
false
nilled($in-xml//child[5])
false

See Also

fn:emptyWhether a value is the empty sequence
fn:existsWhether an argument is the empty sequence

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26W3C, XQuery 1.0 and XPath 2.0 Functions and Operators, http://www.w3.org/TR/xpath-functions/
Datypic XSLT Services

Recommended Reading:

XQuery