如何强制子元素在 XSD 中具有值? [英] How to force child element to have a value in XSD?

查看:26
本文介绍了如何强制子元素在 XSD 中具有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML(例如),在 Order 段中有一个名为 Type 的段.该段是一对多的,所以我们有类似的东西:

I have XML (as an example) with a segment called Type within the segment Order. The segment is a one to many, so we have something like :

<Order>
  <Type>1</Type>
  <Type>2</Type>
  <Type>3</Type>
</Order>

现在验证应该是,Order 段中总是应该有值为 1 的 Type 段,如果没有,则应该发生验证错误.我尝试了一些验证,但无法得到正确的结果.有没有人想过如何实现这一点,是否有可能实现?

Now the validation should be, that there always should be segment Type with value 1 in the segment Order, if not, a validation error should occur. I tried some validations, but could not get the correct result. Has someone thought on how to implement this and if this is even possible?

推荐答案

XSD 1.0

您的约束无法在 XSD 1.0 中表达.

XSD 1.0

Your constraint cannot be expressed in XSD 1.0.

您的约束可以在 XSD 1.1 中使用断言来表达,声明至少有一个 OrderType 子元素具有1 的值:

Your constraint can be expressed in XSD 1.1 using an assertion to state that there be at least one Type child of Order that has a value of 1:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1"> 
  <xs:element name="Order">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Type" maxOccurs="unbounded" type="xs:integer"/>
      </xs:sequence>
      <xs:assert test="Type = 1"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

这篇关于如何强制子元素在 XSD 中具有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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