我有以下XML文件:
<?xml version="1.0" encoding="UTF-8" ?>
<catalog>
<chainList>
<chain chainName="Chain">
<handlerList>
<handler handlerName="This is a name">com.lorescianatico.chain.handler.DummyHandler</handler>
<handler handlerName="This is another name">com.lorescianatico.chain.handler.AnotherDummyHandler</handler>
</handlerList>
</chain>
<chain chainName="AnotherChain">
<handlerList>
<handler handlerName="This is a name">com.lorescianatico.chain.handler.DummyHandler</handler>
<handler handlerName="This is another name">com.lorescianatico.chain.handler.AnotherDummyHandler</handler>
</handlerList>
</chain>
</chainList>
</catalog>
我需要使用XSD架构执行验证。我定义的模式如下:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element name="chainList" minOccurs="1" maxOccurs="1">
<xs:complexType >
<xs:sequence>
<xs:element name="chain" minOccurs="0" maxOccurs="unbounded" type="chain"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniqueChainName">
<xs:selector xpath="."/>
<xs:field xpath="chain/@chainName"/>
</xs:unique>
</xs:element>
</xs:sequence>
<xs:attribute name="catalogName" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:complexType name="chain">
<xs:sequence>
<xs:element name="handlerList" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="handler" minOccurs="1" maxOccurs="unbounded" type="handler"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniqueHandlerClass">
<xs:selector xpath="."/>
<xs:field xpath="handler"/>
</xs:unique>
<xs:unique name="uniqueHandlerName">
<xs:selector xpath="."/>
<xs:field xpath="handler/@handlerName"/>
</xs:unique>
</xs:element>
</xs:sequence>
<xs:attribute name="chainName" use="required" type="xs:string"/>
</xs:complexType>
<xs:complexType name="handler">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="handlerName" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
现在,如果使用我粘贴的模式测试此xml,则验证失败,因为我在不同的链中定义了相同的处理程序值。有没有办法定义唯一约束来检查值是否仅在子xml中是唯一的?就像用于定义唯一约束的XPath查询的范围一样?我希望属性handlerName和handler的值仅在同一个链中是唯一的。
提前致谢。
答案 0 :(得分:1)
有时你只需要停下来思考。几个小时后,我找到了解决方案:
<xs:unique name="uniqueHandlerName">
<xs:selector xpath="handler"/>
<xs:field xpath="@handlerName"/>
</xs:unique>
以这种方式写作,约束对我有用。