XML Schema基于另一个来限制一个字段

时间:2021-08-07 17:18:08

I have the following schema, which I use to ensure that a person's PhoneNumber and PhoneNumberType (Home, Work, etc.) is not longer than 10 characters. However, I want to improve this schema so that PhoneNumberType is not required if a PhoneNumber is not provided, but is required if the PhoneNumber is provided. Is there a way to do this in XML Schema 1.0?

我有以下架构,我用它来确保一个人的PhoneNumber和PhoneNumberType(Home,Work等)不超过10个字符。但是,我想改进此架构,以便在未提供PhoneNumber时不需要PhoneNumberType,但如果提供了PhoneNumber则需要PhoneNumberType。有没有办法在XML Schema 1.0中执行此操作?

I am aware this could be accomplished in XML Schema 1.1 using <xs:assert/>, but unfortunately I am stuck with XML Schema 1.0.

我知道这可以使用 在XML Schema 1.1中完成,但不幸的是我坚持使用XML Schema 1.0。

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xs:element name="PhoneNumber">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:minLength value="0"/>
        <xs:maxLength value="10"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:element>
  <xs:element name="PhoneNumberType">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:minLength value="0"/>
        <xs:maxLength value="10"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:element>
</xsd:schema>

3 个解决方案

#1


2  

Sorry, XML Schema can't do that.

抱歉,XML Schema无法做到这一点。

Some similar questions:

一些类似的问题:

#2


0  

It seems to me that this is a "has" relationship.

在我看来,这是一种“有”关系。

If you have a PhoneNumber element, then it should have a property that is of type PhoneNumberType. Rather than messing around with validating and restrictions, I would suggest that you turn PhoneNumber into a complex element and make PhoneNumberType a required property of it.

如果您有PhoneNumber元素,那么它应该具有PhoneNumberType类型的属性。我建议你将PhoneNumber变成一个复杂的元素,并使PhoneNumberType成为它的必需属性,而不是搞乱验证和限制。

#3


0  

May be too late, but you can put them in group like this

可能为时已晚,但你可以将它们放在这样的组中

<xs:group name="group">
<xs:sequence>
    <xs:element ref="PhoneNumber"/>
    <xs:element ref="PhoneNymberType" />
</xs:sequence>

And make this group required or not

并且需要或不需要这个组

#1


2  

Sorry, XML Schema can't do that.

抱歉,XML Schema无法做到这一点。

Some similar questions:

一些类似的问题:

#2


0  

It seems to me that this is a "has" relationship.

在我看来,这是一种“有”关系。

If you have a PhoneNumber element, then it should have a property that is of type PhoneNumberType. Rather than messing around with validating and restrictions, I would suggest that you turn PhoneNumber into a complex element and make PhoneNumberType a required property of it.

如果您有PhoneNumber元素,那么它应该具有PhoneNumberType类型的属性。我建议你将PhoneNumber变成一个复杂的元素,并使PhoneNumberType成为它的必需属性,而不是搞乱验证和限制。

#3


0  

May be too late, but you can put them in group like this

可能为时已晚,但你可以将它们放在这样的组中

<xs:group name="group">
<xs:sequence>
    <xs:element ref="PhoneNumber"/>
    <xs:element ref="PhoneNymberType" />
</xs:sequence>

And make this group required or not

并且需要或不需要这个组