存在 XML 验证问题,具体取决于所使用的 XML 解析器......我如何表示要普遍接受的约束

发布于 2024-10-11 09:34:56 字数 3109 浏览 2 评论 0原文

我有一个奇怪的情况,让我有点困惑......

我有一个 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' -constraint '{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>

感谢您的帮助!

I have a strange situation that has me perplexed a bit....

I have an XML data file I am trying to validate against a schema that seems to give different results depending on the parsers used. Not sure what I am doing wrong or how to better represent the constraints in the schema so that all the parsers can properly validate the XML.... Here is a snippet of the problem portion of the Schema:

<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>

Here is the offending XML:

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

Produces this error:

Element '{http://www.AAAA.org/schemas/canadianTVGAP}DemoValue', attribute 'marketRef': The XPath '@marketRef|@demoRef' of a field of unique identity-constraint '{http://www.AAAA.org/schemas/canadianTVGAP}DemoValueConstraint' evaluates to a node-set with more than one member.

Simplified Error is:
The field ‘marketRef’ is expecting at the most one value.

The intent for the xml is to allow all these combinations:

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>

Thanks for any help!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北笙凉宸 2024-10-18 09:34:56

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

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

gap:demoValueType 的定义中,只需使用 指定两个属性即可。从您的示例来看,您可能希望在 @demoref 的定义上使用 use="required"

Do you need to use field here for some other reason than to restrict the attribute list to the combinations you describe?

An XML element can have, at most, one instance of any named attribute. This is a fact about XML, independent of schema.

In your definition of gap:demoValueType, simply specify the two attributes by using <xsd:attribute ... />. By the look of your examples, you probably want to use use="required" on the definition of @demoref)

鱼窥荷 2024-10-18 09:34:56

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

I'm not surprised you are getting different results from different processors here, because the spec is very obscurely written. However, the union expression in your second xs:field is clearly wrong - you should never have a field that can select more than one node. I would expect it to work correctly if your first field is @demoRef and your second is @marketRef, however, I've been re-reading the spec and I would hesitate to argue with an implementor who interpreted it differently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文