XML模式如何要求按属性对元素进行排序?

时间:2022-01-26 17:19:14

Suppose I have an XML file that looks like this:

假设我有一个如下所示的XML文件:

<Transactions>
<Transaction OrderID="5"> ... </Transaction>
<Transaction OrderID="6"> ... </Transaction>
<Transaction OrderID="11"> ... </Transaction>
<Transaction OrderID="7"> ... </Transaction>
</Transactions>

Using XML schema, is it possible to indicate that Order #11 is not in the correct sequence? Each Transaction element individually passes validation, but the OrderID should be in increasing order.

使用XML模式,是否可以指示订单#11的顺序不正确?每个Transaction元素都单独通过验证,但OrderID应按递增顺序排列。

And, a related question: Can a validation rule indicate whether or not numbers can be skipped? For example, there is no transaction #8,9, or 10.

并且,一个相关问题:验证规则是否可以指示是否可以跳过数字?例如,没有交易#8,9或10。

1 个解决方案

#1


1  

If your validator supports XSD 1.1, you can use xs:assert to deny certain attribute values like this:

如果验证器支持XSD 1.1,则可以使用xs:assert拒绝某些属性值,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" 
           elementFormDefault="qualified" 
           attributeFormDefault="unqualified"
           vc:minVersion="1.1">
   <xs:element name="Transactions">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="Transaction" maxOccurs="unbounded">
               <xs:complexType mixed="true">
                   <xs:attribute name="OrderID" type="xs:integer"/>
                   <xs:assert test="empty(index-of((8,9,10),@OrderID))"/>
               </xs:complexType>                                
            </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema>

This should answer your second question.

这应该回答你的第二个问题。

As for your first question, I don't think order validation is possible. You can, however, use XSLT to sort the XML document.

至于你的第一个问题,我不认为订单验证是可能的。但是,您可以使用XSLT对XML文档进行排序。

----- added the answer to the first question -----

-----添加了第一个问题的答案-----

Thank you, Michael Kay. Well...here is the answer to your first question.

谢谢Michael Kay。嗯......这是你第一个问题的答案。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Transactions">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Transaction" maxOccurs="unbounded">
                    <xs:complexType mixed="true">
                        <xs:attribute name="OrderID" type="xs:integer"/>
                        <xs:assert test="empty(index-of((8,9,10),@OrderID))"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:assert test="every $x in Transaction satisfies 
                             (empty($x/preceding-sibling::*) or 
                             ($x/@OrderID gt $x/preceding-sibling::*[1]/@OrderID))"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

#1


1  

If your validator supports XSD 1.1, you can use xs:assert to deny certain attribute values like this:

如果验证器支持XSD 1.1,则可以使用xs:assert拒绝某些属性值,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" 
           elementFormDefault="qualified" 
           attributeFormDefault="unqualified"
           vc:minVersion="1.1">
   <xs:element name="Transactions">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="Transaction" maxOccurs="unbounded">
               <xs:complexType mixed="true">
                   <xs:attribute name="OrderID" type="xs:integer"/>
                   <xs:assert test="empty(index-of((8,9,10),@OrderID))"/>
               </xs:complexType>                                
            </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema>

This should answer your second question.

这应该回答你的第二个问题。

As for your first question, I don't think order validation is possible. You can, however, use XSLT to sort the XML document.

至于你的第一个问题,我不认为订单验证是可能的。但是,您可以使用XSLT对XML文档进行排序。

----- added the answer to the first question -----

-----添加了第一个问题的答案-----

Thank you, Michael Kay. Well...here is the answer to your first question.

谢谢Michael Kay。嗯......这是你第一个问题的答案。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Transactions">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Transaction" maxOccurs="unbounded">
                    <xs:complexType mixed="true">
                        <xs:attribute name="OrderID" type="xs:integer"/>
                        <xs:assert test="empty(index-of((8,9,10),@OrderID))"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:assert test="every $x in Transaction satisfies 
                             (empty($x/preceding-sibling::*) or 
                             ($x/@OrderID gt $x/preceding-sibling::*[1]/@OrderID))"/>
        </xs:complexType>
    </xs:element>
</xs:schema>