相同标签不同属性名称的 XSD [英] XSD for same tag different attribute names

查看:25
本文介绍了相同标签不同属性名称的 XSD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的 xml 场景,想使用 xsd 验证它.下面是xml

I have weird scenario with xml and want to validate it using xsd. below is the xml

<tag1>
   <tag2 attribute1="value1" />
   <tag2 attribute2="value2" />
<tag1>

以下是我正在使用的 xsd

and below is the xsd I am using

<xs:element name="tag1">
  <xs:complexType>
    <xs:sequence>
      <xs:element minOccurs="1" maxOccurs="unbounded" name="tag2">
        <xs:complexType>
          <xs:attribute name="attribute1" type="xs:string" use="optional" />
          <xs:attribute name="attribute2" type="xs:string" use="optional" />
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

如果tag2 具有attribute1 或attribute2 或其中任何一个,则一切正常.但我的情况是,如果没有如下所示的带有属性 1 的 tag2

everything works fine if tag2 has both or any of attribute1 or attribute2. but my scenario is if there is no tag2 with attribute1 as below

<tag1>
   <tag2 attribute2="value2" />
<tag1>

它应该被认为是无效的

<tag1>
   <tag2 attribute1="valueX" />
<tag1>

有效.

有什么方法可以构建 XSD 来验证这种情况吗?

Is there any way an XSD can be built that validates this scenario?

推荐答案

我不知道你到底想强加什么规则,但 XSD 1.0 中的一般规则是,如果两个兄弟元素具有相同的名称,那么它们必须具有相同的类型(即不能对两个同名的同级元素应用不同的验证规则).

I don't know exactly what rules you want to impose, but the general rule in XSD 1.0 is that if two sibling elements have the same name, then they must have the same type (that is, you cannot apply different validation rules to two sibling elements with the same name).

当然,在 XSD 1.1 中,您可以使用断言来解决这个问题.

In XSD 1.1, of course, you can get around this with assertions.

这篇关于相同标签不同属性名称的 XSD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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