在XSD内部做出与选择相似的序列,即“违反'独特粒子归因'”例外

时间:2018-01-22 09:00:27

标签: java xml xsd xml-parsing xsd-validation

xsd以下工作正常

<xs:complexType name="header">
        <xs:sequence>
            <xs:element name="leftAlignedColumns" type="listOfColumns" minOccurs="0" maxOccurs="1" />
            <xs:element name="rightAlignedColumns" type="listOfColumns" minOccurs="0" maxOccurs="1" />
        </xs:sequence>
    </xs:complexType>

<xs:complexType name="header">
        <xs:sequence>
            <xs:element name="leftAlignedColumns" type="listOfColumns" minOccurs="1" maxOccurs="1" />
            <xs:element name="rightAlignedColumns" type="listOfColumns" minOccurs="1" maxOccurs="1" />
        </xs:sequence>
    </xs:complexType>

但我的要求是leftAlignedColumnsrightAlignedColumns中的任何一个都必须出现在标题内。

  

所以我看到XML schema construct for "any one or more of these elements but must be at least one"

,如下所示
 <xs:complexType name="header">
        <xs:choice>
            <xs:sequence>
                <xs:element name="leftAlignedColumns" type="listOfColumns" minOccurs="1" maxOccurs="1" />
                <xs:element name="rightAlignedColumns" type="listOfColumns" minOccurs="0" maxOccurs="1" />
            </xs:sequence>
            <xs:sequence>
                <xs:element name="leftAlignedColumns" type="listOfColumns" minOccurs="0" maxOccurs="1" />
                <xs:element name="rightAlignedColumns" type="listOfColumns" minOccurs="1" maxOccurs="1" />
            </xs:sequence>
        </xs:choice>
    </xs:complexType>

开始抛出以下错误:

org.xml.sax.SAXParseException; lineNumber: 41; columnNumber: 35; cos-nonambig: "http://www.yyy.com/xxx/2015/CommonContainerSchema":leftAlignedColumns and "http://www.yyy.com/xxx/2015/CommonContainerSchema":leftAlignedColumns (or elements from their substitution group) violate "Unique Particle Attribution". During validation against this schema, ambiguity would be created for those two particles.
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:396)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
        at com.sun.org.apache.xerces.internal.impl.xs.XSConstraints.reportSchemaError(XSConstraints.java:341)
        at com.sun.org.apache.xerces.internal.impl.xs.XSConstraints.fullSchemaChecking(XSConstraints.java:498)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:579)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:538)
        at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:255)
        at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:638)
        at com.yyy.xxx.desktop.common.layout.transformer.LayoutTransformer.<clinit>(LayoutTransformer.java:53)
        at com.yyy.xxx.desktop.common.layout.manager.LayoutManager.getLayout(LayoutManager.java:96)
        at com.yyy.xxx.desktop.common.layout.manager.LayoutManagerTest.testLayoutGETRest(LayoutManagerTest.java:55)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at junit.framework.TestCase.runTest(TestCase.java:176)
        at junit.framework.TestCase.runBare(TestCase.java:141)
        at junit.framework.TestResult$1.protect(TestResult.java:122)
        at junit.framework.TestResult.runProtected(TestResult.java:142)
        at junit.framework.TestResult.run(TestResult.java:125)
        at junit.framework.TestCase.run(TestCase.java:129)
        at junit.framework.TestSuite.runTest(TestSuite.java:252)
        at junit.framework.TestSuite.run(TestSuite.java:247)
        at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

当我尝试时:

<xs:complexType name="header">
        <xs:choice minOccurs="1" maxOccurs="2">
            <xs:element name="leftAlignedColumns" type="listOfColumns" minOccurs="0" maxOccurs="1" />
            <xs:element name="rightAlignedColumns" type="listOfColumns" minOccurs="0" maxOccurs="1" />
        </xs:choice>
    </xs:complexType>
  

即使我没有在标题

中传递任何元素,它也会通过

1 个答案:

答案 0 :(得分:2)

您尚未正确实施mentioned question的解决方案。

这将是一个正确的实施:

<xs:complexType name="header">
        <xs:choice>
            <xs:sequence>
                <xs:element name="leftAlignedColumns" type="listOfColumns" minOccurs="1" maxOccurs="1" />
                <xs:element name="rightAlignedColumns" type="listOfColumns" minOccurs="0" maxOccurs="1" />
            </xs:sequence>
            <xs:sequence>
                <xs:element name="rightAlignedColumns" type="listOfColumns" minOccurs="1" maxOccurs="1" />
            </xs:sequence>
        </xs:choice>
    </xs:complexType>