这将是一个复杂的问题,因此如果您对 XML Schema 1.1 不是非常熟悉,您可能无法回答这个问题。
但是,此验证检查适用于 XML Schema 1.1 Nerds :)
我有一个如下的 XML:
<HTMLFile filename="N40139">
<variant>2</variant>
<overlayTitle>Job Cards</overlayTitle>
<groups>
<group>
<groupTitleBtn>true</groupTitleBtn>
<name>Removing thermostat</name>
<src>03-8.2.12.15-10.13.35_MTU_20V8000M91</src>
<steps>
<step number="1">
<start>0</start>
</step>
<step number="2">
<start>03</start>
</step>
<step number="4">
<start>30</start>
</step>
</steps>
</group>
<group>
<groupTitleBtn>true</groupTitleBtn>
<name>Removing thermostat2</name>
<src>03-8.2.12.15-10.13.35_MTU_20V8000M91</src>
<steps>
<step number="1">
<start>35</start>
</step>
<step number="2">
<start>45</start>
</step>
<step number="4">
<start>55</start>
</step>
</steps>
</group>
</groups>
</HTMLFile>
这是我现有的用于验证此 XML 的 XML 模式:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
vc:minVersion="1.1">
<xs:element name="HTMLVideoMapper">
<xs:complexType>
<xs:sequence>
<xs:element name="HTMLFile" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="variant" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="overlayTitle" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="groups" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="group" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="groupTitleBtn" type="xs:boolean" minOccurs="0" maxOccurs="1"/>
<xs:element name="name" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="src" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="steps" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="step" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="start" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="999"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="src" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="end" type="xs:string" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="number" type="xs:integer" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="filename" type="xs:string" use="required" />
<!-- overlayTitle is not allowed in variant 3 -->
<xs:assert test="(variant = 3 or overlayTitle)" />
<!-- groups/group/name is not allowed in variant 3 -->
<xs:assert test="(variant = 3 or groups/group/name)" />
<!-- groups/group/groupTitleBtn is not allowed in variant 3 -->
<xs:assert test="(variant = 3 or groups/group/groupTitleBtn)" />
<!-- groups/group/steps is not allowed in variant 3 -->
<xs:assert test="(variant = 3 or groups/group/steps)" />
<!-- in variant 3 only one group is allowed -->
<!-- if the group src in two groups is the same,
than the nested step-elements of the second group can
not have a smaller value than the last nested step-element
in the first group -->
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
但是,我需要检查三个约束。我自己已经成功地构建了这个 XML Schema,但现在我已经达到了我对它的经验的极限。我无法将 XML Schema 中的最后两条注释创建为代码。
这是任务
- 如果元素
variant
的值为 3,则 groups -> group-elements 的长度只能为 1(一个元素/对象)。 - 如果 的
variant
值为 1 或 2,则有必要遍历所有组 -> 组。在迭代时,我们将src
每个值保存group
在一个临时变量中,以便我们可以检查下一个group
src 是否相同。如果是这样,我们需要遍历该组的所有步骤和前一组的步骤。仅step
start
当第二组的第一个元素大于step
start
第一组的点时才有效。 - 如果 的
variant
值为 1 或 2,则再次需要遍历所有组 -> 组和嵌套step
元素。一个元素的每个元素都必须大于前start
一个元素。所以如果有一个with 属性并且它的元素的值为 3 ,那么下一步只有一个. 这是需要检查的。step
step
start
step
number="1"
start
number="2"
start
>3