Using nilled in XPath

Even though it was introduced in 2007, it ain’t supported by Apache Xalan yet, but only by Saxon-SA.


<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:xsi="" xsi:noNamespaceSchemaLocation="Schema.xsd">
  <Element xsi:nil="true"/>


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="">
  <xs:element name="Root">
        <xs:element name="Element" nillable="true">


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="">
  <xsl:template match="/Root">
          <xsl:when test="nilled(Element)">0</xsl:when>


import net.sf.saxon.FeatureKeys;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;

import org.xml.sax.InputSource;

import static java.lang.System.out;

public final class SaxonSASample2 {

  public static void main(final String[] args) throws Exception {
    final TransformerFactory factory = TransformerFactory.newInstance(); // ensure javax.xml.transform.TransformerFactory system property is set to com.saxonica.SchemaAwareTransformerFactory

    factory.setAttribute(FeatureKeys.SCHEMA_VALIDATION_MODE, "strict");

    final Transformer transformer = factory.newTransformer(new StreamSource(new FileReader("Stylesheet.xsl")));

    transformer.transform(new SAXSource(new InputSource(new FileReader("Document.xml"))), new StreamResult(out));


It doesn’t work with a DOM source because the type annotations of all elements is stripped to xs:untyped by Saxon-SA, whether they have been validated or not.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s