1

这将是一个复杂的问题,因此如果您对 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 中的最后两条注释创建为代码。

这是任务

  1. 如果元素variant的值为 3,则 groups -> group-elements 的长度只能为 1(一个元素/对象)。
  2. 如果 的variant值为 1 或 2,则有必要遍历所有组 -> 组。在迭代时,我们将src每个值保存group在一个临时变量中,以便我们可以检查下一个groupsrc 是否相同。如果是这样,我们需要遍历该组的所有步骤和前一组的步骤。仅step start当第二组的第一个元素大于step start第一组的点时才有效。
  3. 如果 的variant值为 1 或 2,则再次需要遍历所有组 -> 组和嵌套step元素。一个元素的每个元素都必须大于前start一个元素。所以如果有一个with 属性并且它的元素的值为 3 ,那么下一步只有一个. 这是需要检查的。stepstep startstepnumber="1"startnumber="2"start >3
4

1 回答 1

1

Since you don't explain what it is that you're finding difficult, it's not possible to offer anything except general advice (or just solve your problem for you, which really misses the point).

Your problem appears to be mostly that you are not sure how to express your constraints in XPath.

Let's start with the simple ones you have already written. The comment says "overlayTitle is not allowed in variant 3", which seems to be incompatible with the assertion (variant = 3 or overlayTitle), which would allow

<HTMLFile filename="N40139">
  <variant>3</variant>
  <overlayTitle>Job Cards</overlayTitle>
  ...

because the XPath or is inclusive, not exclusive. I think you mean any of

if (variant eq 3) then not(overlayTitle) else true()
not(variant eq 3 and overlayTitle)
variant ne 3 or not(overlayTitle)

And similarly for the other constraints on variant 3.

For the three constraints you have not yet been able to formulate, I recommend you look at

  • the count() function (which can tell you how many occurrences of groups/group there are)

  • quantified expressions (which can check a condition on each instance of groups/group, including the complicated ones you describe)

  • general value comparisons (which can compare all of the steps descended from $group1 and $group2 to see if any descendants of $group2 are less than or equal to any descendants of $group1: $group2//step/start &lt;= $group1//step/start -- you'll want to negate this, of course).

The validation would be much simpler if there were less redundancy in the XML, of course, and if you gave variants 1, 2, and 3 distinct names.

于 2015-02-11T17:36:03.257 回答