Saturday, January 9, 2010

Xerces-J: more XSD 1.1 tests; negative wild-cards

I'm pretty satisfied with the XSD 1.1 assertions and CTA (type alternatives) implementation (as I've been writing few posts about them, earlier on this blog), in current Xerces-J SVN code base (though, a user feedback would be great, for the Xerces project. Instructions to report bugs in Xerces-J can be found at, http://xerces.apache.org/xerces2-j/jira.html).

I'm now beginning to test some of other XSD 1.1 features. To start with these new set of posts, following are few use cases for "Negative wildcards" (ref, http://www.ibm.com/developerworks/xml/library/x-xml11pt3/index.html#N101C9), which I've found to be working fine with Xerces-J.

XSD 1.0 had following XML representation of, xs:any wild-card Schema component:
  <any
    id = ID
    maxOccurs = (nonNegativeInteger | unbounded)  : 1
    minOccurs = nonNegativeInteger : 1
    namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) )  : ##any
    processContents = (lax | skip | strict) : strict
    {any attributes with non-schema namespace . . .}>
      Content: (annotation?)
  </any>

("anyAttribute" is another wild-card Schema component)

XSD 1.1 enhances the xs:any wild-card definition to following:
  <any
    id = ID
    maxOccurs = (nonNegativeInteger | unbounded)  : 1
    minOccurs = nonNegativeInteger : 1
    namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) ) 
    notNamespace = List of (anyURI | (##targetNamespace | ##local)) 
    notQName = List of (QName | (##defined | ##definedSibling)) 
    processContents = (lax | skip | strict) : strict
    {any attributes with non-schema namespace . . .}>
      Content: (annotation?)
  </any>

As we could notice, xs:any now allows (in XSD 1.1, which was not available in XSD 1.0) two additional specifiers in it's definition, namely "notNamespace" and "notQName".

Here's a fictitious example of usage of "notNamespace" specifier:

XML document, [1]:
  <Example xmlns="http://www.example.com/mySample">
    <a>hi there</a>
    <b>hi there ..</b>
    <c>hi there ...</c>
    <d xmlns="http://www.notallowed.com/sorry">hi there ....</d>
  </Example>

XSD 1.1 Schema, [2]:
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
             targetNamespace="http://www.example.com/mySample"
             elementFormDefault="qualified">

    <xs:element name="Example">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="a" type="xs:string" />
          <xs:element name="b" type="xs:string" />
          <xs:element name="c" type="xs:string" />
          <xs:any notNamespace="http://www.notallowed.com/sorry"
                  processContents="lax"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  
  </xs:schema>

The XSD schema, [2] defines an xs:any wild-card definition, which doesn't allow an XML instance document to have an element instance (allowed by the wild-card) to be in the namespace, "http://www.notallowed.com/sorry".

Therefore when the XML instance, [1] is validated by XSD document, [2] we get following error while performing validation with Xerces-J XSD 1.1 schema engine:
test.xml:5:46:cvc-complex-type.2.4.a: Invalid content was found starting with element 'd'. One of '{WC[##other:"http://www.notallowed.com/sorry"]}' is expected.

If we change the instance document, to specify element "d" to following:
<d>hi there ....</d>
or say,
<d xmlns="http://www.allowed.com">hi there ....</d>

the XSD validation, succeeds (as element "d" is now not in the namespace, "http://www.notallowed.com/sorry").

Here's an example for usage of "notQName" specifier:

XML document, [3]:
  <Example xmlns="http://www.example.com/mySample">
    <a>hi there</a>
    <b>hi there ..</b>
    <c>hi there ...</c>
    <XX>hi there ....</XX>
  </Example>

XSD 1.1 Schema, [4]:
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
             xmlns:tns="http://www.example.com/mySample"
             targetNamespace="http://www.example.com/mySample"
             elementFormDefault="qualified">

    <xs:element name="Example">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="a" type="xs:string" />
          <xs:element name="b" type="xs:string" />
          <xs:element name="c" type="xs:string" />
          <xs:any notQName="tns:XX"
                  processContents="lax"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  
  </xs:schema>

The XSD schema, [4] doesn't allow an instance document to have, an element "XX" (in namespace, "http://www.example.com/mySample"), where the xs:any wild-card allows an element content.

Therefore, when XML document [3] is validated by XSD schema, [4] we get following error message, with Xerces-J:
test.xml:5:7:cvc-complex-type.2.4.a: Invalid content was found starting with element 'XX'. One of '{WC[##any, notQName(tns:XX)]}' is expected.

So if we, replace the offending element, with:
<abc>hi there ....</abc>
or say,
<XX xmlns="http://www.allowed.com">hi there ....</XX>
(here, the local-name in XML instance document, is same as that specified in the "notQName" specifier, while the namespace of element instance is different, than specified by "notQName", which makes this element instance, valid)

the XML validation passes.

All these XSD language enhancements, in 1.1 version look cool (and, useful :)) to me, and they give some more XML validation capabilities to XSD schema, authors.

Out of my curiosity, I was thinking if we could write few of new XSD 1.1 wild-card capabilities, with assertions.

For e.g, some of the features of "notNamespace" attribute can be written with an assertion like following:
  <xs:assert test="not(namespace-uri(*[last()]) = (
                        'http://www.notallowed.com/sorry1',
                        'http://www.notallowed.com/sorry2',
                        'http://www.notallowed.com/sorry3')
                       )" />

But using assertions for this need, might have following disadvantages, or limitations [5]:
1. The XSD 1.1 engine, has to build a XPath tree to evaluate an assertion, which is a memory overhead.
2. It looks like, that by using assertions, we cannot implement following facilities of "notNamespace" attribute: we cannot specify namespace URIs with keywords, ##targetNamespace & ##local.
3. Using the, "notNamespace" attribute on xs:any wildcard, gives us optimization benefits of xs:any implementation (like, this doesn't have to build a XPath tree, which an assertion approach requires). Moreover, it's better to use a native facility of a construct (like, xs:any), which keeps the XSD schema's design more natural, and easy to understand.

And also, some of the features of "notQName" attribute can be written with an assertion like following:
  <xs:assert test="not(local-name(*[last()]) eq 'XX'
                       and
                      namespace-uri(*[last()]) eq 'http://www.example.com/mySample')" />

This approach would have similar issues, as specified above [5].

I hope, that this post was useful.

Friday, January 1, 2010

XSD 1.1: few more assertions use cases; assertion inheritance

I'm continuing through, with writing uses cases for XSD 1.1 assertions, and testing them with Xerces-J. Here's the next ones in this series:

The uses cases below, demonstrate XSD assertions usage in a XSD schema type hierarchy.

Example 1

XML document [1]:
  <Example x_count="3">
    <x a="val1">2</x>
    <x a="val2">4</x>
    <x a="val3">6</x>
  </Example>

XSD 1.1 document [2]:
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
    <xs:element name="Example">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="x" type="x_Type" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="x_count" type="xs:nonNegativeInteger" use="required" />
        <xs:assert test="@x_count eq count(./x)" />
        <xs:assert test="every $x in x[position() lt last()] satisfies
                    number($x/@a/substring-after(.,'val')) lt
      number($x/following-sibling::x[1]/@a/substring-after(.,'val'))" />
      </xs:complexType>
    </xs:element>
  
    <xs:complexType name="x_Type">
      <xs:simpleContent>
        <xs:extension base="myInteger">
          <xs:attribute name="a" type="attrType" use="required" />
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  
    <xs:simpleType name="myInteger">
      <xs:restriction base="xs:positiveInteger">
        <xs:assertion test="$value mod 2 = 0" />
      </xs:restriction>
    </xs:simpleType>
  
    <xs:simpleType name="attrType">
      <xs:restriction base="xs:string">
         <xs:pattern value="val[1-9][0-9]*" />
         <xs:maxLength value="20" />
      </xs:restriction>
    </xs:simpleType>
  
  </xs:schema>

The purpose of the XML document [1], and the corresponding XSD schema [2] would be quite self-explanatory I believe.

I'll try to explain below, what the assertions in above XSD schema [2], are intended to accomplish:
a) The assertion on complex type (an anonymous type) of element, "Example" is an usual assertion on a XSD complex type, as I've explained in few earlier posts in this series. For the interest of readers, this assertion is ensuring that, value of attribute "a" of element(s) "x" have a suffix integer value, to string "val" is specified in a numerically ascending order.
b) The schema type, of element "x" is "x_Type". x_Type is a complex type (because, it specifies an attribute), and has simple content. The simple content definition of, element "x" is defined by the simple type, "myInteger". The type, myInteger specifies an assertion facet (which tests, that the integer value content, of element "x" is even). This demonstrates, that a schema type (x_Type here) inherits assertions from it's base types (the assertions, all the way up in type hierarchy are inherited -- if any of the assertions, in some of ancestor XSD types are specified).

