“独特的粒子属性"违反 [英] "Unique Particle Attribution" violation

查看:23
本文介绍了“独特的粒子属性"违反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下(简化的)架构来验证我收到的一些 XML 文件:

I wrote the following (simplified) schema to validate some XML files I receive:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

    <xs:element name="Param">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="RadioAddr" type="xs:string" />
                <xs:element name="DataToRead" type="xs:integer" minOccurs="0" maxOccurs="1" />
                <xs:choice minOccurs="0" maxOccurs="1">
                    <xs:group ref="Group1" />
                    <xs:group ref="Group2" />
                    <xs:group ref="Group3" />
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:group name="Group1">
        <xs:sequence>
            <xs:element ref="Password" />
            <xs:element name="RadioActivated" type="xs:integer" minOccurs="0" maxOccurs="1" />
            <xs:element ref="IdNumber" minOccurs="0" maxOccurs="1" />
            <xs:element ref="AdjustClock" minOccurs="0" maxOccurs="1" />
        </xs:sequence>
    </xs:group>

    <xs:group name="Group2">
        <xs:sequence>
            <xs:element ref="IdNumber" minOccurs="0" maxOccurs="1" />
            <xs:element ref="Password" />
            <xs:element ref="AdjustClock" minOccurs="0" maxOccurs="1" />
        </xs:sequence>
    </xs:group>

    <xs:group name="Group3">
        <xs:sequence>
            <xs:element ref="IdNumber" minOccurs="0" maxOccurs="1" />
            <!-- No password here -->
            <xs:element ref="AdjustClock" minOccurs="0" maxOccurs="1" />
        </xs:sequence>
    </xs:group>

    <xs:element name="Password" type="xs:string" />
    <xs:element name="IdNumber" type="xs:integer" />
    <xs:element name="AdjustClock" type="xs:integer" />

</xs:schema>

验证此架构时,我收到以下错误消息:

When validating this schema, I obtain the following error message:

无效.错误 - 第 5、25 行:org.xml.sax.SAXParseException;行号:5;列数:25;cos-nonambig:密码和密码(或其替代组中的元素)违反唯一粒子归因".在对此模式进行验证期间,歧义会为这两个粒子创建.

Not valid. Error - Line 5, 25: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 25; cos-nonambig: Password and Password (or elements from their substitution group) violate "Unique Particle Attribution". During validation against this schema, ambiguity would be created for those two particles.

我完全理解这种歧义,但我找不到使我的架构有效的解决方案.

I fully understand the ambiguity but I can't find a solution to make my schema valid.

一个可能的解决方案是做类似的事情

A possible solution would be to do something like

<xs:element name="Param>
    <xs:complexType>
        <xs:choice maxOccurs="unbounded">
            <!-- put all the possible elements here -->
        </xs:choice>
    </xs:complexType>
</xs:element>

但是我对这个解决方案的问题是我失去了一个对我进一步有用的抽象级别(组)(我使用这个模式用 JAXB 生成 Java 类).

but my problem with this solution is that I lose one level of abstraction (the groups) that is useful for me further (I use this schema to generate Java classes with JAXB).

那么,有没有办法使用 <xs:group> 使我的架构有效,或者我是否必须展平我的架构(如我上面提到的解决方案)?

So, is there a way to make my schema valid using <xs:group> or do I have to flatten my schema (like the solution I mentioned above) ?

以下是 XSD 应允许的示例:

Here are examples that should be allowed by the XSD:

允许的最小值:

<Param>
    <RadioAddr>1</RadioAddr>
</Param>

也是合法的:

<Param>
    <RadioAddr>1</RadioAddr>
    <Password>1234</Password>
</Param>

<Param>
    <RadioAddr>1</RadioAddr>
    <Password>1234</Password>
    <RadioActivated>1</RadioActivated>
    <IdNumber>12345678</IdNumber>
</Param>

<Param>
    <RadioAddr>1</RadioAddr>
    <IdNumber>12345678</IdNumber>
    <Password>1234</Password>
</Param>

推荐答案

要克服 Unique Particle Attribution 违规,您必须允许解析器明确知道它在语法中的位置,而不必多看元素.

To beat the Unique Particle Attribution violation, you have to have to allow a parser to unambiguously know where it stands in the grammar without having to look ahead more than one element.

出现当前错误是因为在遇到Password元素时无法知道解析器是在Group1还是Group2中,因为IdNumber 是可选的.您可以改为强制使用 IdNumber,但这会在 Group2Group3 之间在 IdNumber 上产生歧义.然后,您可能会尝试使用排序来区分 xs:choice 组,但随后您会发现元素的可选性使您的努力落空.您可能会删除可选性,如果可以接受组之间的不同排序,那么您可能会有答案.

The current error arises because it is not possible to know when encountering the Password element whether the parser is in Group1 or Group2 because IdNumber is optional. You might make IdNumber mandatory instead, but that would create ambiguity between Group2 and Group3 over IdNumber. You might then try using ordering to differentiate the xs:choice groups, but then you'd find that the optionality of the elements was defeating your effort. You might remove the optionality, and if different ordering among the groups is acceptable, then you may have your answer.

然而,这将是一个相当奇怪的语法.在这一点上,您可能会像您提到的那样更好地展平,而不是使用无界 xs:choice,这将允许其元素的任意和无界重复,您可以保留一些出现通过元素的简单 xs:sequence 代替:

However, that would be a rather odd grammar. At that point, you'd probably be better flattening as you mentioned, but rather than using an unbounded xs:choice, which would allow arbitrary and unbounded repeats of its elements, you can retain some of the occurrence constraints via a simple xs:sequence of the elements instead:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

  <xs:element name="Param">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="RadioAddr" type="xs:string" />
        <xs:element name="DataToRead" type="xs:integer" minOccurs="0" maxOccurs="1" />
        <xs:element name="RadioActivated" type="xs:integer" minOccurs="0" maxOccurs="1" />
        <xs:element ref="IdNumber" minOccurs="0" maxOccurs="1" />
        <xs:element ref="Password" minOccurs="0"/>
        <xs:element ref="AdjustClock" minOccurs="0" maxOccurs="1" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="Password" type="xs:string" />
  <xs:element name="IdNumber" type="xs:integer" />
  <xs:element name="AdjustClock" type="xs:integer" />

</xs:schema>

这篇关于“独特的粒子属性"违反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