Nillability


The xsi:nil attribute was defined in XML Schema because there were some people from the relational database tradition who felt that omitting an element or attribute from an XML document was not an adequate way of representing the SQL concept of null values. I have to say I find the facility completely unnecessary: Null was invented in SQL so that a cell in a table could hold no data, but XML already has perfectly good ways of representing absence of data, namely an absent element or attribute. But xsi:nil exists, and it can't be uninvented, and you may need to use it if it has been built in to the semantics of the vocabulary for your source or result documents.

As with xsi:type, the xsi:nil attribute is mentioned in the XSLT specification only in notes and examples. However, it gets a rather more detailed treatment in the XPath formal semantics, because it has a significant effect in complicating the rules for type matching.

You can use xsi:nil on an element only if the schema defines the element as nillable. If you do set «xsi:nil=" true " » on an element, then the element is valid only if it is empty; moreover, it is allowed to be empty in this case even if the content model for the element would otherwise not permit it to be empty.

The possibility of encountering an xsi:nil potentially plays havoc with the type safety of your stylesheet. If you write a function that is designed to process valid book elements, and every book must have an ISBN, then the function should be allowed to access the ISBN without adding conditional logic to check that it is there. For this reason, a function or variable that accepts nilled elements has to declare that it does so. If a function parameter is declared with the type «as=" element (*, book-type) " », then passing the element <book xsi:nil=" true" /> to this function will cause a type error. If you want to write a function that accepts this element, you must instead write «as="element (*, book-type?) " » to show that your function can handle this input.

Apart from this, xsi:ni1 behaves in XSLT just like any other attribute.

You do need to be a little careful if you want to put your stylesheets through a schema processor (which you might do, for example, if you store your stylesheets in an XML database). The schema processor attaches a special meaning to attributes such as xsi:nil, xsi:type, and xsi:schemaLocation, even though XSLT does not. It's therefore best to avoid using these attributes directly on literal result elements. Two possible ways round this problem are:

  • Generating these attributes using the <xsl:attribute> instruction instead.

  • Using a namespace alias for the xsi namespace: See the description of the <xsl: namespace-alias > declaration in Chapter 5 (page 350).




XSLT 2.0 Programmer's Reference
NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)
ISBN: 764569090
EAN: 2147483647
Year: 2003
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net