XSD:同名<元素>的随机顺序不同类型=不可能元素>
对此进行了搜索、实验、研究和绞尽脑汁(昨晚梦见了)。
尝试构建 XSD 架构,以根据标签值的标签约束来验证以下示例 XML。
<data>
<dataSet>
<title>mediaType</title>
<value>FullLength</value>
</dataSet>
<dataSet>
<title>available</title>
<value>true</value>
</dataSet>
<dataSet>
<title>country</title>
<value>Canada</value>
</dataSet>
</data>
关于单个 dataSet 的约束的模式
<xs:complexType name="typeAvailable">
<xs:sequence>
<xs:element name="title">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="available" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="value">
<xs:simpleType>
<xs:restriction base="xs:boolean" />
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="typeMediaType">
<xs:sequence>
<xs:element name="title">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="mediaType" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="value">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="FullLength|Clip" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="typeCountry">
<xs:sequence>
<xs:element name="title">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="typeCountry" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="value">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="Canada|US" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
我能想到的最好的方案在 dataSet 乱序时不会验证(它们将会乱序)
<xs:complexType name="typeData">
<xs:all>
<xs:element name="dataSet" type="typeMediaType" />
<xs:element name="dataSet" type="typeAvailable" />
<xs:element name="dataSet" type="typeCountry" />
</xs:all>
</xs:complexType>
当然,我坚持我得到的数据,但没有人说我不能用 XSLT 将它转换成什么,我不知道。我希望有一个优雅的 XSD 解决方案,唉,我担心这是不可能的。
谁能证明我错了?这是我很长一段时间以来的第一个模式项目。
更新
你知道吗,我想我要验证它的结构,阅读<数据> 无论如何,
<data>
<mediaType>FullLength</mediaType>
<available>true</available>
<country>Canada</country>
</data>
我可能应该这样做,因为验证后的数据在
Searched and experimented, researched and racked my brain about this (dreamt about it last night).
Trying to build an XSD schema to validate the following example XML based on constraints of the tag depending on the value of the tag.
<data>
<dataSet>
<title>mediaType</title>
<value>FullLength</value>
</dataSet>
<dataSet>
<title>available</title>
<value>true</value>
</dataSet>
<dataSet>
<title>country</title>
<value>Canada</value>
</dataSet>
</data>
the schema regarding constraints on the individual dataSet 's
<xs:complexType name="typeAvailable">
<xs:sequence>
<xs:element name="title">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="available" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="value">
<xs:simpleType>
<xs:restriction base="xs:boolean" />
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="typeMediaType">
<xs:sequence>
<xs:element name="title">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="mediaType" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="value">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="FullLength|Clip" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="typeCountry">
<xs:sequence>
<xs:element name="title">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="typeCountry" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="value">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="Canada|US" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
The best I could come up with doesn't validate when the dataSet 's are out of order (which they will be)
<xs:complexType name="typeData">
<xs:all>
<xs:element name="dataSet" type="typeMediaType" />
<xs:element name="dataSet" type="typeAvailable" />
<xs:element name="dataSet" type="typeCountry" />
</xs:all>
</xs:complexType>
Of course, I'm stuck with the data that I get, but nothing says I can't transform it with XSLT -- into what, I don't know. I was hoping for an elegant XSD solution, alas, I fear this is impossible.
Anyone can prove me wrong? This is my first schema project in quite a long time.
UPDATE
You know what, I think I'm going to validate the structure of it, read the <data> portion w/ LINQ and transform it via XSLT to
<data>
<mediaType>FullLength</mediaType>
<available>true</available>
<country>Canada</country>
</data>
It probably behooves me to do this anyway, as working w/ the data after validation was going to be janky with it in <title><value> pairs any how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XML 模式无法实现“条件”约束。按键的技巧很少,但也很有限。考虑使用 Schematron,它具有更丰富的验证功能。
一般来说,XML Schema更多的是关于结构和类型,它的约束能力非常有限。
"Conditional" constraints are not possible with XML Schema. There are few tricks with keys, but they are very limited. Consider using Schematron, it has much richer validation capablities.
Generally, XML Schema is more about structure and typing, its constraining capabilities are very limited.
元素声明一致不允许具有不同类型的同名元素a>约束。
Same-named elements with different types is disallowed by the Element Declarations Consistent constraint.