xmpp : XML schema - xsd

I am a beginner in XML schema and XMPP message protocols. Is there any explicit way to understand the below XML schema. What does each tags means and how many types of tags are there - what is the specified way of reading and writing them:
<?xml version='1.0' encoding='UTF-8'?>
<xs:schema
xmlns:xs='http://www.w3.org/2001/XMLSchema'
targetNamespace='http://jabber.org/protocol/address'
xmlns='http://jabber.org/protocol/address'
elementFormDefault='qualified'>
<xs:annotation>
<xs:documentation>
The protocol documented by this schema is defined in
XEP-0033: http://www.xmpp.org/extensions/xep-0033.html
</xs:documentation>
</xs:annotation>
<xs:element name='address'>
<xs:complexType>
<xs:simpleContent>
<xs:extension base='empty'>
<xs:simpleType>
<xs:restriction base='xs:NCName'>
<xs:enumeration value='bcc'/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name='uri' use='optional' type='xs:string'/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:simpleType name='empty'>
<xs:restriction base='xs:string'>
<xs:enumeration value=''/>
</xs:restriction>
</xs:simpleType>
</xs:schema>

XSD is a complex language, and there's no way of giving an adequate explanation in a few paragraphs. Reading the W3C XML Schema Primer (XML Schema Part 0) might be a good start; even better, get Eric van der Vlist's book and read the opening chapters.
The fragment you have given us is not well-formed: it contains an unmatched </xs:attribute> end tag at line 26. If we add the missing line:
<xs:attribute name='type' use='required'>
at line 21, then the schema is defining rules for an address element that has two attributes and no content: a mandatory type attribute whose only permitted value is bcc, and an optional uri attribute whose value is any string. It's a rather odd way to define an element with no content: it's actually defining it as an element that does have content, where the content must be an empty string. Perhaps the schema designers had a reason for this, but it's not immediately obvious.

Related

XSD custom type with attribute and restriction

I am developing an XSD document to validate XML Import files. Nearly all elements of the import file 'can' have an ID attribute (UPDATE). The UPDATE attribute must be limited to 4 possible values, so I have this pre-set type to use for the attribute restriction...
<xs:simpleType name="MyUpDir">
<xs:restriction base="xs:string">
<xs:enumeration value="OVERWRITE"/>
<xs:enumeration value="ADDONLY" />
<xs:enumeration value="NOERASE" />
<xs:enumeration value="IGNORE" />
</xs:restriction>
</xs:simpleType>
In addition to the attribute restrictions, each element's value is limited by a variety of pre-set custom types
Example:
<xs:simpleType name="MyChar50">
<xs:restriction base="xs:string">
<xs:maxLength value="50" />
</xs:restriction>
</xs:simpleType>
To combine the two, I know I can do it in-line for each element as follows:
<xs:element name="FullName">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="MyChar50">
<xs:attribute name="UPDATE" type="MyUpDir" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
The problem is that there are over 1000 elements in the import file, each having varying length/regEx/precision restrictions (roughly 20 custom types) as well as have the potential for the UPDATE attribute. Without the UPDATE attribute, I could do each element on its own line by using the custom types, greatly reducing the 'content' portion of the XSD. But from what I've read, it appears that to accomodate the custom types AND the potential for the attribute mentioned, I'm forced to use the expanded sample (last example) instead of being able to retain a single line for each such element. Is there not a way to minimize this further by creating a custom type that combines the two?
I would think that you could do 20 custom types more (for a total of 40) and then use the appropriate ones (w/ or w/o attribute). In your case:
<xs:complexType name="MyChar50Attr"><!-- This one has attributes -->
<xs:simpleContent>
<xs:extension base="MyChar50">
<xs:attribute name="UPDATE" type="MyUpDir"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="FullName" type="MyChar50Attr"/>

XSD: restrict attribute to xs:float or ""

I'm trying to define an element type in XSD, for which i want an optional attribute, which if present can either contain a float, or be empty (but still present).
i.e:
<xs:element name="MyElement">
<xs:complexType>
<xs:attribute name="optionalFloatAttribute" type="xs:float" use="optional"/>
</xs:complexType>
</xs:element>
Needs "fixing" to allow all of the following xml:-
<MyElement/>
or
<MyElement optionalFloatAttribute=""/>
or
<MyElement optionalFloatAttribute="3.14159"/>
The only way I can see of doing this is to change type to xs:string, and use xs:restriction with a regular expression. But this doesn't seem very ideal to me. Is there a better way?
And I have to be able to support these variations of the xml - the program and existing xml is legacy, and I am trying to back-create a schema to match the myriad variations I see in what we have to regard as valid xml.
You can define custom type for that by combining float and empty string:
<xs:element name="MyElement">
<xs:complexType>
<xs:attribute name="optionalFloatAttribute" type="emptyFloat" use="optional"/>
</xs:complexType>
</xs:element>
<xs:simpleType name="emptyFloat">
<xs:union>
<xs:simpleType>
<xs:restriction base='xs:string'>
<xs:length value="0"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base='xs:float'>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
Or using regExp:
<xs:simpleType name="emptyFloat">
<xs:restriction base="xs:string">
<xs:pattern value="-?\d*\.?\d*"/>
</xs:restriction>
</xs:simpleType>
If you could stand using an element rather than an attribute you could make the xs:float nillable. This way you can use the xsi:nil="true" in your instance document to indicate that the element has no value:
<!-- definition -->
<xs:element name="quantity" type="xs:float" nillable="true" />
<!-- instance -->
<quantity xsi:nil="true" />
No equivalent for attributes though.
I don't think there's a way to handle this and use xs:float. Fundamentally it comes down to the fact that empty string isn't a valid number. You'd either normally expect a value of 0, or for the element to be missing altogether. There's a good explanation as the answer to the following question:
Empty elements for primitve datatypes forbidden in XSD
It seems that the option of using xs:string and a regexp might be your best plan.