Example 2

XML document [3]:
  <Example>
    <x a="val1">2</x>
    <x a="val2">4</x>
    <x a="val3">6</x>
  </Example>

XSD 1.1 document [4]:
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
    <xs:element name="Example">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="x" type="x_Type" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:assert test="every $x in x[position() lt last()] satisfies
                       number($x/@a/substring-after(.,'val')) lt
         number($x/following-sibling::x[1]/@a/substring-after(.,'val'))" />
      </xs:complexType>
    </xs:element>
  
    <xs:complexType name="x_Type">
      <xs:simpleContent>
        <xs:restriction base="x_base">
          <xs:assert test="$value lt 100" /> 
        </xs:restriction>
      </xs:simpleContent>
    </xs:complexType>
  
    <xs:complexType name="x_base">
      <xs:simpleContent>
        <xs:extension base="myInteger">
          <xs:attribute name="a" type="attrType" use="required" />
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  
    <xs:simpleType name="myInteger">
      <xs:restriction base="xs:positiveInteger">
        <xs:assertion test="$value mod 2 = 0" />
      </xs:restriction>
    </xs:simpleType>
  
    <xs:simpleType name="attrType">
      <xs:restriction base="xs:string">
        <xs:pattern value="val[1-9][0-9]*" />
        <xs:maxLength value="20" />
      </xs:restriction>
    </xs:simpleType>
  
  </xs:schema>

