如何在XSD中为复合模式建模

问题描述

我正在尝试找出如何对特定类型的序列化进行建模,以便针对该模式(使用Saxon)验证对模式敏感的XSLT。

一个简单的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>
,

我想说这是替代组的好用例。

定义一个名为SHAPE的抽象元素声明,将SQUARE和TRIANGLE定义为SHAPE替换组的成员,然后在要允许SQUARE或TRIANGLE出现的位置的内容模型中使用SHAPE。然后,当您引入其他形状时,只需要将它们添加到替换组中即可。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...