XSD中的条件必需元素 [英] Conditional required elements in an XSD

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

问题描述

我需要一个XML模式,该模式可以接受客户或客户ID的请求。如果客户ID为0,则我们需要客户数据;如果客户ID> 0,则客户数据是可选的(他们可以提交附件)。我的XSD目前看起来像这样;



 < xs:complexType> 


< xs:any>
< xs:element name = UserID type = xs:string minOccurs = 1 />
< xs:element name =密码 type = xs:string minOccurs = 1 />
< xs:element name = ReferenceNumber type = xs:string minOccurs = 0 maxOccurs = 1 />
< xs:element name = CustomerId minOccurs = 1 maxOccurs = 1 type = xs:integer />
< xs:element ref = WhereSigned />
< xs:element name = ContactName type = xs:string minOccurs = 0 maxOccurs = 1 />
< xs:element name = ContactTelephone type = xs:string minOccurs = 0 maxOccurs = 1 />
< xs:element name = ContactFax type = xs:string minOccurs = 0 maxOccurs = 1 />
< xs:element name = ContactEmail type = xs:string minOccurs = 0 maxOccurs = 1 />
< xs:element ref = MainApplicant maxOccurs = 1 />
< xs:element ref = JointApplicant minOccurs = 0 maxOccurs = 1 />
< xs:element ref =资产 minOccurs = 0 maxOccurs = 1 />
< / xs:any>


< / xs:complexType>



我需要一些类似的东西这个...(我希望这很简单,可惜不是。)



 < xs:complexType> 
< xs:choice>

< xs:any>
< xs:element name = UserID type = xs:string minOccurs = 1 />
< xs:element name =密码 type = xs:string minOccurs = 1 />
< xs:element name = ReferenceNumber type = xs:string minOccurs = 0 maxOccurs = 1 />
< xs:element name = CustomerId minOccurs = 1 maxOccurs = 1>
< xs:simpleType>
< xs:restriction base = xs:integer>
< xs:minInclusive value = 0 />
< xs:maxInclusive value = 0 />
< / xs:restriction>
< / xs:simpleType>
< / xs:element>
< xs:element ref = WhereSigned />
< xs:element name = ContactName type = xs:string minOccurs = 0 maxOccurs = 1 />
< xs:element name = ContactTelephone type = xs:string minOccurs = 0 maxOccurs = 1 />
< xs:element name = ContactFax type = xs:string minOccurs = 0 maxOccurs = 1 />
< xs:element name = ContactEmail type = xs:string minOccurs = 0 maxOccurs = 1 />
< xs:element ref = MainApplicant maxOccurs = 1 />
< xs:element ref = JointApplicant minOccurs = 0 maxOccurs = 1 />
< xs:element ref =资产 minOccurs = 0 maxOccurs = 1 />
< / xs:any>

< xs:any>
< xs:element name = UserID type = xs:string minOccurs = 1 />
< xs:element name =密码 type = xs:string minOccurs = 1 />
< xs:element name = ReferenceNumber type = xs:string minOccurs = 0 maxOccurs = 1 />
< xs:element name = CustomerId minOccurs = 1 maxOccurs = 1>
< xs:simpleType>
< xs:restriction base = xs:integer>
< xs:minInclusive value = 1 />
< / xs:restriction>
< / xs:simpleType>
< / xs:element>
< xs:element ref =资产 minOccurs = 0 maxOccurs = 1 />
< / xs:any>

< / xs:choice>
< / xs:complexType>



任何XSD专家(执行这些操作甚至有存在?!)谁能够伸出援手或提出建议?下面是一个简化的示例,它假定CustomerId等于零时,必须使用MainApplicant和WhereSigned。

 <?xml version = 1.0 ; encoding = utf-8 ?> 
