根据所使用的XML解析器具有XML验证问题.....我如何表示要被普遍接受的约束

时间:2011-01-10 14:48:04

标签: xml xsd xml-validation

我有一种奇怪的情况让我感到困惑......

我有一个XML数据文件,我试图根据使用的解析器提供不同结果的模式进行验证。不确定我做错了什么或如何更好地表示模式中的约束,以便所有解析器都可以正确验证XML ....以下是模式的问题部分的片段:

<xsd:element name="DemoValues">
<xsd:annotation>
  <xsd:documentation>Demo values for any, or all, of the demo categories defined on the GAP . A
    demo value includes a reference to the category it applies to, a value in the appropriate
    format and an optional market reference if it is for a specific market. If the market
    reference is omitted the demo value applies to the entire area serviced by the outlet. Each
    demo category may only have a single demo value within this group of demo values. However if
    the demo value is for a market, there may be a demo value per market within this group of
    demo values. </xsd:documentation>
</xsd:annotation>
<xsd:complexType>
  <xsd:sequence>
    <xsd:element name="DemoValue" type="gap:demoValueType" maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:complexType>
<xsd:unique name="DemoValueConstraint">
  <xsd:annotation>
    <xsd:documentation>Constraint allows only up to one demo value for a demo category, and per
      market when a market reference exists. </xsd:documentation>
  </xsd:annotation>
  <xsd:selector xpath="gap:DemoValue"/>
  <xsd:field xpath="@demoRef"/>
  <xsd:field xpath="@marketRef|@demoRef"/>
</xsd:unique>

以下是违规的XML:

<DemoValues>
          <DemoValue demoRef="DM0" marketRef="MKT1">0.40</DemoValue>
          <DemoValue demoRef="DM1">15.00</DemoValue>
</DemoValues>

产生此错误:

元素'{http://www.AAAA.org/schemas/canadianTVGAP}DemoValue',属性'marketRef':XPath'@ marketRef | @demoRef'的唯一身份约束字段'{http:/ /www.AAAA.org/schemas/canadianTVGAP}DemoValueConstraint'评估为具有多个成员的节点集。

简化错误是: “marketRef”字段期望最多一个值。

xml的目的是允许所有这些组合:

1)

 <DemoValues>
          <DemoValue demoRef="DM0" marketRef="MKT1">0.40</DemoValue>
          <DemoValue demoRef="DM1">15.00</DemoValue>
 </DemoValues>

2)

<DemoValues>
          <DemoValue demoRef="DM0" marketRef="MKT1">0.40</DemoValue>
          <DemoValue demoRef="DM0" marketRef="MKT2">0.41</DemoValue>
          <DemoValue demoRef="DM0" marketRef="MKT3">0.42</DemoValue>
          <DemoValue demoRef="DM0" marketRef="MKT4">0.43</DemoValue>
          <DemoValue demoRef="DM1">15.00</DemoValue>
</DemoValues>

3)

<DemoValues>
          <DemoValue demoRef="DM0" marketRef="MKT1">0.40</DemoValue>
          <DemoValue demoRef="DM1" marketRef="MKT1">0.41</DemoValue>
          <DemoValue demoRef="DM2" marketRef="MKT1">0.42</DemoValue>
          <DemoValue demoRef="DM3">15.00</DemoValue>
</DemoValues>

感谢您的帮助!!

2 个答案:

答案 0 :(得分:1)

除了将属性列表限制为您描述的组合之外,您是否需要在此处使用字段?

XML元素最多可以包含任何命名属性的一个实例。这是关于XML的事实,与模式无关。

gap:demoValueType的定义中,只需使用<xsd:attribute ... />指定两个属性即可。通过您的示例,您可能希望在@demoref的定义上使用use="required"

答案 1 :(得分:1)

我不会惊讶于你在这里得到不同处理器的不同结果,因为规范非常模糊。但是,第二个xs:字段中的union表达式显然是错误的 - 您永远不应该有一个可以选择多个节点的字段。如果你的第一个字段是@demoRef而你的第二个字段是@marketRef,我希望它能正常工作,但是,我一直在重读这个规范,我会犹豫与一个不同解释它的实现者争论。