xml - 如何在 XSD 中为复合模式建模
问题描述
我正在尝试研究如何对特定样式的序列化进行建模,以便针对模式验证模式感知 XSLT(使用 Saxon)。
xml 的一个简单示例是
<?xml version="1.0" encoding="UTF-8"?>
<rootShape>
<SQUARE width="10" x="1" y="25">
<contains>
<TRIANGLE rotation="180" x="1" y="34">
<contains>
<TRIANGLE rotation="180" x="221" y="34">
<contains/>
</TRIANGLE>
<SQUARE width="10" x="1" y="25">
<contains/>
</SQUARE>
</contains>
</TRIANGLE>
</contains>
</SQUARE>
</rootShape>
序列化的“风格”以大写形式导出对象“类型”,然后以小写形式导出对象的属性/字段。叶类型(例如字符串、整数、日期时间等)被建模为属性,而对其他对象/值的引用递归地遵循相同的模式。
所以...我可以很容易地针对其中的一些编写一个简单的 XSD。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="SQUARE">
<xs:complexType>
<xs:sequence>
<xs:element name="contains"/>
</xs:sequence>
<xs:attribute name="width" type="xs:int"/>
<xs:attribute name="x" type="xs:int"/>
<xs:attribute name="y" type="xs:int"/>
</xs:complexType>
</xs:element>
<xs:element name="TRIANGLE">
<xs:complexType>
<xs:sequence>
<xs:element name="contains"/>
</xs:sequence>
<xs:attribute name="rotation" type="xs:int"/>
<xs:attribute name="x" type="xs:int"/>
<xs:attribute name="y" type="xs:int"/>
</xs:complexType>
</xs:element>
<xs:element name="rootShape">
<xs:complexType>
<xs:choice>
<xs:element ref="SQUARE"/>
<xs:element ref="TRIANGLE"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
(这验证成功)但我有点卡住如何处理“包含”关系。
关系的基数是 0 -> 无界,但基数与特定元素相关联,我想将基数应用于不同的选择/替代方案(即,就像联合类型)。
有任何想法吗?我是一个 xsd 菜鸟,我看过“类型”和“替代”,但没有得到任何明智的东西。
解决方案
achchc,您可以将基数约束应用于选择....愚蠢的我。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="SQUARE">
<xs:complexType>
<xs:sequence>
<xs:element name="contains">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="SQUARE"/>
<xs:element ref="TRIANGLE"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="width" type="xs:int"/>
<xs:attribute name="x" type="xs:int"/>
<xs:attribute name="y" type="xs:int"/>
</xs:complexType>
</xs:element>
<xs:element name="TRIANGLE">
<xs:complexType>
<xs:sequence>
<xs:element name="contains">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="SQUARE"/>
<xs:element ref="TRIANGLE"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="rotation" type="xs:int"/>
<xs:attribute name="x" type="xs:int"/>
<xs:attribute name="y" type="xs:int"/>
</xs:complexType>
</xs:element>
<xs:element name="rootShape">
<xs:complexType>
<xs:choice>
<xs:element ref="SQUARE"/>
<xs:element ref="TRIANGLE"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
推荐阅读
- java - 在桌面java上获取设备像素比
- c# - Asp.Net Core Swagger / FromForm ///(三斜杠)评论没有被拾起?
- qt - 使用 QAbstractItemModel 作为 C++ 中的 Q_PROPERTY 和 READ WRITE
- angular - 有没有办法将验证器设置为表单数组中的字段?
- powershell - 从 powershell 脚本调用 Azure Cosmos DB UDF 函数
- python - 由于未找到 settings.py,我的 Django 站点未在 Elasticbeanstalk 中加载
- netsuite - != 工作流条件中的语句?
- sql - SSIS ForEach ADO 枚举器 - 性能问题
- python - 如何让数据框对多列执行 SQL groupby 操作?
- r - 在转换 POSIXct 的时区然后分别提取日期/时间时遇到问题