对 xsd:any? 实施进一步限制 [英] Enforce further constraints on xsd:any?

查看:26
本文介绍了对 xsd:any? 实施进一步限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XSD 1.0 序列,它定义了一组元素,其中一些元素可能是可选的,但它们都不能出现多次,并且还以 <xsd:any minOccurs="0"maxOccurs="unbounded" namespace="##any" processContents="lax"/> 标签,以便用户在以编程方式导出符合标准的 XML 时以最简单的方式添加自己的数据元素(即理想情况下无需他们定义自己的命名空间/XSD).

I have an XSD 1.0 sequence that defines a set of elements, some of which may be optional but none of which can occur more than once, and which also ends with an <xsd:any minOccurs="0" maxOccurs="unbounded" namespace="##any" processContents="lax"/> tag in order to enable users to add their own data elements in the easiest possible way when programmatically exporting conforming XML (i.e. ideally without requiring them to define their own namespace/XSD).

在处理用户生成的 XML 时,我将忽略他们通过 <any> 标记添加的任何自定义元素,但我想知道是否有办法强制元素的唯一性名称,以便我可以确定它们没有放入具有 maxOccurs=1 的序列中定义的元素的重复项?

When processing XML that users generate, I will ignore any custom elements that they add via the <any> tag, but I want to to know if there is a way to enforce uniqueness of element names so that I can be sure that they're not putting in duplicates of elements defined in the sequence with maxOccurs=1?

这是一个示例 xsd(交互式版本 这里):

Here is a sample xsd (interactive version here):

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns="testxsd" targetNamespace="testxsd" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            elementFormDefault="qualified">
    <xsd:element name="XSDSequence">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="itemA" type="xsd:string"
                             minOccurs="1" maxOccurs="1"/>
                <xsd:element name="itemB" type="xsd:string"
                             minOccurs="0" maxOccurs="1"/>
                <xsd:element name="itemC" type="xsd:string"
                             minOccurs="1" maxOccurs="1"/>
                <xsd:any minOccurs="0" maxOccurs="unbounded" 
                         namespace="##any" processContents="lax"/>  
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

这是一个通过 XSD 验证的示例 XML,但由于末尾重复的 <itemA> 我想出错:

and here is a sample XML that passes XSD validation but which I would like to error out on due to the duplicated <itemA> at the end:

<XSDSequence xmlns="testxsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <itemA>foo</itemA>
    <itemC>bar</itemC>
    <randomElement>this is ok</randomElement>
    <itemA>I want this second itemA to cause an error</itemA>
</XSDSequence>

(请注意,通过属性而不是元素捕获数据项不是我想做的事情.)

(Note that capturing data items via attributes rather than elements is not something that I want to do.)

推荐答案

不,在 XSD 1.0 中,一旦您允许 any 元素,您就不能指定超出 minOccursmaxOccursnamespaceprocessContents.

No, in XSD 1.0, once you've allowed any element, you can't specify further constraints beyond those afforded by minOccurs, maxOccurs, namespace, and processContents.

在 XSD 1.1 中,您还可以针对 xsd:any 元素制作 xsd:assertions;你可以用断言来表达你的约束.[更新:不要错过 @C.M. Sperberg-McQueen 的 XSD 1.1 更简单的想法:xsd:any/@notQName="itemA itemB itemC"]

In XSD 1.1, you can also make xsd:assertions against the xsd:any elements; you'd be able to express your constraint with an assertion. [Update: Don't miss @C. M. Sperberg-McQueen's simpler idea for XSD 1.1: xsd:any/@notQName="itemA itemB itemC"]

这篇关于对 xsd:any? 实施进一步限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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