无法使用XSD 1.1类型替代工具

时间:2019-02-20 19:17:36

标签: xml validation xsd

我正在尝试在XSD 1.1中编写XML模式文档。具体来说,我正在尝试使用Type Alternative

我的XSD文档如下

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
           targetNamespace="http://shop.accesso.com"
           xmlns="http://shop.accesso.com"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="CONFIG">
        <xs:complexType>
            <xs:sequence>
                <!-- define an element named 'settings' -->
                <xs:element name="settings">
                    <xs:complexType>
                        <xs:attribute name="servlet" type="xs:anyURI" use="required"/>
                        <xs:attribute name="machineId" type="xs:unsignedByte" use="required"/>
                        <xs:attribute name="merchantId" type="xs:unsignedShort" use="required"/>
                        <xs:attribute name="language" type="xs:string"/>
                        <xs:attribute name="locale" type="xs:string"/>
                        <xs:attribute name="maximize" type="xs:boolean"/>
                    </xs:complexType>
                </xs:element>

                <!-- any number of PARAM elements, setting alternative types based on the key attribute value -->
                <xs:element name="PARAM" type="baseParamType" maxOccurs="unbounded">
                    <xs:alternative test="@key='printers'" type="printerParamType"/>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <!-- base PARAM type -->
    <xs:complexType name="baseParamType">
        <xs:attribute name="key" use="required" type="paramKeyTypes"/>
    </xs:complexType>

    <!-- Printer PARAM type -->
    <xs:complexType name="printerParamType">
        <xs:complexContent>
            <!-- extend the base PARAM type -->
            <xs:extension base="baseParamType">
                <!-- the printer PARAM can have a child FORMAT element -->
                <xs:sequence>
                    <xs:element name="FORMAT">
                        <xs:complexType>
                            <xs:attribute name="name" use="required" type="xs:string"/>
                            <xs:attribute name="printer" use="required" type="xs:string"/>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <!-- Normal PARAM -->
    <xs:complexType name="nonPrinterParamType">
        <!-- empty for now -->
    </xs:complexType>

    <!-- valid values for the key attribute of PARAM elements -->
    <xs:simpleType name="paramKeyTypes">
        <xs:restriction base="xs:string">
            <xs:enumeration value="printers"/>
            <xs:enumeration value="dynamic_gateway_config"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

这是正在验证的配置

<?xml version="1.0"?>
<CONFIG xmlns="http://shop.accesso.com"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://shop.accesso.com config.xsd">

    <settings servlet="http://localhost:1813"
              machineId="1"
              merchantId="803"
              language="en"
              locale="en_US"
              maximize="false" />

    <PARAM key="printers">
      <FORMAT name="default" printer="Microsoft XPS Document Writer" />
    </PARAM>

    <PARAM key="use_static_gateway_config" />
</CONFIG>

但是,它的PARAM的FORMAT子元素带有“打印机”键的错误提示

  

此处不允许使用元素格式

1 个答案:

答案 0 :(得分:0)

这是我对撒克逊人的看法:

Schema checking successful. Time: 1784ms. Memory: 53Mb.
Using parser org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser
Processing file:/Users/mike/Desktop/temp/test.xml
Validation error on line 17 column 46 of test.xml:
  FORG0001: Value "use_static_gateway_config" contravenes the enumeration facet "printers,
  dynamic_gateway_conf..." of the type Q{http://shop.accesso.com}paramKeyTypes
  See http://www.w3.org/TR/xmlschema-2/#cvc-complex-type clause 3

如果我在枚举中添加“ use_static_gateway_config”,则一切正常。

所以我的结论是,您的架构和实例文档基本上都很好(可以进行此更正),并且XSD处理器或调用它的方式存在问题。