tags:

views:

70

answers:

1

Given is following XML file:

 <root>
  <customers>
    <customer name="CustomerA" orders="111,222" />
  </customers>

  <orders>
    <order ID="111">
      <description text="Some bananas ..." />
    </order>

    <order ID="222">
      <desciption text="good coffee" />
    </order>

  </orders>

</root>

Now I want to validate the orders attribute on the customer element. All orders are sperated with an comma ... So, very simple.

Is this possible with schemas files?

A: 

EDIT:

I think what youre after is something roughly like the following, though since this uses id's that means they need to be unique withing the docuement so you cant ahve an order.id and a customer.id that are the same - not sure if there is a way around that. you could also alternatively use xsd:IDREFS to do it like you posted in your original example i think, but personally i like it better this way...

   <xsd:complexType name="Customer">
      <xsd:attribute name="customerId" type="xsd:ID" use="required"/>
      <xsd:attribute name="name" type="xsd:string" use="required"/>
   </xsd:complexType>

   <xsd:complexType name="Order">
      <xsd:sequence>
         <xsd:element name="text" type="xsd:string"/>
         <xsd:attribute name="customerId" type="xsd:IDREF" use="required"/>
         <xsd:attribute name="id" type="xsd:ID" use="required"/>
      </xsd:sequence>
   </xsd:complexType>

   <xsd:element name="customers>
       <xsd:sequence>
           <xsd:element name="customer" type="CustomerType" />
       </xsd:sequence>
   </xsd:element>
   <xsd:element name="orders">
     <xsd:sequence>
         <xsd:element name="order" type="OrderType" />
     </xsd:sequence>
   </xsd:element>

If i were you i would do something like:

<root>
  <customers>
    <customer id="unique-customer-id" name="CustomerA" />
  </customers>
  <orders>
    <order id="222" customerId="unique-customer-id" text="Some Bananas..." />
  </orders>
</root>

You can craft a schema to ensure that a order.customerId corresponds to a customer.id i would think this would aslo make lookup and transformation much easier. Of course you could make it even easier if orders were just children of the customer, but im sure you must have other requirements that make this too verbose or ill advised.

prodigitalson
I think that your code snippet does the same as mine. But the question is, how to validate this.
Carnation
If does but in a different way.. if i recall (and its been awhile since i created schema or used xml for much more than a config file) you can link the attribute element to be validated as a reference to another element.
prodigitalson
Can you explain me, how do I this or can you post an example? :)
Carnation
see my update above as well as: http://www.xfront.com/GlobalVersusLocal.html
prodigitalson
The example seems to have a small error: attribute customerID should have type xs:ID instead of xs:IDREF. Also there are 2 restrictions that are easy to avoid but cause problems with the example code: XML IDs can't start with a number character (they need to be valid XML names, see http://www.w3.org/TR/REC-xml/#NT-Name ) and xs:IDREFS must be a space separated list of IDs, not comma separated. Valid space characters are (#x20 | #x9 | #xD | #xA)
jasso
I was aware of the number issue, but failed to mention it. The other error for the `Customer` type i have corrected.
prodigitalson