我有以下XML和XSD作为我的问题的基本示例:
XML
<parentgroup>
<parent id="1">
<child id="2" />
<child id="3" />
<childgroup id="4">
<child id="5" />
<child id="2" />
</childgroup>
<child id="5" />
</parent>
<parent id="2">
<childgroup id="33">
<child id="1" />
<child id="4" />
</childgroup>
<child id="1" />
<child id="5" />
<childgroup id="4">
<child id="1" />
<child id="6" />
</childgroup>
</parent>
</parentgroup>
XSD
<xsd:element name="parentgroup" type="parentgroup">
<xsd:unique name="UniqueParentId">
<xsd:selector xpath="child::parent"></xsd:selector>
<xsd:field xpath="@id"></xsd:field>
</xsd:unique>
</xsd:element>
<xsd:complexType name="parentgroup">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="parent" type="parent" maxOccurs="unbounded" minOccurs="1">
<xsd:unique name="UniqueChildId">
<xsd:selector xpath=".//*"></xsd:selector>
<xsd:field xpath="@id"></xsd:field>
</xsd:unique>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:int" />
</xsd:complexType>
<xsd:complexType name="parent">
<xsd:choice maxOccurs="unbounded" minOccurs="1">
<xsd:element name="child" type="child" maxOccurs="unbounded" minOccurs="1"></xsd:element>
<xsd:element name="childgroup" type="childgroup" maxOccurs="unbounded" minOccurs="1"></xsd:element>
</xsd:choice>
<xsd:attribute name="id" type="xsd:int" />
</xsd:complexType>
<xsd:complexType name="childgroup">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="child" type="child" maxOccurs="unbounded" minOccurs="1"></xsd:element>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:int" />
</xsd:complexType>
<xsd:complexType name="child">
<xsd:attribute name="id" type="xsd:int" />
</xsd:complexType>
任务
所有父母都有一个唯一的“id”属性。没有一个孩子有相同的“id”属性。就像他的父母一样。
父级中的子级和子组具有唯一的“id”属性。排除子组中的子级。(id在第一个depth plus parent中是唯一的)
子组中的子组具有唯一的“id”属性。(因此,允许子组中的childs的id等于同一父级中的childgroup之外的childs的id,但不允许将自己的childgroup的id作为euqal,如2。点[(id在第一个深度加上父项时是唯一的])
1。问题
第一点。2。很清楚,但我不知道如何排除childgroup的后代,我尝试了以下操作,但始终得到xpath语法错误
第一次尝试
<xsd:element name="parent" type="parent" maxOccurs="unbounded" minOccurs="1">
<xsd:unique name="UniqueChildId">
<xsd:selector xpath=".//*[not(childgroup)]"></xsd:selector>
<xsd:field xpath="@id"></xsd:field>
</xsd:unique>
</xsd:element>
第二次尝试
<xsd:element name="parent" type="parent" maxOccurs="unbounded" minOccurs="1">
<xsd:unique name="UniqueChildId">
<xsd:selector xpath=".//*[not(child::childgroup)]"></xsd:selector>
<xsd:field xpath="@id"></xsd:field>
</xsd:unique>
</xsd:element>
第三尝试
<xsd:element name="parent" type="parent" maxOccurs="unbounded" minOccurs="1">
<xsd:unique name="UniqueChildId">
<xsd:selector xpath="child::*[not(childgroup)]"></xsd:selector>
<xsd:field xpath="@id"></xsd:field>
</xsd:unique>
</xsd:element>
谢谢你的帮助!
无效XML
E.G.1
<parentgroup>
<parent id="1">
<child id="2" />
<child id="3" />
<childgroup id="7">
<child id="5" />
<child id="6" />
</childgroup>
<child id="7" />
</parent>
<parent id="8">
<childgroup id=9">
<child id="10" />
<child id="9" />
</childgroup>
<child id="12" />
<child id="13" />
<childgroup id="14">
<child id="15" />
<child id="16" />
</childgroup>
</parent>
</parentgroup>
原因
无效,因为在第一个父项中:childgroup id=“7”与child id=“7”具有相同的id
无效,因为在第二个父项中:child id=“9”具有与其“父”相同的id childgroup id=“9”
E.G.2
<parentgroup>
<parent id="1">
<child id="2" />
<child id="3" />
<childgroup id="4">
<child id="5" />
<child id="7" />
</childgroup>
<child id="7" />
</parent>
<parent id="8">
<childgroup id=9">
<child id="10" />
<child id="8" />
</childgroup>
<child id="12" />
<child id="14" />
<childgroup id="14">
<child id="15" />
<child id="16" />
</childgroup>
</parent>
</parentgroup>
原因
无效,因为在第二个父项中:childgroup id=“14”与child id=“14”具有相同的id
E.G.2
<parentgroup>
<parent id="1">
<child id="2" />
<child id="7" />
<childgroup id="4">
<child id="5" />
<child id="6" />
</childgroup>
<child id="7" />
</parent>
<parent id="8">
<childgroup id=9">
<child id="10" />
<child id="11" />
</childgroup>
<child id="12" />
<child id="14" />
<childgroup id="15">
<child id="16" />
<child id="11" />
</childgroup>
</parent>
</parentgroup>
原因
无效,因为在第一个父项中:child id=“7”与child id=“7”具有相同的id
E.G.2
<parentgroup>
<parent id="1">
<child id="2" />
<child id="3" />
<childgroup id="4">
<child id="1" />
<child id="2" />
</childgroup>
<child id="7" />
</parent>
<parent id="8">
<childgroup id=9">
<child id="10" />
<child id="11" />
</childgroup>
<child id="12" />
<child id="14" />
<childgroup id="9">
<child id="16" />
<child id="11" />
</childgroup>
</parent>
</parentgroup>
原因
无效,因为在第二个父级中:childgroup id=“9”与childgroup id=“9”具有相同的id
有效XML
E.G.1
<parentgroup>
<parent id="1">
<child id="2" />
<child id="3" />
<childgroup id="4">
<child id="5" />
<child id="6" />
</childgroup>
<child id="7" />
</parent>
<parent id="8">
<childgroup id=1">
<child id="2" />
<child id="3" />
</childgroup>
<child id="4" />
<child id="5" />
<childgroup id="6">
<child id="7" />
<child id="9" />
</childgroup>
</parent>
</parentgroup>
E.G.2
<parentgroup>
<parent id="1">
<child id="2" />
<child id="3" />
<childgroup id="4">
<child id="5" />
<child id="7" />
</childgroup>
<child id="7" />
</parent>
<parent id="8">
<childgroup id=1">
<child id="2" />
<child id="5" />
</childgroup>
<child id="4" />
<child id="5" />
<childgroup id="6">
<child id="2" />
<child id="5" />
</childgroup>
</parent>
</parentgroup>
最佳答案
我已经纠正了你的错误。下面是新的xsd,它使用发布的xml和我理解的需求。我希望下面的图表能帮助您更好地可视化应该使用的约束和xpath。
修改后的xsd:
<?xml version="1.0" encoding="utf-8" ?>
<!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)-->
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="parentgroup" type="parentgroup">
<xsd:unique name="UniqueParentId">
<xsd:selector xpath="parent"/>
<xsd:field xpath="@id"/>
</xsd:unique>
</xsd:element>
<xsd:complexType name="parentgroup">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="parent" type="parent" maxOccurs="unbounded">
<xsd:unique name="UniqueChildId">
<xsd:selector xpath=".|childgroup|child"/>
<xsd:field xpath="@id"/>
</xsd:unique>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:int"/>
</xsd:complexType>
<xsd:complexType name="parent">
<xsd:choice maxOccurs="unbounded">
<xsd:element name="child" type="child" maxOccurs="unbounded"/>
<xsd:element name="childgroup" type="childgroup" maxOccurs="unbounded">
<xsd:unique name="UniqueChildIdInChildGroup">
<xsd:selector xpath=".|child"/>
<xsd:field xpath="@id"/>
</xsd:unique>
</xsd:element>
</xsd:choice>
<xsd:attribute name="id" type="xsd:int"/>
</xsd:complexType>
<xsd:complexType name="childgroup">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="child" type="child" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:int"/>
</xsd:complexType>
<xsd:complexType name="child">
<xsd:attribute name="id" type="xsd:int"/>
</xsd:complexType>
</xsd:schema>
更新:xsd可以很好地处理所有示例xml。以下是QTassistant对每一项的看法:
无效:
例如1:
Error occurred while loading [], line 9 position 4There is a duplicate key sequence '7' for the 'UniqueChildId' key or unique identity constraint.Error occurred while loading [], line 15 position 5There is a duplicate key sequence '9' for the 'UniqueChildIdInChildGroup' key or unique identity constraint.ParentChildChildGroup-Inv-Eg1.xml is invalid.
例如2:
Error occurred while loading [], line 21 position 5There is a duplicate key sequence '14' for the 'UniqueChildId' key or unique identity constraint.ParentChildChildGroup-Inv-Eg2.xml is invalid.
例如2(3?):
Error occurred while loading [], line 9 position 4There is a duplicate key sequence '7' for the 'UniqueChildId' key or unique identity constraint.ParentChildChildGroup-Inv-Eg3.xml is invalid.
例如2(4?):
Error occurred while loading [], line 21 position 5There is a duplicate key sequence '9' for the 'UniqueChildId' key or unique identity constraint.ParentChildChildGroup-Inv-Eg4.xml is invalid.
您所描述的有效xml已成功验证。