Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Re: Semi-unique IDs in Schema >Thread Next - Re: Semi-unique IDs in Schema Re: Semi-unique IDs in SchemaTo: NULL Date: 5/3/2004 12:09:00 PM Martin Honnen wrote: > > > Victor Engmark wrote: > >> How do I define that the contents of an element should be unique only >> in a sub-tree of the whole XML file? >> >> In my case, I have several documents, each containing several files. >> The file names have to be unique only within each document. I.e., the >> following is valid: >> <doc> >> <file>AAA</file> >> <file>BBB</file> >> </doc> >> <doc> >> <file>AAA</file> >> </doc> >> >> , while the following is not: >> <doc> >> <file>AAA</file> >> <file>BBB</file> >> <file>AAA</file> <!-- Matches previous file! --> >> </doc> > > > XML schema allows for uniqueness constraints, here is an example schema > > <?xml version="1.0" encoding="UTF-8"?> > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> > > <xs:element name="root"> > <xs:complexType> > <xs:sequence> > <xs:element ref="doc" maxOccurs="unbounded" /> > </xs:sequence> > </xs:complexType> > </xs:element> > > <xs:element name="doc"> > <xs:complexType> > <xs:sequence> > <xs:element name="file" type="xs:string" maxOccurs="unbounded" /> > </xs:sequence> > </xs:complexType> > <xs:unique name="uniqueFile"> > <xs:selector xpath="file" /> > <xs:field xpath="." /> > </xs:unique> > </xs:element> > > </xs:schema> > > and an example document > > <?xml version="1.0" encoding="UTF-8"?> > <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > xsi:noNamespaceSchemaLocation="test20040430Xsd.xml"> > <doc> > <file>AAA</file> > <file>BBB</file> > </doc> > <doc> > <file>AAA</file> > <file>AAA</file> > </doc> > </root> > > where the validation will flag an error for the second <file> element in > the second <doc> element. I tried this, but I have obviously overlooked something, because the file http://vengmark.home.cern.ch/vengmark/moi/examples/input.xml is valid according to http://vengmark.home.cern.ch/vengmark/moi/examples/moi.xsd, even though none of the ID elements are unique. Sorry the example is verbose... -- Victor | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