Here's how the assertions in XSD schema [4], work on XML document, [3]:
There's nothing too complicated about the assertion rules here. The assertions on the complex type, "x_Type" consists of assertions within this type, and the assertions inherited from the base type.

The XSD examples, [2] and [4] look quite similar. The difference between XSD examples, [2] and [4] is that, the type "x_Type" in example [2] inherits a XSD simple type, while the type "x_Type" in example, [4] inherits a XSD complex type. The element, "Example" in XML document, [3] doesn't have an attribute, "x_count" (this is a cosmetic difference, between the two examples).

Both of the above, XSD examples demonstrate assertions inheritance from base XSD types (one of the examples demonstrates inheriting assertions from a simple type, while the other example demonstrates inheriting assertions, from a complex type).

I hope, that this post was useful.

Happy New Year, 2010

As the new year dawns, here's a new year wish, to readers of this blog:


Sunday, December 13, 2009

XSD 1.1: few more assertions and CTA use cases

In my quest to test Xerces-J's XSD 1.1 implementation, I've come up with another example, using XSD 1.1 assertions and CTA (type alternatives) which I'll like to share here.

Here's a fictitious use-case and some discussions and analysis of the XSD technical options, for solving this use-case, later on in this post.

XML document [1]:
  <shapes>
    <polygon kind="square">
      <a>10</a>  
      <b>10</b>
      <c>10</c>
      <d>10</d>
    </polygon>
    <polygon kind="rectangle">
      <a>10</a>  
      <b>8</b>
      <c>10</c>
      <d>8</d>
    </polygon>
    <polygon kind="triangle">
      <a>5</a>  
      <b>10</b>
      <c>15</c>
    </polygon>
  </shapes>

XML document [2]:
  <shapes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <polygon kind="square" xsi:type="Quadrilateral">
      <a>10</a>  
      <b>10</b>
      <c>10</c>
      <d>10</d>
    </polygon>
    <polygon kind="rectangle" xsi:type="Quadrilateral">
      <a>10</a>  
      <b>8</b>
      <c>10</c>
      <d>8</d>
    </polygon>
    <polygon kind="triangle">
      <a>5</a>  
      <b>10</b>
      <c>15</c>
    </polygon>
  </shapes>

