DevPinoy.org
A Filipino Developers Community

>>> First two to make 3 wins! <<<

Using XPath for Identity Constraint of Elements and Attributes

rated by 0 users
This post has 5 Replies | 1 Follower

Top 50 Contributor
Posts 27
Points 450
jong.net Posted: 08-25-2006 1:08 AM

Hi to all, I have an XML schema document that looks like this :

<?xml version="1.0" ?>
<xs:schema id="Employees" targetNamespace="http://tempuri.org/Employee.xsd" xmlns:mstns="http://tempuri.org/Employee.xsd"
  xmlns="http://tempuri.org/Employee.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
  attributeFormDefault="qualified" elementFormDefault="qualified">
  <xs:element name="Employees" msdata:IsDataSet="true" msdata:EnforceConstraints="False">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:element name="Employee">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Firstname" type="xs:string" minOccurs="1" msdata:Ordinal="0" maxOccurs="1" />
              <xs:element name="Lastname" type="xs:string" minOccurs="1" msdata:Ordinal="1" maxOccurs="1" />
            </xs:sequence>
            <xs:attribute name="empId" form="unqualified" type="xs:byte" use="required" />
          </xs:complexType>
          <xs:key name="EmployeeKey">
            <xs:selector xpath="Employee" />
            <xs:field xpath="Employee/@empId" />
          </xs:key>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

I want to test whether the schema that I created validates against an instance document. Below is my sample instance document.

<?xml version="1.0" encoding="utf-8"?>
<Employees xmlns="http://tempuri.org/Employee.xsd">
  <Employee empId="1">
    <Firstname>John</Firstname>
    <Lastname>Doe</Lastname>
  </Employee>
  <Employee empId="1">
    <Firstname>Jane</Firstname>
    <Lastname>Doe</Lastname>
  </Employee>
</Employees>

As we should expect, the instance document will be invalidated since both <Employee> elements have the same empId attribute (empId="1"). When I validate this instance document against the schema that I created, it is recognized as a valid XML document. What could be the problem? Is there a syntax error in my XPath declaration?

Success is 1% Inspiration and 99% Perspiration.. - T. Edison -
  • | Post Points: 20
Top 25 Contributor
Posts 202
Points 2,290
try this it might work:

<xs:key name="EmployeeKey">
          <xs:selector xpath="Employee" />
          <xs:field xpath="@empId" />
</xs:key>
  • | Post Points: 5
Top 25 Contributor
Posts 202
Points 2,290
OT:  it would be nice if the thread originators would make an effort to answer replies to their post.  sayang lang effort ng mga sumasagot sa mga problema niyo!  
  • | Post Points: 20
Top 50 Contributor
Posts 50
Points 815
Ayah, minsan (madalas) walang thank you sa ganito. Iniisip ko na lang kung di na nagreply na-solve na nila yung problema nila. Kaya hindi ko masabing sayang lang ang effort.

OT: medyo tagal na ko di nag-post dito. Tingnan ko mga iba pang pending questions dito... [:-)]
  • | Post Points: 20
Top 50 Contributor
Posts 27
Points 450

hehe.. sensya na ha.. actually, ngayon lang ulit na-check tong forums, i was out of the office and was not able to continue my XML study and development. hehe.. anyway, thanks po sa mga effort ninyo, I really appreciate it. Smile..

actually, na-try ko na po yung mga previous na binigay niyo. Unfortunately, hindi yata nag-work eh. Fortunately, however, I also posted that same problem in msdn forums and someone there was able to make it work..

Here's the link : http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=667201&SiteID=1

ps: thanks smash and punzie. huwag na kayo tampo. hehe..

Success is 1% Inspiration and 99% Perspiration.. - T. Edison -
  • | Post Points: 20
Top 25 Contributor
Posts 202
Points 2,290
o tingnan mo, di parepareho tayung may natutunan from an XML guru to boot. the last time i dabbled with xml, DTDs pa ang uso. wido-wido na lang kung baga...
  • | Post Points: 5
Page 1 of 1 (6 items) | RSS

Copyright DevPinoy 2005-2008