Is this the proper way to added enumerated attributes to a complexType?

Is this the proper way to set an attribute with enumerated values on the complexType AvailStatusMessageType. I see a lot of examples that declare a complexContent section right below the complexType declaration? What is this complexContent for and is it necessary here?
<xs:complexType name="AvailStatusMessageType">
<xs:sequence>
<xs:element name="LengthsOfStay" type="LengthsOfStayType" />
<xs:element name="RestrictionStatus" type="RestrictionStatusType"/>
</xs:sequence>
<xs:attribute name="BookingLimit">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="SetLimit" />
<xs:enumeration value="AdjustLimit"/>
<xs:enumeration value="RemoveLimit"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
Element types can be divided into two categories in XML Schemas
Elements that can contain structural markup (attributes or child elements)
Elements that contain only textual markup
Further, the elements that contain markup (group 1) can be again divided to two groups
Elements that are allowed to have child elements
Elements that are not allowed to have child elements
First division separates (1) <complexType> and (2) <simpleType>. Second one separates (1) <complexContent> and (2) <simpleContent>.
<xs:complexContent> is not usually seen, because it is an implicit default so the whole structure can be abbreviated by omitting that element. This common structure
<xs:complexType>
... (<xs:sequence> or anything) ...
</xs:complexType>
is actually identical to
<xs:complexType>
<xs:complexContent>
<xs:restriction base="xs:anyType">
... (<xs:sequence> or anything) ...
</xs:restriction>
</xs:complexContent>
</xs:complexType>
In your structure an element with type "AvailStatusMessageType" 1) contains markup 2) and has child elements. So your structure is a complex type with complex content. Your example seems correct even though you haven't used the <xs:complexContent> element, because you are actually using the abbreviated form. It is identical to this:
<xs:complexType name="AvailStatusMessageType">
<xs:complexContent>
<xs:restriction base="xs:anyType">
<xs:sequence>
<xs:element name="LengthsOfStay" type="LengthsOfStayType" />
<xs:element name="RestrictionStatus" type="RestrictionStatusType"/>
</xs:sequence>
<xs:attribute name="BookingLimit">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="SetLimit" />
<xs:enumeration value="AdjustLimit"/>
<xs:enumeration value="RemoveLimit"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
A complex type may have simple or complex content. You would use <simpleContent> if you were defining a complex type with simple content. You must use the <complexContent> element when you are restricting or extending another complex type (because you specify the base type as an attribute in the <complexContent> element). If you are simply creating a complex type (with complex content), as you are doing, you may use (but are not required to use) the <complexContent> element.
Your enumeration looks right.

XSD any element

I'm trying to create a list that some of the elements are defined and some are not, without priority to order.
I tried it this way, with an any element:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="object" mixed="true">
<xs:choice>
<xs:element name="value" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:enumeration value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:any namespace="##any" processContents="skip"/>
</xs:choice>
</xs:complexType>
<xs:element name="object" type="object"/>
</xs:schema>
And it tells me this error:
:0:0: error: complex type 'object' violates the unique particle
attribution rule in its components 'value' and '##any'
Can someone help me out solve the problem?
You cannot define your schema like that, it violates the unique particle attribution rule: the parser cannot tell whether a "value" element it finds in the document should be validated against "value" or against "any".
Here is a good overview.
Consider using two namespaces and using xsd:any with a namespace, this will remove the problem.

How to declare a non-string element as having optional content in XML Schema

I have seen XML Schema element with attributes containing only text but I have an element that's an xs:dateTime instead.
The document I'm trying to write a schema for looks like this:
<web-campaigns>
<web-campaign>
<id>1231</id>
<start-at nil="true"/>
</web-campaign>
<web-campaign>
<id>1232</id>
<start-at>2009-08-08T09:00:00Z</start-at>
</web-campaign>
</web-campaigns>
Sometimes the xs:dateTime element has content, sometimes it doesn't.
What I have so far (which doesn't validate yet) is:
<xs:element name="start-at">
<xs:complexType mixed="true">
<xs:simpleContent>
<xs:extension base="xs:dateTime">
<xs:attribute name="nil" default="false" type="xs:boolean" use="optional" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
If I replace xs:dateTime with xs:string, I can validate the document just fine, but I really want an xs:dateTime, to indicate to consumers what's in that element. I tried with/without mixed="true" as well, to no avail.
If it makes a difference, I validate using xmllint (on Mac OS X 10.5) and XML Schema Validator
you can define your own types as union of types.
1/ define the "empty" type as a string that only allows "" ähm nothing :)
<xs:simpleType name="empty">
<xs:restriction base="xs:string">
<xs:enumeration value=""/>
</xs:restriction>
</xs:simpleType>
2/ next define a type that allows date AND empty
<xs:simpleType name="empty-dateTime">
<xs:union memberTypes="xs:dateTime empty"/>
</xs:simpleType>
3/ declare all your nullable datetime elements as type="empty-dateTime"
You need
<xs:element name="start-at" minOccurs="0">
mixed-mode isn't relevant to your situation, you don't need that. By default, minOccurs="1", i.e. the element is mandatory.
With minOccurs="0", you either specify the element with content, or not at all. If you want to be able to permit <start-at/>, then you cannot use xs:dateTime.

Resources