gpt4 book ai didi

XML 架构 : Can I make some of an attribute's values be required but still allow other values?

转载 作者:数据小太阳 更新时间:2023-10-29 01:45:23 25 4
gpt4 key购买 nike

(注意:我无法更改收到的 XML 的结构。我只能更改验证它的方式。)

假设我可以这样获取 XML:

<Address Field="Street" Value="123 Main"/>
<Address Field="StreetPartTwo" Value="Unit B"/>
<Address Field="State" Value="CO"/>
<Address Field="Zip" Value="80020"/>
<Address Field="SomeOtherCrazyValue" Value="Foo"/>

我需要创建一个 XSD 架构来验证“街道”、“州”和“ zip ”必须存在。但我不在乎“StreetPartTwo”和/或“SomeOtherCrazyValue”是否恰好也存在。

如果我知道只有我关心的三个可以被包含(并且每个只会被包含一次),我可以做这样的事情:

<xs:element name="Address" type="addressType" maxOccurs="unbounded" minOccurs="3"/>

<xs:complexType name="addressType">
<xs:attribute name="Field" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Street"/>
<xs:enumeration value="State"/>
<xs:enumeration value="Zip"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>

但这不适用于我的情况,因为我可能还会收到我不关心的那些其他 Address 元素(它们也具有“Field”属性)。

我有什么想法可以确保我关心的东西存在,但也让其他东西进来吗?

TIA!肖恩

最佳答案

您无法仅使用 XML 架构来进行您寻求的验证。

根据 "XML Schema Part 1: Structures" specification ...

When two or more particles contained directly or indirectly in the {particles} of a model group have identically named element declarations as their {term}, the type definitions of those declarations must be the same.

这并不是说您不能构建将验证正确文档的模式。这意味着,您不能构建一个无法验证某些不正确文档的模式。当我说“不正确”时,我指的是违反您用英语陈述的限制的文件。

例如,假设您有一个包含三个 Street 元素的文档,如下所示:

<Address Field="Street" Value="123 Main"/> 
<Address Field="Street" Value="456 Main"/>
<Address Field="Street" Value="789 Main"/>
<Address Field="SomeOtherCrazyValue" Value="Foo"/>

根据您的架构,该文档是一个有效地址。可以向您的模式添加 xs:unique 约束,以便它拒绝此类损坏的文档。但即使使用 xs:unique,针对此类模式进行验证也会声明其他一些不正确的文档是有效的——例如包含三个 <Address> 元素的文档,每个元素都具有唯一的 Field 属性,但没有一个具有 Field="Zip"

事实上,不可能生成正式编纂您声明的约束的 W3C XML 架构。 <xs:all> 元素几乎 使您到达那里,但它仅适用于元素,不适用于属性。而且,它不能与扩展一起使用,所以在 W3C XML Schema 中,您不能说“所有这些元素以任何顺序加上任何其他元素”。


为了执行您寻求的验证,您的选择是:

  1. 依赖 XML Schema 以外的东西,
  2. 分多个步骤执行验证,第一步使用 XML 模式,第二步使用其他方法。

对于第一个选项,我认为您可以使用 Relax NG 来完成。缺点是,它不是标准,据我所知,它既没有得到广泛支持,也没有得到广泛支持。这就像为了表达思想而学习盖尔语。盖尔语没有错,但它有点像语言的死胡同和 I think RelaxNG is, too

对于第二个选项,一种方法是根据您的架构进行验证作为第一步,然后作为第二步:

一个。应用 XSL 转换,将 <Address> 元素转换为以其 Field 属性的值命名的元素。该转换的输出将如下所示:

<root>
<Street Value="101 Bellavista Drive"/>
<State Value="Confusion"/>
<Zip Value="10101"/>
</root>

B.针对不同的模式验证该转换的输出,它看起来像这样:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:all>
<xs:element maxOccurs="1" minOccurs="1" ref="Street" />
<xs:element maxOccurs="1" minOccurs="1" ref="State" />
<xs:element maxOccurs="1" minOccurs="1" ref="Zip" />
</xs:all>
</xs:complexType>
</xs:element>

<xs:element name="Street">
<xs:complexType>
<xs:attribute name="Value" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="State">
<xs:complexType>
<xs:attribute name="Value" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="Zip">
<xs:complexType>
<xs:attribute name="Value" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>

</xs:schema>

您需要扩展该架构以处理转换输出中的其他元素,如 <SomeOtherCrazyValue>。或者您可以将 xsl 转换构造为不发出不属于 {State,Street,Zip} 之一的元素。

需要说明的是,我知道您无法更改收到的 XML。这种方法不需要那样做。它只是使用一种时髦的两步验证方法。一旦第二个验证步骤完成,您就可以丢弃转换的结果。


编辑 - 实际上,肖恩,再次考虑这个问题,您可以只使用步骤 B。假设您的 XSL 转换仅从文档中删除不包含的 <Address> 元素Field 属性值有 State、Street 或 Zip。换句话说,不会有 <Address Field="SomeOtherCrazyValue"...> 。该转换的结果可以使用您的模式进行验证,使用 maxOccurs="3"、minOccurs="3"和 xs:unique。

关于XML 架构 : Can I make some of an attribute's values be required but still allow other values?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2777352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com