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.

4 comments:

Swati said...

Are Assertions on complex types supported in Xerces-C++ 2.8?

Mukul Gandhi said...

assertions, and other XSD 1.1 features are currently supported only in Xerces-J.

Swati said...

Thanks for your help!

Muhammad Azeem said...

more script see here

http://java.pakcarid.com/Cpp.aspx?sub=323&ff=2542&topid=38&sls=24