XSD: model properties (name / value pairs)

I have properties defining XML:

<properties>
    <property name="play_sound" value="true" />
    <property name="duration" value="30" />
</properties>

Is it possible, using an XML schema, to specify conditions such as "if the property name is" duration ", then the value must be integer." I think this is not possible, what is the recommended way to model such information in an XML file?

I was thinking of something like:

<properties>
    <play_sound>true</play_sound>
    <duration>30</duration>
</properties>

That way, I can define type restrictions in my schema. But what happens if I have hundreds of different properties that are likely to grow in the future ...?

Thanks.

+3
source share
4 answers

XML Schema 1.0 has no such restrictions.

XML Schema 1.1, , .

Saxon XSLT/XQuery, XML Schema 1.1, .

- Schematron.

. XML , .

+1

XML , .

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="properties">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="play_sound" type="xs:boolean"/>
                <xs:element name="duration" type="xs:byte"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

<properties>
    <play_sound>true</play_sound>
    <duration>30</duration>
</properties>
+2

, . , - Schematron, .

. , , , XML Xsd.

+1
source

XSD does not have conditional support to do what you want. Is it absolutely important for you that your XML is parsed?

If so, your only options define properties as types (for example, you described) or as attributes (for example, <my_object play_sound="true" duration="30"/>)

Usually, however, it doesn’t matter if validation is performed at a later stage (for example, when your XML is being digested by your application), at what point it is easy enough to do what you want.

+1
source

Source: https://habr.com/ru/post/1713011/


All Articles