Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何在XSD中建模复合模式_Xml_Xsd_Saxon - Fatal编程技术网

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

Xml 如何在XSD中建模复合模式,xml,xsd,saxon,Xml,Xsd,Saxon,我正在尝试研究如何对特定样式的序列化进行建模,以便根据模式验证支持模式的XSLT(使用Saxon) xml的一个简单示例是 <?xml version="1.0" encoding="UTF-8"?> <rootShape> <SQUARE width="10" x="1" y="25"> <contains>

我正在尝试研究如何对特定样式的序列化进行建模,以便根据模式验证支持模式的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>

序列化的“样式”以大写形式导出对象“类型”,然后以小写形式导出对象的属性/字段。叶类型(例如string、int、datetime等)被建模为属性,而对其他对象/值的引用递归地遵循相同的模式

所以…我可以很容易地针对其中一些编写一个简单的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>

(这成功地验证了)但是我有点被困在如何处理“contains”关系中

关系的基数为0->unbounded,但基数与特定元素相关联,我希望将基数应用于不同的选择/备选方案(即,更像联合类型)

有什么想法吗?
我是一个xsd noob,我已经研究过“类型”和“备选方案”,但没有任何合理的选择。

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的抽象元素声明,将正方形和三角形定义为SHAPE替换组的成员,然后在内容模型中使用SHAPE,无论您希望在何处显示正方形或三角形。然后,当你引入更多的形状时,你只需要将它们添加到替换组。

所以这里的问题实际上是,这个模式将(有效地)从ER图自动生成,所以实际上我并不关心它有多简洁,只关心它的语义……我有点不明白。有一个新问题定义了一个模式,但使用了类型,因为serialiser中出现了一些可怕的“类型”替代褶皱。好消息是,一旦我了解了如何处理替代/选择的0/n关系,那么我几乎可以做我需要的一切。我想我可能会稍后再看替代组,这可能是一个合理的优化