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 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:
一些类似的问题:
- can the length of two xml lists be defined as being required to be equal?
- Restrict Element Values Based on Attribute
- How to use attribute value as a discriminator for XML polymorphic type selection?
- How to Validate in XSD the xml node value against it’s neighbor xml node value
可以将两个xml列表的长度定义为必须相等吗?
基于属性限制元素值
如何使用属性值作为XML多态类型选择的鉴别器?
如何在XSD中验证xml节点值对其邻居xml节点值的影响
#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:
一些类似的问题:
- can the length of two xml lists be defined as being required to be equal?
- Restrict Element Values Based on Attribute
- How to use attribute value as a discriminator for XML polymorphic type selection?
- How to Validate in XSD the xml node value against it’s neighbor xml node value
可以将两个xml列表的长度定义为必须相等吗?
基于属性限制元素值
如何使用属性值作为XML多态类型选择的鉴别器?
如何在XSD中验证xml节点值对其邻居xml节点值的影响
#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
并且需要或不需要这个组