【发布时间】:2013-08-23 20:13:05
【问题描述】:
我有这个 xsd
<xs:schema version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="foo">
<xs:complexType>
<xs:choice>
<xs:element name="bar"
minOccurs="0"
maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id"
use="required"
type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name="batz"
minOccurs="0"
maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="idref"
use="required"
type="xs:string" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:key name="ID">
<xs:selector xpath="./bar" />
<xs:field xpath="@id" />
</xs:key>
<xs:keyref name="IDREF" refer="ID">
<xs:selector xpath="./batz" />
<xs:field xpath="@idref" />
</xs:keyref>
</xs:element>
</xs:schema>
我有这两个 xml 使用这个 xsd 作为验证:
首先
<?xml version="1.0" encoding="UTF-8"?>
<foo xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:noNamespaceSchemaLocation=
'file:/home/tareq/NetBeansProjects/HTML5Application/public_html/refTest.xsd'>
<bar id="1"/>
<bar id="2"/>
</foo>
秒:
<?xml version="1.0" encoding="UTF-8"?>
<foo xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:noNamespaceSchemaLocation =
'file:/home/tareq/NetBeansProjects/HTML5Application/public_html/refTest.xsd'>
<batz idref="1" /> <!-- this should succeed because <bar id="1"> exists -->
<batz idref="3" /> <!-- this should FAIL -->
</foo>
当我用sequence 标记替换choice 标记并将两个xml 写入一个xml 时,验证显示错误并正常工作。
问题出现在这个 xsd 中,我的意思是两个 xml 彼此之间不能是 ref/keyref。
这就是我现在所面临的,也是我从 3 天以来一直在努力做的事情。
【问题讨论】: