在 XML Schema 中的元素列表中指定排序 [英] Specify sorting in lists of elements in XML Schema

查看:36
本文介绍了在 XML Schema 中的元素列表中指定排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 XML 模式中指定列表的元素必须按某些文本节点排序?

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

例如:

我想制作一个 xsd,其中以下 xml 是有效的:

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>

但以下不是:

<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>

即集合中的元素按User.Id排序出现

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

推荐答案

添加到 MS-McQ 的答案:特别是在 XSD 1.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)"

注意事项:

  1. 断言不能在单个 User 元素上,因为它是关于以断言所在元素为根的子树的断言,并且无法访问该子树之外的数据

  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

我不是故意使用否定形式(a le b),因为它适用于所有元素,甚至是第一个元素

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

如果您使用 Saxon 作为您的 XSD 1.1 处理器,那么每个用户满足 c"形式的断言会得到优化,因为如果断言失败,错误消息将告诉您哪个特定用户未能满足条件.

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.

这篇关于在 XML Schema 中的元素列表中指定排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