2010-08-08 62 views
1

給出的下面的XML文件:XML模式 - 複雜的驗證

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

現在我要驗證的訂單客戶元素屬性。所有訂單都以逗號分隔......因此,非常簡單。

這可能與模式文件?

回答

0

編輯

我覺得什麼youre是後東西大致像下面,但因爲這使用的ID,這意味着它們需要使你不能ahve的order.id和客戶是唯一withing的docuement .id是相同的 - 不確定是否有解決方法。你也可以選擇使用xsd:IDREFS要做得像你在原來的例子,我認爲公佈,但我個人比較喜歡這種方式...

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

如果我是你,我會做這樣的事情:

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

您可以制定一個模式以確保order.customerId對應於customer.id,我認爲這樣做可以使查找和轉換更容易。當然,如果訂單只是客戶的孩子,那麼你可以更容易,但是確保你必須有其他的要求,使得這個過於冗長或不合理。

+0

我認爲你的代碼片段和我的一樣。但問題是,如何驗證這一點。 – Carnation 2010-08-08 20:54:51

+0

如果有,但以不同的方式..如果我記得(和它已經有一段時間,因爲我創建了架構或使用xml比配置文件更多),您可以鏈接要驗證的實體元素作爲引用另一個元素。 – prodigitalson 2010-08-08 21:12:08

+0

你能解釋我嗎,我該怎麼做,或者可以發表一個例子? :) – Carnation 2010-08-08 21:41:14