2014-10-07 62 views
0

我喜欢根据这些元素之一的值创建具有不同子元素的元素。我想知道如何以及如何实现这一点(最佳解决方案)。 所以如果Typepiece必须有Weight元素。如果Typekilogramgram或......它不需要它。按值构建不同子元素的xml架构限制列表

<?xml version="1.0" encoding="UTF-8"?> 
    <Amounts> 
     <Amount> 
      <Type>piece</Type> 
      <Value>6</Value> 
      <Weight> 
       <Value>1.5</Value> 
       <Type>liter</Type> 
      </Weight> 
     </Amount> 
     <Amount> 
      <Type>kilogram</Type> 
      <Value>0.610</Value> 
     </Amount> 
    </Amounts> 

所以我想我可能会做这样的事情,但我会得到错误(见下文)。

<?xml version="1.0" encoding="UTF-8"?> 
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.test.org/amount" 
    xmlns:amount="http://www.test.org/amount" elementFormDefault='qualified'> 
    <element name="Amount"> 
     <complexType> 
      <choice> 
       <group ref="amount:kilogram" /> 
       <group ref="amount:piece" /> 
      </choice> 
     </complexType> 
    </element> 
    <group name="kilogram"> 
     <sequence> 
      <element name="Type" fixed="kilogram" /> 
      <element name="Value" type="float" /> 
     </sequence> 
    </group> 
    <group name="piece"> 
     <sequence> 
      <element name="Type" fixed="piece" /> 
      <element name="Value" type="int" /> 
      <element name="Weight"> 
       <complexType> 
        <sequence> 
         <element name="Type" type="amount:amountType" /> 
         <element name="Value" type="float" /> 
        </sequence> 
       </complexType> 
      </element> 
     </sequence> 
    </group> 
    <simpleType name="amountType"> 
     <restriction base="string"> 
      <enumeration value="kg" /> 
      <enumeration value="g" /> 
      <enumeration value="mg" /> 
      <enumeration value="lb" /> 
     </restriction> 
    </simpleType> 
</schema> 

错误我在第5行得到的是:在这条线找到

多个批注: - COS-nonambig: “http://www.test.org/amount ”:类型和“ http://www.test.org/amount”(或类型元素的替代 组)违反了“唯一粒子归因”。在验证
针对此模式时,将为这两个 粒子创建歧义。 - cos-element-consistent:类型'#AnonType_Amount'的错误。模型 组中出现名称为“值”的多个元素,具有不同的类型。

回答

1

如果您将“类型”属性而不是元素,那么您可以使用条件类型分配在XSD 1.1中执行此操作。否则,对不起,你运气不好。

+0

嗯好吧,你可以举一个简单的例子如何在XSD 1.1中做到这一点?我会稍后尝试,然后检查你的答案。 – DarsVaeda 2014-10-08 09:37:23

+0

Priscilla Walmsley在这里有很多条件类型赋值的例子:http://www.datypic.com/books/defxmlschema/chapter14.html(如果你想正确理解它们,请购买她的书!) – 2014-10-08 10:50:54