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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With