Дата XSD с атрибутом, но необязательным значением - PullRequest
0 голосов
/ 28 января 2012

Как мне указать значение даты XSD, которое является необязательным?Есть ли способ, которым я мог бы избежать использования nillable?

Например, оба

<element attribute="attribute">optional-value</element>
<element attribute="attribute"/>

являются допустимыми типами, где "необязательное значение" должно быть определено как тип xsd: date.

1 Ответ

3 голосов
/ 28 января 2012

Да, но не с такими инструментами, как:

XSD:

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="root">
        <xsd:complexType>
            <xsd:simpleContent>
                <xsd:extension base="EmptyDate">
                    <xsd:attribute name="attribute" type="xsd:string" use="required"/>
                </xsd:extension>
            </xsd:simpleContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:simpleType name="EmptyDate">
        <xsd:union memberTypes="xsd:date emptyString"/>
    </xsd:simpleType>

    <xsd:simpleType name="emptyString">
        <xsd:restriction base="xsd:string">
            <xsd:length value="0"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>

Неверный XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attribute="attribute1" xmlns="http://tempuri.org/XMLSchema.xsd"> </root>

Допустимый XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attribute="attribute1" xmlns="http://tempuri.org/XMLSchema.xsd"/>
...