是否可以仅在另一个元素的属性设置为特定值时才需要定义 XML 元素的属性? [英] Is it possible to define an attribute of an XML element to be required only if another element's attribute is set to a particular value?

查看:23
本文介绍了是否可以仅在另一个元素的属性设置为特定值时才需要定义 XML 元素的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下这是否可行,如果可行,如何实现:

I would like to ask if this is possible and if yes, how to achieve it:

例如,如果我在 XML 模式定义中有一个带有 complexType 的元素定义,如下所示:

For example if I have an element definition with a complexType in an XML schema definition like this:

<xsd:element name="tag" type="tag"/>
<xs:complexType name="tag">
    <xs:choice maxOccurs="unbounded">
        <xs:element name="subtag" type="subtag"/>
        <xs:element name="another_subtag" type="another_subtag"/>
        <xs:element name="another_subtag_2" type="another_subtag_2"/>
    </xs:choice>
    <xs:attribute name="type" type="attr_type"/>
    <xs:attribute name="an_attr" type="an_attr"/>
    <xs:attribute name="another_attr" type="another_attr"/>
</xs:complexType name="attr_type">
    <xsd:restriction base="xsd:string">
         <xsd:enumeration value="type_1"/>
         <xsd:enumeration value="type_2"/>
         <xsd:enumeration value="type_3"/>
    </xsd:restriction>
<xsd:simpleType/>

1) 如果标签元素的属性attr_type"设置为type_2",是否可以使标签元素的属性an_attr"成为必需的?

1) Is it possible to make the attribute 'an_attr' of the tag element required only if the tag element has the attribute 'attr_type' set to 'type_2'?

还有一个问题:2) 是否可以使 complexType 'tag' 再次包含 不同 子元素,例如关于'attr_type'的值?例如:

And another question: 2) Is it possible to make the complexType 'tag' contain different child elements based again e.g. on the value of the 'attr_type'? For example for:

<tag attr_type="type_1">

只有这个孩子:

<xs:element name="subtag" type="subtag"/>

为了:

<tag attr_type="type_2">

只有孩子:

 <xs:element name="another_subtag" type="another_subtag"/>
OR
 <xs:element name="another_subtag_2" type="another_subtag_2"/>

?

如果可能,我该如何实现?

If it is possible, how can I achieve this?

感谢关注!

正如我在这里看到的 -> https://blogs.oracle.com/rammenon/条目/xml_schema_11_what_you_need_to在示例编号 22 中(在条件类型分配中),是否可以通过这种方式完成?

As I saw here -> https://blogs.oracle.com/rammenon/entry/xml_schema_11_what_you_need_to In the example number 22 (in Conditional Type Assignments), could it be done in such a way?

<!--inline alternative type definitions --> 
<element name="TimeTravel" type="TravelType"> 
      <alternative test="@direction='Future'"> 
          <complexType> 
              <complexContent> 
              <restriction base="TravelType" 
                         .... 
<!--        some past travel related elements go here --> 
            </complexType> 
       </alternative> 
      <alternative test="@direction='Past'"> 
          <complexType> 
              <complexContent> 
              <restriction base="TravelType" 
                         .... 
   <!--        some future travel related elements go here --> 
            </complexType> 
       </alternative> 
  </element> 
                          OR 
<!--Named alternative type definitions --> 
<element name="TimeTravel" type="TravelType"> 
   <alternative test="@direction='Future' type="FutureTravelType"/> 
   <alternative test="@direction='Past' type="PastTravelType"/> 
</element>

据我了解,TimeTravel 元素可以根据方向属性值具有不同的 complexType,对吗?但它说必须使用 XSD 1.1.我可以在我的 XML 架构中使用这个规则吗?

As I understood it, TimeTravel element can have different complexTypes based on the direction attribute value, am I correct? But it says that XSD 1.1 must be used. Can I use this rules inside my XML Schema?

推荐答案

我们再试一次,因为我的答案有争议,最好再详细一点.编辑我的原始答案会使随后的评论变得难以理解,只会增加混乱.

