2010-10-08 65 views
0

我有以下XML架構派生時:XML架構和問題,從混合型

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:element name="content" type="contentType"/> 

    <xs:complexType name="contentType"> 
     <xs:complexContent> 
      <xs:extension base="versionedElementType"> 
       <xs:sequence> 
        <xs:element name="item" type="itemType" minOccurs="1" maxOccurs="unbounded" /> 
       </xs:sequence> 
      </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="itemType" mixed="true"> 
     <xs:complexContent> 
      <xs:extension base="itemTypeBase"> 
        <xs:sequence> 
         <xs:element name="order" type="xs:unsignedInt"/> 
         <xs:element name="id" type="xs:string"/> 
        </xs:sequence> 
      </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 

    <!-- Simple type convert to complex type --> 
    <xs:complexType name="itemTypeBase" mixed="true"> 
     <xs:simpleContent> 
      <xs:extension base="itemDescriptionType"> 
      </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 

    <!-- Simple type -string restriction --> 
    <xs:simpleType name="itemDescriptionType" > 
     <xs:restriction base="xs:string"> 
      <xs:minLength value="1"/> 
      <xs:maxLength value="64"/> 
     </xs:restriction> 
    </xs:simpleType> 


    <xs:complexType name="versionedElementType"> 
     <xs:attribute name="version" type="xs:string" use="required"/> 
    </xs:complexType> 

</xs:schema> 

,我用它來驗證此XML實例(我想文字與子元素的「項目」元素混合「秩序」和「ID」):

<?xml version="1.0" encoding="UTF-8"?> 
<content xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:noNamespaceSchemaLocation="Content.xsd" 
     version ="1.0"> 

    <item>Description here... 
    <order>2</order> 
    <id>2</id> 
    </item> 
</content> 

無論我做了驗證還說taht有一個錯誤:

內容類型派生的類型,其基礎必須BOT混合或兩者都是元素。類型'itemType'是混合的,但其基類型不是。

但我可以看到,這兩種類型 - itemType和itemTypeBase混合!

非常感謝 斯登

回答

3

首先錯誤的,我看看,如果我在Visual Studio 2010中打開您的架構:

The derived type and the based type must have the same content type.

在你目前的架構類型itemTypeBase相對於定義的<xs:simpleContent>和派生類型itemType<xs:complexContent>這是不允許的。您不允許使用子元素並使用<xs:simpleContent>,或者您使用子元素並使用<xs:complexContent>

我個人不喜歡,也不使用混合類型。如果我理解你是正確的,你想對內容中的文本做一些限制。您希望內容長度在1到64個字符之間。但是,<order>2</order>,<id>2</id>和所有空格(包括新行字符)也是內容的一部分。如果你想<item>有簡單的內容,那麼你不能在裏面插入子元素。

所以實用主義解決辦法是從混合模式走,並使用XML文檔中的表格

<?xml version="1.0" encoding="utf-8"?> 
<content xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:noNamespaceSchemaLocation="Content.xsd" 
     version ="1.0"> 
    <item> 
     <description>Description here...</description> 
     <order>2</order> 
     <id>2</id> 
    </item> 
</content> 

其中Content.xsd是

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:element name="content" type="contentType"/> 

    <xs:complexType name="contentType"> 
     <xs:sequence> 
      <xs:element name="item" type="itemType" 
         minOccurs="1" maxOccurs="unbounded" /> 
     </xs:sequence> 
     <xs:attribute name="version" type="xs:string" use="required"/> 
    </xs:complexType> 

    <xs:complexType name="itemType"> 
     <xs:sequence> 
      <xs:element name="description" type="itemDescriptionType"/> 
      <xs:element name="order" type="xs:unsignedInt"/> 
      <xs:element name="id" type="xs:string"/> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:simpleType name="itemDescriptionType" > 
     <xs:restriction base="xs:string"> 
      <xs:minLength value="1"/> 
      <xs:maxLength value="64"/> 
     </xs:restriction> 
    </xs:simpleType> 

</xs:schema> 

一切都將是非常簡單明瞭。

+0

嗨,首先我想將文本與元素混合在一起,但由於我無法找到正確的XML模式,所以我使用了在那裏顯示的類似解決方案。我必須100%同意你的意見,它更簡單明瞭。感謝您的時間和良好的迴應。 BR STeN – STeN 2010-10-14 03:55:50