两个元素可以在 XSD 中具有不同的名称但类型相同吗? [英] Can two elements have different names but same type in XSD?

查看:30
本文介绍了两个元素可以在 XSD 中具有不同的名称但类型相同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑两种情况:

我有一个如下所示的 XML:

I have one XML that looks like:

<personinfo>
   <info> 
        <option1>Coke</option1>
   </info>
</personinfo>

我应该在 option1 和 option2 元素之间进行选择.

where I should have a choice between an option1 and option2 element.

我有另一个类似的 XML:

I have another XML that looks like:

 <personinfo>
       <info> 
            <firstname>Yair</firstname>
            <lastname>Zaslavsky</lastname>
       </info>
    </personinfo>

名字和姓氏都应该出现的地方(因此是一个序列).

where both firstname and lastname should appear (hence a sequence).

我尝试使用以下架构:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="optionsChoice">
    <xs:choice>
      <xs:element name="option1" type="xs:string"/>
      <xs:element name="option2" type="xs:string"/>
    </xs:choice>
  </xs:complexType>

  <xs:complexType name="optionsSequence">
     <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
  </xs:complexType>

<xs:complexType name="personinfo">
  <xs:choice>
      <xs:element name="info" type="optionsSequence"/>
      <xs:element name="info" type="optionsChoice"/>
    </xs:choice>
</xs:complexType>


</xs:schema>

运气不好.

请记住,在这两种情况下,我都必须将info"作为元素名称.

Bear in mind that I must have "info" as an element name in both cases.

我该如何解决这个问题?

How can I fix this issue?

推荐答案

不能让两个名称相同但类型不同的元素一起出现在一个内容模型中.

You cannot have two elements with the same name but different types appear in a content model together.

但是,您可以将选择下推,这样 personinfo 可以是 option1option2 的选择,也可以是 option2 的选择code>firstname 和 lastname:

You can, however, push the choice down such that personinfo can be either a choice of option1 or option2 or a sequence of firstname and lastname:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="personinfo">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="info">
          <xs:complexType>
            <xs:choice>
              <xs:choice>
                <xs:element name="option1" type="xs:string"/>
                <xs:element name="option2" type="xs:string"/>
              </xs:choice>
              <xs:sequence>
                <xs:element name="firstname" type="xs:string"/>
                <xs:element name="lastname" type="xs:string"/>
              </xs:sequence>
            </xs:choice>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

此 XSD 将成功验证您的两个 XML 文档.

This XSD would valid both of your XML documents successfully.

这篇关于两个元素可以在 XSD 中具有不同的名称但类型相同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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