Ambiguous XML Schema

I am trying to create a fairly simple XML schema for XML, similar to the following:

<messages> <item> <important_tag></important_tag> </item> <item> <important_tag></important_tag> <tag2></tag2> </item> <item> <tag2></tag2> <tag3></tag3> </item> </messages> 

The idea is that <important_tag> will have a specific definition that may or may not appear in <item> . It can also appear more than once. In addition, there may be other tags before or after <important_tag> , which I cannot name in advance.

I would like to give a specific definition for <important_tag> . For example, define the attributes that should contain. I mean , if important_tag is present, it should match my definition. Any other tag must not conform to any definition.

I tried using the following scheme:

 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="messages"> <xs:complexType> <xs:sequence> <xs:element ref="item" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="item"> <xs:complexType> <xs:sequence> <xs:element ref="important_tag" minOccurs="0"/> <xs:any minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="important_tag"> <xs:complexType> <xs:simpleContent> ... specific definitions for important_tag ... </xs:simpleContent> </xs:complexType> </xs:element> </xs:schema> 

This leads to an error saying that the scheme is ambiguous.

The exact error message is:

 cos-nonambig: '<xs:element ref="important_tag">' makes the content model non-deterministic against '<xs:any>'. Possible causes: name equality, overlapping occurrence or substitution groups. 

I am using Altova XML Spy.

How to solve this?

Thanks Dana

+7
xml xsd ambiguity
source share
3 answers

Regarding the error: the error message mentions a line that is not included in xsd, but these two lines in it are ambiguous:

 <xs:element ref="important_tag" minOccurs="0"/> <xs:any minOccurs="0"/> 

The simplest example to show ambiguity is just one <important_tag> :

  <important_tag></important_tag> 

The problem is that it can be interpreted as one "important_tag" and zero "any" tags (which you wanted), but it can also be interpreted as zero "important_tag" and one "any" tag. This is because the tag "any" can match any tag, including "important_tag".

I read that the next version of XML Schema allows you to say what you had in mind: any tag except important_tag.

XML matching in two different ways is similar to the regular expression "a * a *" corresponding to "a" in two different ways (first "a", or one second "a"). This ambiguity was usually called "non-deterministic" in the XML specification for DTD, but the XML Schema specification calls it the Unique Particle Attribute (UPA) rule, which means that you must be able to determine which part of the schema each part of the XML document receives.

+6
source share

There is a wonderful article on MSDN that talks about allowing extensible schemes that you can find here. I suggest you go through all this, but, in particular, to your question, this explains why you get this error in step 2 in the section "Using an XML Schema to Create an XML Format for a Version" (you can search for "non-deterministic" and right there.

Basically, as soon as you have xs: any element, the validator cannot accept anything about other sibling elements, so - you can have a tag_definition definition that does not require these required attributes, and therefore these elements cannot be checked

+7
source share

With your requirements (for example, β€œAny other tag should not match any definition.”), Schematron , which is rule-based (β€œthis must be true,” β€œit must be wrong,”) may be a better solution than W3C Schema. that more "everything should be so."

+1
source share

All Articles