- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
(注意:我无法更改收到的 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 中,您不能说“所有这些元素以任何顺序加上任何其他元素”。
为了执行您寻求的验证,您的选择是:
对于第一个选项,我认为您可以使用 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/
关于 this页面,我看到以下代码: if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) 但我不明白为什么会变成这样。
函数pthread_mutex_init允许您指定指向属性的指针。但是我还没有找到关于pthread属性是什么的很好的解释。我一直只是提供NULL。这个论点有用吗? 该文档,对于那些忘记它的人: PT
我们有一个 xml 节点“item”,其属性为“style”,即“Header1”。但是,这种风格可以改变。我们有一个名为 Header1 的属性集,它定义了它在 PDF 中的外观,通过 xsl:fo
我的任务是在用户点击它时从输入框中删除占位符并使标签可见。如果用户未在其中再次填写任何内容,请放回占位符并使标签不可见。 我可以隐藏它但不能重新分配它。我试过 element.setAttribute
我从文章中编写代码,并且有: public IActionResult Create([Bind(Include="Imie,Nazwisko,Stanowisko,Wiek")] Pracownik
你能给我解释一下以下属性吗? 1) [MonoTouch.Foundation.Register("SomeClass")] 这个属性是否只用于向IB注册类?以编程方式扩展 iOS 类时是否必须使用此
我正在编写一个 C++ 程序,在调试时我在执行以下函数: int CClass::do_something() { ... // I've put a breakpoint here } 我的 C
我已经在 polymer 0.5 中构建了我的应用程序。 现在我已经将它更新到 polymer 1.0。 对于响应式布局,我使用了一个布局属性,它使用 Polymer 0.5 中布局属性的自定义逻辑。
我是使用 Jade 的新手——到目前为止它很棒。 但是我需要发生的一件事是具有“itemscope”属性的元素: 我的 Jade 符是: header(itemscope, itemtype='ht
我正在研究一个厨师实现,有时在过去的地方使用了 attribute.set,attribute.default 会这样做。为了解决这个问题,我对 Chef 属性优先范式非常熟悉。我知道“正常”属性(使
我经常看到html data-attribute (s) 将特定值/参数添加到 html 元素,例如使用它们将按钮“链接”到要打开的模式对话框等的 Bootstrap。 现在,我看到一个几乎著名的
假设如下: def create_new_salt self.salt = self.object_id.to_s + rand.to_s end 为什么使用“ self ”更好。而不是实例变量“
根据我的理解,Backbone.js 模型的属性应该通过以下方式声明为有点私有(private)的成员变量 this.set({ attributeName: attributeValue }) //
我有一个看起来像下面的XML文档: ... ... ... ...
我正在实现一个 JSF 组件,需要有条件地添加一些属性。这个问题类似于之前的 JSF: p:dataTable with f:attribute results in "argument type m
我正在尝试将应用程序发布到 Android 电子市场,但出现以下错误: W/ResourceType(16964): No known package when getting value for r
抱歉这么具体的应用程序,但我注意到另一篇关于 Maya 开发的回答很好的帖子。 我刚刚为 Maya 编写了一个插件节点。它只是根据湍流函数杀死一堆粒子。湍流由许多可在属性编辑器中调整的属性驱动。 在属
我在 html 元素中的数据属性为 Update .它具有数据属性的 bool 值。 跟下面的元素Update有什么区别吗?因为数据属性用双引号引起来。 html是否支持 bool 值? 最佳答案 b
我正在尝试为企业库 5.0 的异常处理 block 创建自定义异常处理程序。据我了解,我需要使用属性开始上课“[ConfigurationElementType(typeof(CustomHandle
我找不到这两个选择器之间的区别。两者似乎都做同样的事情,即根据包含给定字符串的特定属性值选择标签。 对于 [attribute~=value] :http://www.w3schools.com/cs
我是一名优秀的程序员,十分优秀!