Wednesday, April 15, 2009

XSD (XML Schema) Hierarchical Unique Contraint

While working on RegexConverter I've started learning things about XML. I disliked it before, but now... I loathe it completely. It feels like the XML people are from another planet and I have to twist my brain at straight angles just to understand what they meant.

One of the problems I met was "how to declare in an XML schema a unique constraint/index on the entire document when there are nested elements of the same type". In other words, something like this:
<group index="1">
<group index="2">
<group index="3">

Bottom line:
  1. Use the declared namespace
  2. Declare the index in the root element
  3. Use the name of the nested element (in my case group) in the selector
  4. Use the one or more levels deep selector (.//)
  5. Use the declared namespace AGAIN.

Concrete example:
<xs:element name="regex" type="regexType">
<xs:unique name="uniqueIndex">
<xs:selector xpath="mstns:group|.//mstns:group"></xs:selector>
<xs:field xpath="@index"></xs:field>

I tried to solve this for two hours only to find the solution was this idiotic. Neither one of "group","mstns:group","mstns:group|.//group" work. The only one is "mstns:group|.//mstns:group".