XSD: A <xs:choice>等价于属性 [英] XSD: A <xs:choice> equivalent for attributes

查看:28
本文介绍了XSD: A <xs:choice>等价于属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望创建一个 XML 文档,其中必须使用两个已知属性之一定义其元素之一,但不能同时使用两者.

I wish to create an XML document in which one of its elements must be defined with one of two known attributes, but not both.

例如,我想定义一个webAddress"元素如下:

For example, I would like to define a "webAddress" element as follows:

 <xs:element name="webAddress">
     <xs:complexType>
         <xs:attribute name="hostName" type="xs:string"/>
         <xs:attribute name="hostAddress" type="xs:string"/>
     </xs:complexType>
 </xs:element>

但我只希望用户能够定义一个 hostName 属性(例如,hostName=") 或一个 hostAddress(例如,hostAddress="")属性,但不能同时定义这两个属性.似乎该构造为元素.是否有与属性等效的功能,或者是否有更好的方法来解决这个问题?

but I only want a user to be able to define either a hostName attribute (e.g., hostName= ") or a hostAddress (e.g., hostAddress="") attribute, but not both. It appears that the construct accomplishes this for elements. Is there a functional equivalent for attributes, or is there a better way to approach this?

如果发现我们无法使用 W3C XML Schema 执行此操作.我们可以使用嵌入的 schematron 规则,但是我们可以通过一起检查元素和属性来在选择元素上使用它吗?例如这样的事情:

If found that we can't do this with W3C XML Schema. We can use an embedded schematron rule but can we do it on choice element by checking element and attribute together? For example something like this:

<xs:choice>
     <xs:element name="webAddress">
         <xs:complexType>
             <xs:attribute name="hostName" type="xs:string"/>
         </xs:complexType>
     </xs:element>
     <xs:element name="webAddress">
         <xs:complexType>
             <xs:attribute name="hostAddress" type="xs:string"/>
         </xs:complexType>
     </xs:element>
  </xs:choice>

推荐答案

您可以使用类型"替换,例如

You could use "type" substitution, e.g.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <!-- Some abstract type - it might define some common parts too. It is abstract and cannot be used directly for instance -->
    <xs:complexType name="abstractType" abstract="true"/>

    <!-- First "children" type of abstract type-->
    <xs:complexType name="hostNameType">
        <xs:complexContent>
            <!-- it is derived from abstractType and adds attribute hostName -->
            <xs:extension base="abstractType">
                <xs:attribute name="hostName" type="xs:string" use="required" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <!-- Second "children" type of abstract type -->
    <xs:complexType name="hostAddressType">
        <xs:complexContent>
            <!-- it is also derived from abstractType and adds attribute hostAddress -->
            <xs:extension base="abstractType">
                <xs:attribute name="hostAddress" type="xs:string" use="required" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <!-- Element of abstractType -->
    <xs:element name="webAddress" type="abstractType" />

</xs:schema>

在实例文档中,您必须指定实际使用的类型:

In instance document you have to specify type which is actually used:

<?xml version="1.0" encoding="UTF-8"?>
<!-- by xsi:type you defined which "concrete" type is really used -->
<webAddress xsi:type="hostNameType" hostName="String" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

<?xml version="1.0" encoding="UTF-8"?>
<webAddress xsi:type="hostAddressType" hostAddress="xxxx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

但是这个定义有点冗长,而且乍一看webAddress"到底是什么类型并不明显.但这对你来说可能是一种方式.

But the definition is a bit verbose and also it is not obvious on the first sight what type "webAddress" really could be. But it could be a way for you.

这篇关于XSD: A &lt;xs:choice&gt;等价于属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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