<xs:assert>
element. Your assertions are written in XPath, and they are evaluated as part of the document validation.<xs:assert>
element, that I borrowed from the XML Schema 1.1 W3C Recommendation:<xs:complexType name="intRange">
<xs:attribute name="min" type="xs:int"/>
<xs:attribute name="max" type="xs:int"/>
<xs:assert test="@min le @max"/>
</xs:complexType>
intRange
has a min
and a max
integer attribute. The assertion ensures that the beginning of the range is less than or equal to the end of the range. A sensible rule, that is easy to implement in XML Schema 1.1.test
attribute of the <xs:assert>
element. Tests are XPath 2.0 expressions, that must evaluate to either true or false. If a test expression does not evaluate to a boolean value, it will be converted automatically, as if it had been wrapped in an fn:boolean
call.<?xml version="1.0" encoding="UTF-8"?>
<agenda>
<item type="registration" startTime="08:00:00" endTime="09:00:00">Registration</item>
<item type="keynote" startTime="09:00:00" endTime="09:30:00">Opening keynote</item>
<item type="session" startTime="09:30:00" endTime="10:30:00">First interesting session of the day</item>
<item type="break" startTime="10:30:00" endTime="10:45:00">Short coffee break</item>
<item type="session" startTime="10:45:00" endTime="11:45:00">Another really interesting session</item>
<item type="session" startTime="11:45:00" endTime="12:30:00">A slightly shorter session</item>
<item type="break" startTime="12:30:00" endTime="13:30:00">Lunch break</item>
<item type="session" startTime="13:30:00" endTime="14:30:00">First session after lunch</item>
<item type="break" startTime="14:30:00" endTime="14:45:00">Afternoon coffee break</item>
<item type="session" startTime="14:40:00" endTime="15:30:00">Another slightly shorter session</item>
<item type="session" startTime="15:30:00" endTime="16:30:00">Last session of the day</item>
</agenda>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="agenda" type="agendaItem"/>
<xs:complexType name="agendaItem">
<xs:sequence>
<xs:element name="item" minOccurs="1" maxOccurs="unbounded">
<xs:complexType mixed="true">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="endTime" use="required" type="xs:time"/>
<xs:attribute name="startTime" use="required" type="xs:time"/>
<xs:attribute name="type" use="required" type="itemType"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="itemType">
<xs:restriction base="xs:string">
<xs:enumeration value="registration" />
<xs:enumeration value="keynote" />
<xs:enumeration value="session" />
<xs:enumeration value="break" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
<item>
level:<xs:assert test="@startTime lt @endTime"/>
<item>
element with an attribute of a sibling <item>
element, the assertion cannot be added at the <item>
level. Instead, we add it at the <agenda>
level. Here’s the complete assertion:<xs:assert test="every $item in item satisfies (not($item/preceding-sibling::item) or $item/@startTime eq $item/preceding-sibling::item[1]/@endTime)"/>
<item>
element must either be the first item (not($item/preceding-sibling::item)
) or its startTime
attribute must match the endTime
attribute of the previous item ($item/@startTime eq $item/preceding-sibling::item[1]/@endTime
).<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" vc:minVersion="1.1" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning">
<xs:element name="agenda" type="agendaItem"/>
<xs:complexType name="agendaItem">
<xs:sequence>
<xs:element name="item" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="endTime" use="required" type="xs:time"/>
<xs:attribute name="startTime" use="required" type="xs:time"/>
<xs:attribute name="type" use="required" type="itemType"/>
<xs:assert test="@startTime lt @endTime"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:assert test="every $item in item satisfies (not($item/preceding-sibling::item) or $item/@startTime eq $item/preceding-sibling::item[1]/@endTime)"/>
</xs:complexType>
<xs:simpleType name="itemType">
<xs:restriction base="xs:string">
<xs:enumeration value="registration" />
<xs:enumeration value="keynote" />
<xs:enumeration value="session" />
<xs:enumeration value="break" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
message
attribute:<xs:assert
xmlns:saxon="http://saxon.sf.net/"
saxon:message="An agenda item's end time must be greater than its start time"
test="@startTime lt @endTime"
/>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
11 | |
9 | |
7 | |
6 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 |