我在驗證XML模式的以下XML文件時遇到了問題。錯誤是「太多期望的項目'1','3'被提供,在聲明中我想表達如下:每當OID值等於BOID值,那麼A_Membership_Degree應該大於或等於B_Membership_Degree使用XPath eq操作符斷言的「項目太多」
示例XML
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="gen.xsd">
<A OID="AA" A_Membership_Degree="0.7" D_Membership_Degree="0.5" >
<A1>X1</A1>
<A2>D1</A2>
</A>
<B BOID="AA" B_Membership_Degree="0.2">
<B1>G1</B1>
<B2>F1</B2>
</B>
<C COID="AA" C_Membership_Degree="0.3" >
<C1>G2</C1>
<C2>F2</C2>
</C>
<A OID="AAA" A_Membership_Degree="0.8" D_Membership_Degree="0.5" >
<A1>X2</A1>
<A2>D2</A2>
</A>
<B BOID="AAA" B_Membership_Degree="0.5" >
<B1>G3</B1>
<B2>F3</B2>
</B>
<C COID="AAA" C_Membership_Degree="0.4" >
<C1>G4</C1>
<C2>F4</C2>
</C>
<A OID="HHH" A_Membership_Degree="0.8" D_Membership_Degree="0.5" >
<A1>X11</A1>
<A2>D11</A2>
</A>
</Document>
當前模式
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
vc:minVersion="1.1">
<xs:element name="Document">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element ref="A" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="B" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="C" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:assert
test="if(.//@OID eq .//@BOID)
then ./A/@A_Membership_Degree
ge ./B/@B_Membership_Degree else false()"/>
</xs:complexType>
<xs:key name="aKey">
<xs:selector xpath="A"/>
<xs:field xpath="@OID"/>
</xs:key>
<xs:keyref name="aKeyRef" refer="aKey">
<xs:selector xpath="./B"/>
<xs:field xpath="@BOID"/>
</xs:keyref>
<xs:keyref name="aKeyRef1" refer="aKey">
<xs:selector xpath="./C"/>
<xs:field xpath="@COID"/>
</xs:keyref>
</xs:element>
<xs:element name="A" type="Atype"/>
<xs:complexType name="Atype">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="A1" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="A2" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="OID" type="xs:string" use="required"/>
<xs:attribute name="A_Membership_Degree" type="FuzzyValue" use="optional"/>
<xs:attribute name="D_Membership_Degree" type="FuzzyValue" use="optional"/>
</xs:complexType>
<xs:element name="B" type="Btype"/>
<xs:complexType name="Btype">
<xs:sequence>
<xs:element name="B1" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="B2" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="BOID" type="xs:string" use="required"/>
<xs:attribute name="B_Membership_Degree" type="FuzzyValue" use="optional"/>
</xs:complexType>
<xs:element name="C" type="Ctype"/>
<xs:complexType name="Ctype">
<xs:sequence>
<xs:element name="C1" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="C2" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="COID" type="xs:string" use="required"/>
<xs:attribute name="C_Membership_Degree" type="FuzzyValue" use="required"/>
</xs:complexType>
<xs:simpleType name="FuzzyValue">
<xs:restriction base="xs:decimal">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
什麼_exactly_是錯誤信息? – 2014-09-20 13:37:01
你能舉出一些應該是有效和無效的XML文檔的例子嗎?你給出的模式允許任何數字,如果A和B元素,所以_which_id你想比較嗎?全部是反對所有? – 2014-09-20 13:41:11
順便說一句,在未來,儘量減少你的問題到最小的內容,再現你所問的問題(但* * *測試,你發佈的內容仍然可以運行,併產生你的問題!)。請參閱http://stackoverflow.com/help/mcve – 2015-12-14 17:56:51