I have an xml file with the associated xsd below:
The element ContentTitle needs to be unique. I've done some investigating and know to use the xs:unique attribute, but I've yet to get this working correctly with my scenario.
Can anyone offer advice?
<xs:element name="AllMeta">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="WikiMeta">
<xs:complexType>
<xs:sequence>
<xs:element name="ContentTitle" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="PageTitle" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="PageMetaDescription" type="xs:string" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Add a xs:unique
element after the xs:complexType
for AllMeta
:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="AllMeta">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="WikiMeta">
<xs:complexType>
<xs:sequence>
<xs:element name="ContentTitle" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="PageTitle" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="PageMetaDescription" type="xs:string" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniqueContentTitle">
<xs:selector xpath="WikiMeta"/>
<xs:field xpath="ContentTitle"/>
</xs:unique>
</xs:element>
</xs:schema>
Then, this XML document instance will be valid:
<?xml version="1.0" encoding="UTF-8"?>
<AllMeta xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="try.xsd">
<WikiMeta>
<ContentTitle>One</ContentTitle>
<PageTitle></PageTitle>
<PageMetaDescription></PageMetaDescription>
</WikiMeta>
<WikiMeta>
<ContentTitle>Two</ContentTitle>
<PageTitle></PageTitle>
<PageMetaDescription></PageMetaDescription>
</WikiMeta>
</AllMeta>
And this XML document instance will be invalid:
<?xml version="1.0" encoding="UTF-8"?>
<AllMeta xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="try.xsd">
<WikiMeta>
<ContentTitle>One</ContentTitle>
<PageTitle></PageTitle>
<PageMetaDescription></PageMetaDescription>
</WikiMeta>
<WikiMeta>
<ContentTitle>One</ContentTitle>
<PageTitle></PageTitle>
<PageMetaDescription></PageMetaDescription>
</WikiMeta>
</AllMeta>
And an error message such as the following will be given by the validator (Xerces-J, in this case):
[Error] try.xml:10:37: cvc-identity-constraint.4.1: Duplicate unique value [One] declared for identity constraint "uniqueContentTitle" of element "AllMeta".
c:/gd/usr/kjh/proj/try/xsd/try.xml: 219 ms (9 elems, 1 attrs, 0 spaces, 49 chars)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.