XSD 1.1 Schema [3]:
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="shapes">
       <xs:complexType>
         <xs:sequence>
           <xs:element name="polygon" type="Triangular" maxOccurs="unbounded">
             <xs:alternative test="@kind = ('square', 'rectangle')" type="Quadrilateral" />
           </xs:element>
         </xs:sequence>   
       </xs:complexType>    
    </xs:element>

    <xs:complexType name="Triangular">
       <xs:sequence>
         <xs:element name="a" type="xs:positiveInteger" />
         <xs:element name="b" type="xs:positiveInteger" />
         <xs:element name="c" type="xs:positiveInteger" />
       </xs:sequence>
       <xs:attribute name="kind" type="xs:string" use="required" />    
    </xs:complexType>

    <xs:complexType name="Quadrilateral">
       <xs:complexContent>
         <xs:extension base="Triangular">
           <xs:sequence>
             <xs:element name="d" type="xs:positiveInteger" />
           </xs:sequence>
           <xs:assert test="if (@kind = 'square') then (a = b and b = c and c = d) else true()" />
           <xs:assert test="if (@kind = 'rectangle') then (a = c and b = d) else true()" />
         </xs:extension>
       </xs:complexContent>
    </xs:complexType>

  </xs:schema>

XSD 1.1 Schema [4]:
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="shapes">
       <xs:complexType>
         <xs:sequence>
           <xs:element name="polygon" type="Polygon" maxOccurs="unbounded" />
         </xs:sequence>   
       </xs:complexType>    
    </xs:element>

    <xs:complexType name="Polygon">
       <xs:sequence>
         <xs:element name="a" type="xs:positiveInteger" />
         <xs:element name="b" type="xs:positiveInteger" />
         <xs:element name="c" type="xs:positiveInteger" />
         <xs:element name="d" type="xs:positiveInteger" minOccurs="0" />
       </xs:sequence>
       <xs:attribute name="kind" type="xs:string" use="required" />
       <xs:assert test="if (@kind = 'triangle') then not(d) else true()" />
       <xs:assert test="if (@kind = 'square') then (a = b and b = c and c = d) else true()" />
       <xs:assert test="if (@kind = 'rectangle') then (a = c and b = d) else true()" />    
    </xs:complexType>

  </xs:schema>

XSD 1.1 Schema [5]:
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="shapes">
       <xs:complexType>
         <xs:sequence>
           <xs:element name="polygon" type="Polygon" maxOccurs="unbounded" />
         </xs:sequence>   
       </xs:complexType>
    </xs:element>

    <xs:complexType name="Polygon">
       <xs:sequence>
         <xs:element name="a" type="xs:positiveInteger" />
         <xs:element name="b" type="xs:positiveInteger" />
         <xs:element name="c" type="xs:positiveInteger" />
         <xs:element name="d" type="xs:positiveInteger" minOccurs="0" />
       </xs:sequence>
       <xs:attribute name="kind" use="required">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="square" />
              <xs:enumeration value="rectangle" />
              <xs:enumeration value="triangle" />
            </xs:restriction>
          </xs:simpleType>
       </xs:attribute>
    </xs:complexType>

  </xs:schema>

XSD 1.1 Schema [6]:
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="shapes">
      <xs:complexType>
         <xs:sequence>
           <xs:element name="polygon" type="Polygon" maxOccurs="unbounded" />
         </xs:sequence>   
       </xs:complexType>    
    </xs:element>

    <xs:complexType name="Polygon">
       <xs:sequence>
         <xs:element name="a" type="xs:positiveInteger" />
         <xs:element name="b" type="xs:positiveInteger" />
         <xs:element name="c" type="xs:positiveInteger" />
       </xs:sequence>
       <xs:attribute name="kind" use="required">
         <xs:simpleType>
           <xs:restriction base="xs:string">
             <xs:enumeration value="square" />
             <xs:enumeration value="rectangle" />
             <xs:enumeration value="triangle" />
           </xs:restriction>
         </xs:simpleType>
        </xs:attribute>    
    </xs:complexType>
 
    <xs:complexType name="Quadrilateral">
       <xs:complexContent>
         <xs:extension base="Polygon">
           <xs:sequence>
             <xs:element name="d" type="xs:positiveInteger" />
           </xs:sequence>
         </xs:extension>
       </xs:complexContent>
    </xs:complexType>

  </xs:schema>

The goal of this use-case is following:
To define a XSD content model, for the XML document [1].

