2013-10-22 102 views
0

我有一个包含这样的XML结构有一个属性定义有效名称目标。我们认为:验证XML其他属性

<goal id="doStuff" .. /> 
<goal id="doOtherStuff" .. /> 
<goal id="dontDoThis" .. /> 
<score goal1="doStuff" value1="true" points="10" /> 
<score goal1="doOtherStuff" value1="true" goal2="dontDoThis" value2="false" points="20" /> 

但没有限制的目标,得分可以依靠,所以在某些时候这种格式可以打破的数量。最后我们想出了:

<goal id="doStuff" .. /> 
<goal id="doOtherStuff" .. /> 
<goal id="dontDoThis" .. /> 
<score doStuff="true" points="10" /> 
<score doOtherStuff="true" dontDoThis="false" points="20" /> 

这适用于应用本身,但我一直无法弄清楚如何编写XML Schema来正确地验证这一点;看起来不可能拥有由另一个元素定义的元素的有效名称(我不得不承认只读过了XML Schema规范的第0部分,我只在第1部分和第2部分中抛出了一对Ctrl + Fs)。

得分必须根据我的控制之外的规则进行计算;例如改变目标定义的方式不是一个选项。我拥有的唯一灵活性就是XML的结构。有人可以建议一种方法将给定的结构编码为XML,这样可以使用XML Schema进行验证吗?

回答

0

如果你可以随意修改你的XML的结构,可以考虑添加必需的目标,得分的子元素,像这样:

<goal id="doStuff" /> 
<goal id="doOtherStuff" /> 
<goal id="dontDoThis" /> 
<score points="10"> 
    <scoreGoal goal="doStuff" value="true" /> 
</score> 
<score points="20"> 
    <scoreGoal goal="doOtherStuff" value="true" /> 
    <scoreGoal goal="dontDoThis" value="false" /> 
</score> 

,那么你可以验证您的架构,例如:

<xs:sequence> 
    <xs:element maxOccurs="unbounded" name="goal"> 
    <xs:complexType> 
     <xs:attribute name="id" type="xs:string" use="required" /> 
     <!--...--> 
    </xs:complexType> 
    </xs:element> 
    <xs:element maxOccurs="unbounded" name="score"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element maxOccurs="unbounded" name="scoreGoal"> 
      <xs:complexType> 
      <xs:attribute name="goal" type="xs:string" use="required" /> 
      <xs:attribute name="value" type="xs:boolean" use="required" /> 
      </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
     <xs:attribute name="points" type="xs:int" use="required" /> 
    </xs:complexType> 
    </xs:element> 
</xs:sequence> 

PS。我很欣赏元素名称scoreGoal可能不是最好的选择,但我对你的数据结构所代表的内容一无所知,所以你可能想给它一个合适的名字。

+0

你完全正确;应该是我自己想到的。 :( 谢谢指出! – user2907756