English 中文(简体)
如何限制复杂情况 XML Schema的具体价值类型?
原标题:How to restrict complexType to a specific value in XML Schema?

我试图在XML Schema实行某种限制的构成形式。

页: 1 在其他几个网站(complexTypes)中重新使用。 我愿具体指出,<代码>外勤人员在<条码>内列入<>PartialType时,必须具有一个价值,并在列入<条码>FullTypeB时具有不同价值。

<xs:complexType name="PartialType">
    <xs:sequence>
        <xs:element name="FieldA" type="xs:unsignedShort" />
        <xs:element name="FieldB" type="xs:unsignedShort" />
    </xs:sequence>
</xs:complexType>

<!-- FieldA must be 1 for FullTypeA-->
<xs:complexType name="FullTypeA">
    <xs:sequence>
        <xs:element ref="PartialType" />
        <xs:element name="FieldC" type="xs:unsignedShort" />
    </xs:sequence>
</xs:complexType>

<!-- FieldA must be 2 for FullTypeB-->
<xs:complexType name="FullTypeB">
    <xs:sequence>
        <xs:element ref="PartialType" />
        <xs:element name="FieldD" type="xs:unsignedShort" />
    </xs:sequence>
</xs:complexType>

How should I structure my XML Schema to accommodate this scenario?

最佳回答

我认为,Xsd确实不允许这样做。

实现这一目标的一个可能途径是使用<条码>x:限制/条码>。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="PartialType">
        <xs:sequence>
            <xs:element name="FieldA" type="xs:unsignedShort" />
            <xs:element name="FieldB" type="xs:unsignedShort" />
            <xs:choice>
                <xs:element name="FieldC" type="xs:unsignedShort" />
                <xs:element name="FieldD" type="xs:unsignedShort" />
            </xs:choice>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="FullTypeA">
        <xs:complexContent>
            <xs:restriction base="PartialType">
                <xs:sequence>
                    <xs:element name="FieldA" type="xs:unsignedShort" fixed="1" />
                    <xs:element name="FieldB" type="xs:unsignedShort" />
                    <xs:element name="FieldC" type="xs:unsignedShort" />
                </xs:sequence>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="FullTypeB">
        <xs:complexContent>
            <xs:restriction base="PartialType">
                <xs:sequence>
                    <xs:element name="FieldA" type="xs:unsignedShort" fixed="2" />
                    <xs:element name="FieldB" type="xs:unsignedShort" />
                    <xs:element name="FieldD" type="xs:unsignedShort" />
                </xs:sequence>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>

</xs:schema>

www.un.org/spanish/ecosoc 与此相对照的是: 页: 1

另一种办法是使用<代码>xs:extension。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="PartialType">
        <xs:sequence>
            <xs:element name="FieldB" type="xs:unsignedShort" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="FullTypeA">
        <xs:complexContent>
            <xs:extension base="PartialType">
                <xs:sequence>
                    <xs:element name="FieldA" type="xs:unsignedShort" fixed="1" />
                    <xs:element name="FieldB" type="xs:unsignedShort" />
                    <xs:element name="FieldC" type="xs:unsignedShort" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="FullTypeB">
        <xs:complexContent>
            <xs:extension base="PartialType">
                <xs:sequence>
                    <xs:element name="FieldA" type="xs:unsignedShort" fixed="2" />
                    <xs:element name="FieldB" type="xs:unsignedShort" />
                    <xs:element name="FieldD" type="xs:unsignedShort" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

</xs:schema>
问题回答

暂无回答




相关问题
How to validate DataTable with XML Schema?

Does anyone know how to validate a DataTable against a particular DataTable XSD (Schema)? Which would be the best performance-optimized way of doing this? For example: <?xml version="1.0" ...

Deprecated XML validation code problem C#

I m trying to figure out how to correct his deprecated xml schema validation code. public static bool ValidateXml(string xmlFilename, string schemaFilename) { ⁞ //Forward stream reading ...

Getting an object representation of an .XSD file in Java

What is the easiest way to obtain a statically typed representation of an XML schema (.XSD) in Java? More specifically I want to be able to programatically traverse all defined simpleType:s and ...

xmlserializer validation

I m using XmlSerializer to deserialize Xml achives. But I found the class xsd.exe generated only offers capability to read the xml, but no validation. For example, if one node is missing in a document,...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签