2011-01-31 43 views
0

我有想要写的模式为两个XML示例:这个XML例子可以写出一个模式吗?

实施例1:

<attributes>
<attribute type="test" value="yes"/>
</attributes>

实施例2:

<attributes>
<attribute type="test">
<value x="100" y="50">yes</value>
</attribute>
</attributes>

实施例3:

<attributes>
<attribute type="test" value="no">
<value x="100" y="50">yes</value>
</attribute>
</attributes>

它是p可以有一个适用于这些模式? “价值”是一个属性,另一个是一个元素?

更新 对,我想我明白我的问题了。

我有这样的XSD:

<xs:complexType name="Attribute"> 
    <xs:sequence > 
     <xs:element name="value" type="Value" minOccurs="0" maxOccurs="unbounded"> 
     </xs:element> 
    </xs:sequence> 
    <xs:attribute name="type" type="xs:string" use="required"> 
    </xs:attribute> 
    <xs:attribute name="value" type="xs:string" > 
    </xs:attribute> 
</xs:complexType> 

但是,当我尝试使用JAXB从XSD生成Java类我得到一个错误:

[xjc] [ERROR] Property "Value" is already defined. Use &lt;jaxb:property> to resolve this conflict. 
    [xjc] line 275 of file:common.xsd 
    [xjc] [ERROR] The following location is relevant to the above error 
    [xjc] line 286 of file:common/common.xsd 
    [xjc] failure in the XJC task. Use the Ant -verbose switch for more details 

我想这是在JAXB的限制而不是XSD。它会尝试创建两个名为getValue()的方法,这将失败。

+0

它是什么让你觉得它不会是可能的? – 2011-01-31 22:47:28

回答

4

答案是'是',但是如果你想要一个基于value属性的模式中的if/else语句,那么答案是'不'。您只需创建包含所有属性和元素值的xsd作为可选项,并且文档将很好地验证。什么是xsd验证会为您做的是告诉您文档在模式中指定的规则下是有效的,但它的作用而不是 do是处理文档内的数据,如value属性的实际值yes/no

如果您发布了您当前拥有的模式,并对它应该是什么样子有疑问,您将得到更具体的模式反馈。

+3

但是,您可以添加Schematron验证。 – biziclop 2011-01-31 22:50:07

1

我猜你的问题是,如果我们可以为你的例子3创建一个模式 - 你基本上有一个名为value的属性和一个名为value的元素。这是可能的

相关问题