[xsd-users] Defining key and key ref

Boris Kolpackov boris at codesynthesis.com
Sun Feb 26 08:45:51 EST 2006


Eyal,

Carmi, Eyal (Eyal) <carmi at avaya.com> writes:

> It validates even though the pKey 'A' appears twice... I also tried
> defining 'pKey' as an element (instead of attribute) but the results are
> the same.

I tried your schema and test xml with xsd-1.9.0 and xerces-c-2.7.0 and
everything works as expected:

test.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="MyFilters">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Filter" type="FilterType" minOccurs="0" maxOccurs="4"/>
    </xs:sequence>
  </xs:complexType>

  <xs:key name="FilterKey">
    <xs:selector xpath="Filter"/>
    <xs:field xpath="@pKey"/>
  </xs:key>
</xs:element>

<xs:complexType name="FilterType">
  <xs:sequence>
    <xs:element name="data1" type="xs:integer"/>
    <xs:element name="data2" type="xs:integer" minOccurs="0"/>
  </xs:sequence>

  <xs:attribute name="pKey" type="xs:string"/>
</xs:complexType>

</xs:schema>


test.xml:

<MyFilters xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="test.xsd">

  <Filter pKey="A">
    <data1>-0</data1>
    <data2>-0</data2>
  </Filter>

  <Filter pKey="A">
    <data1>-0</data1>
    <data2>-0</data2>
  </Filter>

</MyFilters>


driver.cxx:

#include <iostream>
#include "test.hxx"

int
main ()
{
  try
  {
    MyFilters ("test.xml");
  }
  catch (xml_schema::parsing const& e)
  {
    std::cerr << e << std::endl;
    return 1;
  }
}


$ xsd cxx-tree test.xsd
$ g++ driver.cxx test.cxx -lxerces-c
$ ./driver
/tmp/test.xml:9:20 error: Duplicate key value declared for identity constraint of element 'MyFilters'.


hth,
-boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20060226/d37ccddd/attachment.pgp


More information about the xsd-users mailing list