Solution of the use-case, and analysis:
The XSD 1.1 way of solving this would be schema's, [3] or [4] (These are possible solutions, that come to my mind. There could be other solutions too).

The Schema [3] uses both CTA and assertions. Whereas, Schema [4] uses only assertions. To solve this particular use-case, I might likely prefer Schema [4], because the content model defined in this schema is simpler/smaller, which is achieved by defining less of Schema types (only the 'Polygon' type here), and achieving further validation objectives, by defining assertions within this type.

Though, Schema [3] is also an useful solution to this problem, which according to me depicts better XSD type modularity, and also offers better possibilies to reuse the types, defined here in other contexts/use-cases.

But my gut feeling, is to go for Schema [4], for this use-case :)

I am next trying to think, how to solve this use-case in XSD 1.0 way. Here are the things, that come to my mind (with some of of my analysis):
1. Write a XSD Schema, as number [5] above. This is close to the desired solution of the use-case, described in this post. But this schema, doesn't solve this problem completely, as it doesn't strictly enforce the properties of a traingle (has 3 sides), square (has 4 sides, and all sides are equal) or a rectangle (has 4 sides, and opposite sides are equal). This is where, XSD assertions are really needed, if we want to specify XML validation entirely in the XSD layer (I think specifying much of XML validation in XSD layer is good, from application design point of view, as constraints specified with assertions, are entirely declarative and can be easily specified/modified by people, responsible for maintaining business rules, and without requiring to write say procedural code for these kind of validations, in imperative/OO languages like Java).
2. Modify the XML instance, to something like [2]. i.e, make use of XSD 1.0 construct xsi:type (which needs to be specified in the XML instance document) in some way, and validate it with a Schema like, [6]. This solution again, doesn't (and I think with XSD 1.0, we cannot do so) enforce properties of different kind of polygons (as specified in point 1, above), and this also makes the XML document XSD language specific (because it contains the instruction, xsi:type from XSD namespace), making it inconvenient to use such an XML document in environments, where XSD is not available, or where XSD processing is not needed.

The solutions presented in this post, are some of the possible ways, in which the given problem description here might be solved. But I can imagine, that there could be few other possibilities too (from XSD, syntax point of view), to solve such a use-case.

That's all about, I wanted to write at the moment :)

I hope that this post was useful!

Thursday, December 10, 2009

Xerces-J: XSD 1.1 assertions implementation updates

There have been some improvements lately, to the XSD 1.1 assertions support in Xerces-J.

Here are the summary of recent assertion implementation changes, in Xerces-J:

1) XPath 2 expressions, in assertion facets should not access the XPath 2 context, because XPath context is "undefined" during assert facet evaluation

This implies that, the right way to invoke assertion facets, is as follows:
  <xs:simpleType>
    <xs:restriction base="xs:int">
      <xs:assertion test="$value mod 2 = 0" />
    </xs:restriction>
  </xs:simpleType>

(i.e, we need to use the XPath "dynamic context" variable, $value to access the XSD simple type value.)

If an attempt is made to access the XPath context in above XPath expression, like say as follows (using the expression, "." here):
<xs:assertion test=". mod 2 = 0" />

Xerces returns an error message like, following:
test.xml:4:21:cvc-assertion.4.3.15.3: Assertion evaluation ('. mod 2 = 0') for element 'x (attribute => a)' with type '#anonymous' did not succeed (undefined context).

Or an XPath expression, like following:
./@a mod 2 = 0

Would result in a similar error.

A special error message, was constructed (designating, "undefined context" to the user) in Xerces, for this use case.

2) Ability to evaluate assertions, on XML attributes

If attributes in XML document use user-defined XSD simple types, then assertions would also apply to attributes, as they do for XML elements.

Following is a little example for this, use case.

XML document:
  <Example>
    <x a="210">101</x>
  </Example>

Corresponding XSD 1.1 schema:
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
    <xs:element name="Example">
       <xs:complexType>
         <xs:sequence>
           <xs:element name="x" type="X_Type" maxOccurs="unbounded" />
         </xs:sequence>
       </xs:complexType>
    </xs:element>
 
    <xs:complexType name="X_Type">
       <xs:simpleContent>
          <xs:extension base="xs:int">
             <xs:attribute name="a">
                <xs:simpleType>
                   <xs:restriction base="xs:int">
                     <xs:assertion test="$value mod 2 = 0" />
                   </xs:restriction>
                </xs:simpleType>
             </xs:attribute>
          </xs:extension>
       </xs:simpleContent>
     </xs:complexType>

  </xs:schema>

