2010-01-26 102 views
0

只是要简明扼要我想获得这样的事:如何在XML Schema中定义具有相同名称但不同类型的元素序列?

<root> 
    <field value="..." text="...">fixed_value1</field> 
    <field value="..." text="...">fixed_value2</field> 
    <field value="..." text="...">fixed_value3</field> 
    <!-- in some cases we can choose the «fixed_value» among different ones --> 
    ... 
    <field value="..." text="...">fixed_valueN</field> 
</root> 

我尝试不同的方法,但它似乎很不可能才达到,因为XML模式不允许设置的元素列表名称相同,但不同的类型(简单或复杂并不重要...)。这样对吗?没有其他方法来定义像上面那样的结构?

编辑:也许我必须解释它好一点。在元素«field»的打开和关闭标记之间必须有一个由XML Schema定义的值(换句话说,对于用户来说,不可能写入与fixed_value不同的东西)。又如:

<root> 
    <field value="Ferrari" text="company">Car</field> 
    <!-- but it could be Van or Motorcycle or Plane --> 
    <field value="12300000" text="euro">Cost</field> 
    <!-- here instead it's only possible to choose «Cost» --> 
    <field value="Red" text="">Color</field> 
    <!-- same as above --> 
</root> 

这可能吗?提前致谢!

回答

1

请尝试以下XSD:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="root" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="root"> 
    <xs:complexType> 
     <xs:choice minOccurs="0" maxOccurs="unbounded"> 
     <xs:element name="field" nillable="true"> 
      <xs:complexType> 
      <xs:simpleContent> 
       <xs:extension base="xs:string"> 
       <xs:attribute name="value" type="xs:string" /> 
       <xs:attribute name="text" type="xs:string" /> 
       </xs:extension> 
      </xs:simpleContent> 
      </xs:complexType> 
     </xs:element> 
     </xs:choice> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 
相关问题