Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the XML schema to represent a string-to-string dictionary?

Tags:

xml

xpath

xsd

Suppose I have a Dictionary<string, string> instance, like this one:

{ { "a", "1" }, { "b", "2" } }

There are several ways to represent it in XML:

<root>
  <item>
    <key>a</key>
    <value>1</value>
  </item>
  <item>
    <key>b</key>
    <value>2</value>
  </item>
</root>

OR

<root>
  <item key="a" value="1"/>
  <item key="b" value="2"/>
</root>

OR

<root>
  <a>1</a>
  <b>2</b>
</root>

What is the XML schema to describe each of the representations?

Thanks.

like image 602
mark Avatar asked Oct 10 '22 12:10

mark


1 Answers

Look at xs:unique element. For 1st XML appropriate XSD is:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="item">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="key" type="xs:string"  />
                            <xs:element name="value" type="xs:string" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>

        <xs:unique name="uniqueItem">
            <xs:selector xpath="item"/>
            <xs:field xpath="key"/>
        </xs:unique>
    </xs:element>
</xs:schema>

For 2nd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="item">
                    <xs:complexType>
                        <xs:attribute name="key" type="xs:string" use="required" />
                        <xs:attribute name="value" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>

        <xs:unique name="uniqueItem">
            <xs:selector xpath="item"/>
            <xs:field xpath="@key"/>
        </xs:unique>

    </xs:element>
</xs:schema>

For 3rd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="a" type="xs:string" />
                <xs:element name="b" type="xs:string" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
like image 68
Kirill Polishchuk Avatar answered Nov 02 '22 23:11

Kirill Polishchuk