Please note, that how we specify a XSD user-defined simple type for attribute "a" above, and an assertion facet on the simple type (there could by 0-n assertion facets here, as we have been looking at earlier).

The assertion facet XPath expression, $value mod 2 = 0 would operate on the context variable, $value (which is the attribute's value) and such an assert facet doesn't have access to the XPath context (a "context undefined" error would be flagged by Xerces, if an attempt is made to access the XPath context).

I hope, that this post was useful.

Saturday, December 5, 2009

XPath 2.0: PsychoPath XPath processor update

I've just run all the PsychoPath XPath 2 processor (an Eclipse Web Tools, Source Editing sub-project) W3C test-suite tests, and here are the results for them:

Tests: 8143
Errors: 0
Failures: 0

So it seems, PsychoPath XPath engine passes, 100% of the W3C XPath 2.0 test suite, and some of it's own tests.

This should be a moment of cheer, and wow!

It also looks, like that the upcoming Xerces-J release, 2.10.0 (ref, http://wiki.apache.org/xerces/November2009) would be getting almost a compliant XPath 2.0, engine for XSD 1.1 assertions and CTA.

Ref: An earlier post about PsychoPath status: http://mukulgandhi.blogspot.com/2009/09/psychopath-xpath-20-processor-update.html.

Saturday, November 28, 2009

Xerces-J: XSD 1.1 assertions on simple types

I'm trying to put up a post here, with few examples for assertions on XSD simple types, and also for complex types with simple contents, and testing them with Xerces-J XSD 1.1 implementation. The previous couple of posts on this blog, described assertions on XSD complex types having complex content (i.e, elements having "element only" or mixed content, and/or attributes).

1) Here's an example, taken from Roger L. Costello's collections of XSD 1.1 examples, which he's published on his web site:

XML document [1]:
  <Example>
    <even-integer>100</even-integer>        
  </Example>

XSD 1.1 document [2]:
  <schema xmlns="http://www.w3.org/2001/XMLSchema"
          elementFormDefault="qualified">

    <element name="Example">
       <complexType>
          <sequence>
             <element name="even-integer">
                <simpleType>
                  <restriction base="integer">
                     <assertion test="$value mod 2 = 0" />
                  </restriction>
                </simpleType>
             </element>
          </sequence>
       </complexType>
    </element>

  </schema>

The above XSD 1.1 schema [2] constrains the XSD integer values, to only even ones (this works fine with Xerces!). XSD 1.1 defines a new facet named, assertion on XSD built in simple types, which the above example describes.

Please note that, "assertion" facet (applicable both to XSD simple types, and complex types with simple contents) is conceptually different than "assert" constraint on complex types (some of the explanation, about this is also given below as well).

The XSD 1.1 spec mentions, that the assertions XPath 2 "dynamic context" get's augmented with a variable, $value. The XSD type of variable, $value is that of the base simple type (in this example, the type of $value is xs:integer). The detailed rules, for using variable $value in XSD 1.1 schemas are described, here.

It looks to me, that the ability to have an assertion facet on simple types, significantly enhances the XSD author's capability to provide many new constraints on simple type values, which were not possible in XSD 1.0 (for e.g, an ability to constrain integer values to be even, was not possible in XSD 1.0).

For the above example, we could specify assertions to something like below, as well:
<assertion test="$value mod 2 = 0" />
<assertion test="$value lt 500" />
(i.e, a set of two assertion facet instances)

Or perhaps, specifying only one assertion facet instance as following, <assertion test="($value mod 2 = 0) and ($value lt 500)" /> if user wishes, which realizes the same objective.

This enforces that the simple type value should be even, and also should be less than 500. Also, there are no limits to the number of assertion facet instances that can be specified. To my opinion, an ability to specify unlimited number of assertion facets (and also the assert constraints on complex types), makes assertions a tremendously useful XSD validation constructs.

Notes: Interestingly, the following facet definition achieves the same results as met by the 2nd assertion facet instance, that's described above:
<maxExclusive value="500" />
(this was available in, XSD 1.0 as well)

2) Complex types with simple contents, using assertions:
XML document [3]:
  <root>
    <x label="a">2</x>
    <x label="b">4</x>
  </root>

