首页 > 解决方案 > 如何在 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 菜鸟,我看过“类型”和“替代”,但没有得到任何明智的东西。

标签: xmlxsdsaxon

解决方案


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>

推荐阅读