Specify sorting in lists of elements in XML Schema

江枫思渺然 提交于 2019-12-23 23:27:06

问题


Is there a way to specify in an XML Schema that the elements of a list must appear sorted by some text nodes?

E.g.:

I want to craft an xsd for which the following xml is valid:

<root>
    <Users>
        <User>
            <Id>1</Id>
            <Username>lsuarez</Username>
        </User>
        <User>
            <Id>3</Id>
            <Username>dforlan</Username>
        </User>
        <User>
            <Id>7</Id>
            <Username>ecavanni</Username>
        </User>
    </Users>
</root>

But the following is not:

<root>
    <Users>
        <User>
            <Id>7</Id>
            <Username>ecavanni</Username>
        </User>
        <User>
            <Id>1</Id>
            <Username>lsuarez</Username>
        </User>
        <User>
            <Id>3</Id>
            <Username>dforlan</Username>
        </User>
    </Users>
</root>

That is, the elements in the collection appear sorted by User.Id


回答1:


Adding to MS-McQ's answer: specifically in XSD 1.1, you need an assertion on the complex type of the Users element, of the form

test="every $u in User satisfies not($u/Id le $u/preceding-sibling::User[1]/Id)"

Notes:

  1. The assertion can't be on an individual User element because it's an assertion about the subtree rooted at the element on which the assertion appears, and has no access to data outside that subtree

  2. I've used the negative form not(a le b) deliberately because it's true for all elements even the first

  3. If you use Saxon as your XSD 1.1 processor, assertions of the form "every User satisfies c" are optimized in the sense that if the assertion fails, the error message will tell you which particular User failed to satisfy the condition.




回答2:


In XSD 1.1, you could use an assertion on the type used for Users (or root) to enforce this. I can't think of a way to do it in XSD 1.0 (but perhaps some other ingenious SO reader can).



来源:https://stackoverflow.com/questions/17352043/specify-sorting-in-lists-of-elements-in-xml-schema

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!