views:

612

answers:

4

Hi,

I'm trying to produce a pretty simple XML schema for an 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 AND it may or may not appear under <item>. It may also appear more than once. Additionally, there may be other tags before or after <important_tag> that I can not name in advance.

I would like to give a specific definition for <important_tag>. For example, define attributes that it must contain. What I mean is that if important_tag is present it must conform to my definition. Any other tag doesn't have to conform to any definition.

I tried using the following scheme:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <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 results in an error saying that the schema 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'm using Altova's XML Spy.

How do I solve this?

Thanks, Dana

A: 

Why do you want to mention <important_tag> inside <item> even though you already allow <xs:any>? What do you gain from that?

Joachim Sauer
+5  A: 

There is a great article on MSDN that talks about desigining extensible schemas, which you can find here, I suggest you go through it all, but specifically to your point it explains why you're getting this error in point 2. under "Using XML Schema to Design a Versionable XML Format" (you can search for "non-deterministic" and get straight there.

Basically, once you have an xs:any element the validator cannot assume anything about the other sibling elements, so - you might well have a definition for important_tag that does not require those mandatory attributes and so those elements cannot be validated

Yossi Dahan
And Orchards' article here: http://www.xml.com/lpt/a/1329
annakata
Yes, thanks; read this one too and think it's very good.
Yossi Dahan
A: 

With your requirments (things like "Any other tag doesn't have to conform to any definition."), Schematron, which is based on rules ("this must be true", "that must be false") is may be a better solution than W3C Schema, which is more "everything must be like that".

bortzmeyer
+3  A: 

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

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

The simplest example to show the ambiguity is if there was just one <important_tag>:

  <important_tag></important_tag>

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

I've read that the next version of XML Schema enables you to say what you meant: any tag except important_tag.

Matching the XML in two different ways is similar to the regular expression "a*a*" matching "a" in two different ways (one first "a"; or one second "a"). This ambiguity used to be called "non-deterministic" in the XML spec for DTDs, but the XML Schema spec calls it the Unique Particle Attribution rule (UPA), meaning that you should be able to tell which part of the schema gets each part of the XML document.

13ren
You're right, that was the exact problem. I overcame it by "changing the rules", i.e., enforcing a stricter schema.And thanks for the note on the tag name - that's changed too :)
Dana