如何使用XSD排除XML文件中的枚举值?

问题描述:

是否可以指定标记或属性的值应该不是 some_value如何使用XSD排除XML文件中的枚举值?

我有一个奇怪的要求,其中xsd不知道发送给它的值。该特定标记的值可以是具有任何值的字符串,除了一个值(比如data_migration)。

如果发送了该特定值,应确认发件人有错误。

是否可以指定此限制?

我不是正则表达式专家,但是这个simpleType使得所有以data_migration开头的东西都无效。

<xs:simpleType name="notDataMigration"> 
    <xs:restriction base="xs:string"> 
    <xs:pattern value="^(?!data_migration).*" /> 
    </xs:restriction> 
</xs:simpleType> 

使用正则表达式来指定一个模式,或者在你的情况下,模式不应该包含。

http://www.w3schools.com/schema/schema_facets.asp

+0

你不能这样做使用正则表达式。 – xcut 2010-03-24 14:19:47

+0

请看上面由Jens Granlund选择的答案,相反...... – Yaneeve 2010-03-24 14:49:00

+0

我站在更正的位置,我不知道在模式方言中团体可能被否定。 – xcut 2010-03-24 15:04:44

我不知道你是否能明确排除的值。我不确定这是否有帮助,但您可以创建两个单独的枚举,然后创建枚举的联合。

<xsd:simpleType name="IncludedEnumType"> 
    <xsd:restriction base="xsd:string"> 
    <xsd:enumeration value="pending" /> 
    <xsd:enumeration value="in_process" /> 
    <xsd:enumeration value="failed" /> 
    <xsd:enumeration value="unknown" /> 
    </xsd:restriction> 
</xsd:simpleType> 

<xsd:simpleType name="ExcludedEnumType"> 
    <xsd:restriction base="xsd:string"> 
    <xsd:enumeration value="data_migration" /> 
    </xsd:restriction> 
</xsd:simpleType> 

<xsd:simpleType name="CombinedEnumType"> 
    <xsd:union memberTypes="IncludedEnumType ExcludedEnumType" /> 
</xsd:simpleType> 

你要么IncludedEnumTypeCombinedEnumType必要时使用。使用IncludedEnumType显然会排除ExcludedEnumType中的值。

此方法使用此article by IBM中的解决方案2。

+0

有用,但在这种情况下不;)所以+1。 – 2010-03-24 14:32:10