Let's try again, since my answer has been disputed, it's probably best to give it again in more detail. Editing my original answer would make the subsequent comments incomprehensible and only add confusion.

问题 1:是否可以仅当标签元素的属性attr_type"设置为type_2"时才需要标签元素的属性an_attr"?

Question 1: Is it possible to make the attribute 'an_attr' of the tag element required only if the tag element has the attribute 'attr_type' set to 'type_2'?

答案 1:是的.您可以这样做(已测试;从原始版本修改以避免引用不相关和未定义的类型):

Answer 1: yes. You can do it like this (tested; modified from the original to avoid reference to irrelevant and undefined types):

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="tag" type="tagType">
      <xs:alternative test="@attr_type='type_2'" type="tagTypeWithMandatoryAttr"/>
    </xs:element>

    <xs:complexType name="tagType">
        <xs:choice maxOccurs="unbounded">
            <xs:element name="subtag" type="xs:anyType"/>
            <xs:element name="another_subtag" type="xs:anyType"/>
            <xs:element name="another_subtag_2" type="xs:anyType"/>
        </xs:choice>
        <xs:attribute name="type" type="xs:string"/>
        <xs:attribute name="an_attr" type="xs:string"/>
        <xs:attribute name="another_attr" type="xs:string"/>
    </xs:complexType>

    <xs:complexType name="tagTypeWithMandatoryAttr">
     <xs:complexContent>
      <xs:restriction base="tagType">
          <xs:choice maxOccurs="unbounded">
            <xs:element name="subtag" type="xs:anyType"/>
            <xs:element name="another_subtag" type="xs:anyType"/>
            <xs:element name="another_subtag_2" type="xs:anyType"/>
          </xs:choice>
          <xs:attribute name="type" type="xs:string"/>
          <xs:attribute name="an_attr" type="xs:string" use="required"/>
          <xs:attribute name="another_attr" type="xs:string"/>
      </xs:restriction>
     </xs:complexContent>
    </xs:complexType>
</xs:schema>

问题 2:

是否可以使 complexType 'tag' 再次包含不同的子元素,例如'attr_type' 的值?

Is it possible to make the complexType 'tag' contain different child elements based again e.g. on the value of the 'attr_type'?

答案 2:

是的,例如(类似测试):

Yes, for example (similarly tested):

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="tag" type="xs:anyType">
      <xs:alternative test="@attr_type='type_2'" type="tagType1"/>
      <xs:alternative type="tagType2"/>
    </xs:element>

    <xs:complexType name="tagType1">
        <xs:choice maxOccurs="unbounded">
            <xs:element name="subtag" type="xs:anyType"/>
            <xs:element name="another_subtag" type="xs:anyType"/>
            <xs:element name="another_subtag_2" type="xs:anyType"/>
        </xs:choice> 
    </xs:complexType>

    <xs:complexType name="tagType2">
        <xs:choice maxOccurs="unbounded">
            <xs:element name="subtag" type="xs:anyType"/>
            <xs:element name="another_subtag" type="xs:anyType"/>
            <xs:element name="another_subtag_2" type="xs:anyType"/>
        </xs:choice> 
    </xs:complexType>
</xs:schema>

问题 0:是否可以将 XML 元素 (E) 的属性定义为仅当另一个元素 (F) 的属性设置为特定值时才需要?

Question 0: Is it possible to define an attribute of an XML element (E) to be required only if another element (F)'s attribute is set to a particular value?

答案0:取决于两个元素E和F的关系.如果F是E的祖先,那么可以在F的声明中使用条件类型赋值来完成.如果E和F是兄弟或更多远程相关,则不能使用条件类型赋值来完成,但可以使用附加到 E 和 F 的共同祖先的断言来完成.

Answer 0: it depends on the relationship of the two elements E and F. If F is an ancestor of E, then it can be done using conditional type assignment in the declaration of F. If E and F are siblings or more remotely related, then it cannot be done using conditional type assignment, but it can be done using an assertion attached to the common ancestor of E and F.

这篇关于是否可以仅在另一个元素的属性设置为特定值时才需要定义 XML 元素的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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