我跟随xml和xsd作为我的问题的基本示例:
<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: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>
第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>
感谢您的帮助!
<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>
原因
<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>
原因
<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>
原因
<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>
原因
<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>
答案 0 :(得分:3)
我已经清理并纠正了您的错误。下面是新的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 4
There is a duplicate key sequence '7' for the 'UniqueChildId' key or unique identity constraint.
Error occurred while loading [], line 15 position 5
There is a duplicate key sequence '9' for the 'UniqueChildIdInChildGroup' key or unique identity constraint.
ParentChildChildGroup-Inv-Eg1.xml is invalid.
E.g。 2:
Error occurred while loading [], line 21 position 5
There is a duplicate key sequence '14' for the 'UniqueChildId' key or unique identity constraint.
ParentChildChildGroup-Inv-Eg2.xml is invalid.
E.g。 2(3?):
Error occurred while loading [], line 9 position 4
There is a duplicate key sequence '7' for the 'UniqueChildId' key or unique identity constraint.
ParentChildChildGroup-Inv-Eg3.xml is invalid.
E.g。 2(4?):
Error occurred while loading [], line 21 position 5
There is a duplicate key sequence '9' for the 'UniqueChildId' key or unique identity constraint.
ParentChildChildGroup-Inv-Eg4.xml is invalid.
您所描述的有效XML已成功验证。