<!-由QTAssistant / XSD模块(http://www.paschidev.com)生成的XML模式->
< xsd:schema elementFormDefault =" qualified" xmlns:xsd = http://www.w3.org/2001/XMLSchema xmlns:xtm = http://paschidev.com/schemas/metadata/xtm>
< xsd:complexType name ='Request'>
< xsd:all>
< xsd:element name =" UserID" type = xsd:string />
< xsd:element name ='Password' type = xsd:string />
< xsd:element name = ReferenceNumber type = xsd:string minOccurs = 0 /。
< xsd:element name = CustomerId type = xsd:integer />
< xsd:element ref = WhereSigned; minOccurs = 0 /。
< xsd:element name =" ContactName" type = xsd:string minOccurs = 0。 />
< xsd:element name =" ContactTelephone" type = xsd:string minOccurs = 0。 />
< xsd:element name =" ContactFax" type = xsd:string minOccurs = 0。 />
< xsd:element name =" ContactEmail" type = xsd:string minOccurs = 0。 />
< xsd:element ref = MainApplicant; minOccurs = 0。 />
< xsd:element ref = JointApplicant; minOccurs = 0。 />
< xsd:element ref = Asset minOccurs = 0。 />
< / xsd:all>
< xsd:assert test =" CustomerId [。 eq 0]以及WhereSigned和MainApplicant或CustomerId [。 ne 0] />
< / xsd:complexType>
< xsd:element name = Asset />
< xsd:element name =" MainApplicant" />
< xsd:element name =" JointApplicant" />
< xsd:element name =  WhereSigned" />
< / xsd:schema>

当CustomerId为零时的最小有效XML:

 < root xmlns:xsi =" http://www.w3.org/2001/XMLSchema-instance" xsi:type =请求> 
< UserID> UserID1< / UserID>
< Password> asodasqZX ==< / Password>
< CustomerId> 0< / CustomerId>
< WhereSigned />
< MainApplicant />
< / root>

如果CustomerId不为零,则这是最小XML:

 <根xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance xsi:type =请求> 
< UserID> UserID1< / UserID>
< Password> asodasqZX ==< / Password>
< CustomerId> 1< / CustomerId>
< / root>

可以容易地控制条件(请参见@test表达式);根据需要添加常规XSD约束(例如,如果您的CustomerId需要大于或等于零,则将其设置为nonNegativeInteger等)


I have a requirement for an xml schema which accepts either a request with the customer or with the customer id. If the customer Id is 0 then we need the customer data, if it's > 0 then customer data is optional (They can submit ammendements). My XSD looks like this at the moment;

<xs:complexType>


    <xs:any>
      <xs:element name="UserID" type="xs:string" minOccurs="1"/>
      <xs:element name="Password" type="xs:string" minOccurs="1"/>
      <xs:element name="ReferenceNumber" type="xs:string" minOccurs="0"  maxOccurs="1"/>
      <xs:element name="CustomerId" minOccurs="1" maxOccurs="1" type="xs:integer"/>
      <xs:element ref="WhereSigned"/>
      <xs:element name="ContactName" type="xs:string" minOccurs="0"  maxOccurs="1"/>
      <xs:element name="ContactTelephone" type="xs:string" minOccurs="0"  maxOccurs="1"/>
      <xs:element name="ContactFax" type="xs:string" minOccurs="0"  maxOccurs="1"/>
      <xs:element name="ContactEmail" type="xs:string" minOccurs="0"  maxOccurs="1"/>
      <xs:element ref="MainApplicant"  maxOccurs="1"/>
      <xs:element ref="JointApplicant" minOccurs="0" maxOccurs="1"/>
      <xs:element ref="Asset" minOccurs="0" maxOccurs="1"/>
    </xs:any>


</xs:complexType>

I need something a bit like this... (I wish it were this simple but alas not..)

<xs:complexType>
  <xs:choice>

    <xs:any>
      <xs:element name="UserID" type="xs:string" minOccurs="1"/>
      <xs:element name="Password" type="xs:string" minOccurs="1"/>
      <xs:element name="ReferenceNumber" type="xs:string" minOccurs="0"  maxOccurs="1"/>
      <xs:element name="CustomerId" minOccurs="1" maxOccurs="1">
        <xs:simpleType>
          <xs:restriction base="xs:integer">
            <xs:minInclusive value="0"/>
            <xs:maxInclusive value="0"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element ref="WhereSigned"/>
      <xs:element name="ContactName" type="xs:string" minOccurs="0"  maxOccurs="1"/>
      <xs:element name="ContactTelephone" type="xs:string" minOccurs="0"  maxOccurs="1"/>
      <xs:element name="ContactFax" type="xs:string" minOccurs="0"  maxOccurs="1"/>
      <xs:element name="ContactEmail" type="xs:string" minOccurs="0"  maxOccurs="1"/>
      <xs:element ref="MainApplicant"  maxOccurs="1"/>
      <xs:element ref="JointApplicant" minOccurs="0" maxOccurs="1"/>
      <xs:element ref="Asset" minOccurs="0" maxOccurs="1"/>
    </xs:any>

    <xs:any>
      <xs:element name="UserID" type="xs:string" minOccurs="1"/>
      <xs:element name="Password" type="xs:string" minOccurs="1"/>
      <xs:element name="ReferenceNumber" type="xs:string" minOccurs="0"  maxOccurs="1"/>
      <xs:element name="CustomerId" minOccurs="1" maxOccurs="1">
        <xs:simpleType>
          <xs:restriction base="xs:integer">
            <xs:minInclusive value="1"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element ref="Asset" minOccurs="0" maxOccurs="1"/>
    </xs:any>

   </xs:choice>
</xs:complexType>

Any XSD experts (do these even exist?!) who are able to lend a hand or some advice?

解决方案

This is possible in XSD 1.1; below is a simplified example, which assumes MainApplicant and WhereSigned are mandatory when CustomerId is equal to zero.

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xtm="http://paschidev.com/schemas/metadata/xtm">
    <xsd:complexType name="Request">
        <xsd:all>
            <xsd:element name="UserID" type="xsd:string" />
            <xsd:element name="Password" type="xsd:string" />
            <xsd:element name="ReferenceNumber" type="xsd:string" minOccurs="0"/>
            <xsd:element name="CustomerId" type="xsd:integer"/>
            <xsd:element ref="WhereSigned" minOccurs="0"/>
            <xsd:element name="ContactName" type="xsd:string" minOccurs="0" />
            <xsd:element name="ContactTelephone" type="xsd:string" minOccurs="0" />
            <xsd:element name="ContactFax" type="xsd:string" minOccurs="0" />
            <xsd:element name="ContactEmail" type="xsd:string" minOccurs="0" />
            <xsd:element ref="MainApplicant" minOccurs="0" />
            <xsd:element ref="JointApplicant" minOccurs="0" />
            <xsd:element ref="Asset" minOccurs="0" />
        </xsd:all>
        <xsd:assert test="CustomerId[. eq 0] and WhereSigned and MainApplicant or CustomerId[. ne 0]" />
    </xsd:complexType>
    <xsd:element name="Asset"/>
    <xsd:element name="MainApplicant" />
    <xsd:element name="JointApplicant" />
    <xsd:element name="WhereSigned" />  
</xsd:schema>

Minimum valid XML, when CustomerId is zero:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Request">
    <UserID>UserID1</UserID>
    <Password>asodasqZX==</Password>
    <CustomerId>0</CustomerId>
    <WhereSigned/>
    <MainApplicant/>
</root>

When CustomerId is non-zero, then this is the minimum XML:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Request">
    <UserID>UserID1</UserID>
    <Password>asodasqZX==</Password>
    <CustomerId>1</CustomerId>
</root>

The conditional can be easily controlled (see the @test expression); add regular XSD constraints as needed (e.g. if your CustomerId needs to be greater than or equal to zero, then make it nonNegativeInteger, etc.)

这篇关于XSD中的条件必需元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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