[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 571
  • Last Modified:

XML Schema Unique

For some reason I can't get any xml validator to enforce the unique requirements in this schema.  Here it is:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="CasesReportCriteria"
    targetNamespace="http://Solutions/Courts/CasesReportCriteria"
    elementFormDefault="qualified"
    xmlns="http://Solutions/Courts/CasesReportCriteria"
    xmlns:mstns="http://Solutions/Courts/CasesReportCriteria"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>

  <xs:simpleType name="nonZeroLengthString">
    <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:complexType name="attorneyType">
    <xs:sequence>
      <xs:element name="id" type="nonZeroLengthString" minOccurs="0"/>
      <xs:element name="name" type="nonZeroLengthString" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="criteria">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="attorneys" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="attorney" type="attorneyType" maxOccurs="unbounded"/>
            </xs:sequence>
          </xs:complexType>
          <xs:unique name="uniqueAttorney">
            <xs:selector xpath="mstns:attorney"/>
            <xs:field xpath="id"/>
            <xs:field xpath="name"/>
          </xs:unique>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Open in new window


But the following document passes validation (and it shouldn't):

<?xml version="1.0" encoding="utf-8"?>
<criteria
    xmlns="http://Solutions/Courts/CasesReportCriteria"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <attorneys>
        <attorney>
            <id>123</id>
            <name>Joe Blow</name>
        </attorney>
        <attorney>
            <id>123</id>
            <name>Joe Blow</name>
        </attorney>
    </attorneys>
</criteria>

Open in new window


I'm fairly new to XML schemas so I'm sure it's something simple I'm missing...
0
mcorrente
Asked:
mcorrente
  • 2
  • 2
1 Solution
 
Geert BormansCommented:
The fields with the selector should also be in the namespace

                   <xs:unique name="uniqueAttorney">
                        <xs:selector xpath="mstns:attorney"/>
                        <xs:field xpath="mstns:id"/>
                        <xs:field xpath="mstns:name"/>
                    </xs:unique>

then it will work
0
 
mcorrenteAuthor Commented:
Good catch.  Xpath doesn't like default namespaces, right?  Assumed that since I had defined the namespace of the parent it would propagate to the children, but apparently not.

Now document validates correctly in the online validators I use, but still not catching errors when I validate with XmlReader in VB .NET.  XDocument.Validate seems to catch it, but has other limitations I'm going to have to work around.

Thanks for your help.
0
 
Geert BormansCommented:
Welcome,

It was an easy catch, since a similar issue kept me busy a couple of hours some time ago :-)
But you need to be explicit about the namespace indeed

xs:unique might have some flaws in support
if you are sure that the xs:field always have a value (id and name always present)
then you could use xs:key instead, support for that seems to be better
you never know it is better supported in the .net schema validator
0
 
mcorrenteAuthor Commented:
I'll look into key.  Like I mentioned, I'm fairly new to XML schemas, and I'm trying to avoid boiling the ocean as far as my schema education goes if I can avoid it.  I do believe that id and name will both always be present, so that sounds like it might be the way to go.

Thanks again.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now