问题描述
<?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,它试图描述,约束并最终用于模式识别型XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
<xs:complexType name="SQUARETYPE">
<xs:sequence>
<xs:element name="contains">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="SQUARE">
<xs:alternative test="@kind='FILLEDSQUARETYPE'" type="FILLEDSQUARETYPE"/>
<xs:alternative test="@kind='SQUARETYPE'" type="SQUARETYPE"/>
</xs:element>
<xs:element name="TRIANGLE" type="TRIANGLETYPE"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="kind" type="xs:string"/>
<xs:attribute name="width" type="xs:int"/>
<xs:attribute name="x" type="xs:int"/>
<xs:attribute name="y" type="xs:int"/>
</xs:complexType>
<xs:complexType name="FILLEDSQUARETYPE">
<xs:sequence>
<xs:element name="contains">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="SQUARE">
<xs:alternative test="@kind='FILLEDSQUARETYPE'" type="FILLEDSQUARETYPE"/>
<xs:alternative test="@kind='SQUARETYPE'" type="SQUARETYPE"/>
</xs:element>
<xs:element name="TRIANGLE" type="TRIANGLETYPE"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="kind" type="xs:string"/>
<xs:attribute name="colour" type="xs:string"/>
<xs:attribute name="width" type="xs:int"/>
<xs:attribute name="x" type="xs:int"/>
<xs:attribute name="y" type="xs:int"/>
</xs:complexType>
<xs:complexType name="TRIANGLETYPE">
<xs:sequence>
<xs:element name="contains">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="SQUARE">
<xs:alternative test="@kind='FILLEDSQUARETYPE'" type="FILLEDSQUARETYPE"/>
<xs:alternative test="@kind='SQUARETYPE'" type="SQUARETYPE"/>
</xs:element>
<xs:element name="TRIANGLE" type="TRIANGLETYPE"/>
</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 name="rootShape">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="SQUARE">
<xs:alternative test="@kind='FILLEDSQUARETYPE'" type="FILLEDSQUARETYPE"/>
<xs:alternative test="@kind='SQUARETYPE'" type="SQUARETYPE"/>
</xs:element>
<xs:element name="TRIANGLE" type="TRIANGLETYPE"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
XML会针对XSLT进行验证,但是我不确定为什么。
关键是“ FILLEDSQUARETYPE”,“ SQUARETYPE”和“ TRIANGLETYPE”联合类型的定义
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="SQUARE">
<xs:alternative test="@kind='FILLEDSQUARETYPE'" type="FILLEDSQUARETYPE"/>
<xs:alternative test="@kind='SQUARETYPE'" type="SQUARETYPE"/>
</xs:element>
<xs:element name="TRIANGLE" type="TRIANGLETYPE"/>
</xs:choice>
试图根据元素“ SQUARE”的某些属性来推断其类型。
但是我的“ SQUARE”元素不包含“ kind”属性,所以请不要居住任何一种。
所以...我有点困惑,它推断什么类型?
choice构造对元素的可能性强加了一个集合封闭,alternative构造似乎没有做同样的事情?我不需要一个开放的定义,我想要一个封闭的定义。
我确实想知道是否会关闭它
<xs:element name="SQUARE">
<xs:alternative test="@kind='FILLEDSQUARETYPE'" type="FILLEDSQUARETYPE"/>
<xs:alternative type="SQUARETYPE"/>
</xs:element>
即一个正方形或者是FILLEDSQUARETYPE,或者如果不是,则必须是SQUARETYPE,所以然后查看此类型的定义,它必须具有kind属性,如果没有,那么这就是一个错误?...但这不是什么似乎还在继续。
解决方法
如果没有其他选择满足,则类型默认为包含元素声明中声明的类型,其本身默认为xs:anyType
。
如果您想在@kind
不属于所列列表之一时验证失败,请添加一个枚举构面来约束@kind
的值。
基于迈克尔·凯(Michael Kay)的评价
解决方案是
<xs:element name="SQUARE">
<xs:alternative test="@kind = 'FILLEDSQUARETYPE'" type="FILLEDSQUARETYPE"/>
<xs:alternative test="@kind = 'SQUARETYPE'" type="SQUARETYPE"/>
<xs:alternative type="xs:error"/>
</xs:element>
似乎为我工作。