【问题标题】:Is this XML example possible to write a schema for?这个 XML 示例是否可以为其编写模式?
【发布时间】:2011-01-31 22:41:33
【问题描述】:

我有两个 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>

是否有可能有一个适用于这些的架构? 'value' 是一个属性,另一个是元素?

更新 好吧,我想我现在明白我的问题了。

我有这个 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() 的方法,但都会失败。

【问题讨论】:

  • 它是什么让你觉得它不会是可能的?

标签: xml xsd


【解决方案1】:

答案是“是”,但如果您想要基于value 属性的架构中的 if/else 语句,那么答案是“否”。您只需创建 xsd 并将所有属性和元素值作为可选,并且文档将很好地验证。 xsd 验证将为您做的是告诉您文档在架构中指定的规则下是有效的,但它所做的是像实际的yes/ 那样处理文档内的数据no value 属性的值。

如果您发布您当前拥有的架构并对它的外观有疑问,您将获得更具体的架构反馈。

【讨论】:

  • 您可以,但是可能会为此添加 Schematron 验证。
  • 两个属性元素也可以有不同的命名空间(啊,我的眼睛,我的眼睛,请不要将元素命名为“属性”!)
  • @James:想象一下,如果每个“属性”元素都有一个属性“元素”。 :]
  • @Jeff M:我尽量不这样做。我也在努力不去想象伴随的架构......
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多