开发者

XML schema - complex validation

开发者 https://www.devze.com 2023-01-11 04:04 出处:网络
Given is following XML fi开发者_JAVA技巧le: <root> <customers> <customer name=\"CustomerA\" orders=\"111,222\" />

Given is following XML fi开发者_JAVA技巧le:

 <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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消