Here, the element "x" should have an attribute "label" with type xs:string. But the content of element "x" is simple (of type, xs:int for this example).
Additional we also want, that the simple content value of "x", should be an even number.

The XSD document for these validation constraints, is as follows [4]:
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
   <xs:element name="root">
     <xs:complexType>
       <xs:sequence>
         <xs:element name="x" maxOccurs="unbounded" type="X_Type" />
       </xs:sequence>
     </xs:complexType>
   </xs:element>
   
   <xs:complexType name="X_Type">
     <xs:simpleContent>
        <xs:extension base="xs:int">    
          <xs:attribute name="label" type="xs:string" />
          <xs:assert test="$value mod 2 = 0" />
        </xs:extension>
     </xs:simpleContent>
   </xs:complexType>
  
  </xs:schema>

The use of xs:assert instruction is stressed in this example.

It's interesting to see, that if we change value of one of "x" elements as follows:
<x label="a">21</x>
(I changed the first "x")

Xerces fails the validation of XML instance, and returns following error message to the user:
test.xml:2:22:cvc-assertion.3.13.4.1: Assertion evaluation ('$value mod 2 = 0') for element 'x' with type 'X_Type' did not succeed.

Here, the XML validation did not succeed, because the value 21 is not an even number.

3) The last example of this post is following:
This describes the scenario of Complex types with simple contents. But here, the simple content get's its value by "restriction of a complex type". The previous example described Complex types with simple contents, using derivation by extension.

The XML file remains same [3], while the new XSD document is following [5]:
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
   <xs:element name="root">
     <xs:complexType>
       <xs:sequence>
         <xs:element name="x" maxOccurs="unbounded" type="X_Type" />
       </xs:sequence>
     </xs:complexType>
   </xs:element>
   
   <xs:complexType name="X_Type">
     <xs:simpleContent>
        <xs:restriction base="x_base">      
           <xs:assertion test="$value mod 2 = 0" />
           <xs:assert test="@label = ('a','b')" />
        </xs:restriction>
     </xs:simpleContent>
   </xs:complexType>
   
   <xs:complexType name="x_base">
     <xs:simpleContent>
        <xs:extension base="xs:int">    
          <xs:attribute name="label" type="xs:string" />
        </xs:extension>
     </xs:simpleContent>
   </xs:complexType>
  
 </xs:schema>

Please notice, how assertions are specified on the complex type, "X_Type" (shown with bold emphasis). Here, we have two assertion instructions (xs:assertion and xs:assert). In this example, xs:assertion is a facet for the atomic value, of the complex type (the value of complex type is simple in this case!). While xs:assert is the assertions instruction on the complex type (which has access to the element tree).

The complexType -> simpleContent -> restriction, type definition can specify assertions with following grammar:
... assertion*, ..., assert* (i.e, 0-n xs:assertion components can be followed by 0-n xs:assert components (this ordering is significant, otherwise the XSD 1.1 processor will flag an error).
There could be other constructs as well, before xs:assertion here (and some after it. But anything after xs:assertion*, needs to be before the trailing xs:assert's). This is described in the relevant XSD 1.1 grammar at, http://www.w3.org/TR/2009/CR-xmlschema11-1-20090430/#dcl.ctd.ctsc.

Notes: The XML Schema WG decided to have two different names for assertion instructions (xs:assertion and xs:assert), for this particular scenario, so that the XSD Schema authors could decide, whether they are writing assertions as a facet for simple values, or assertions for complex types (which have access to the element tree). If this naming distinction was not made in XSD 1.1 assertions, then specification of asserts in XSD documents, in this case would have caused ambiguity (i.e, the XSD 1.1 processor could not tell, which assertion is a facet, and which is an assertion for the complex type).

Acknowledgements:
I must mention that XSD 1.1 examples shared by Roger L. Costello, helped us fix quite a bit of bugs in Xerces assertions implementation. Our sincere thanks are due, to Roger.

References:
1. Reader's could also find this article useful, http://www.ibm.com/developerworks/library/x-xml11pt2/ about XSD 1.1 co-occurence constraints, which describes XSD 1.1 assertions facility in detail.

I hope that this post was useful.