6

A product can comply with zero or several standards, eg STD1, STD2, STD3.

The XML has a optional field, let's call it complies.

Can I make something like that? (Here I use a comma.)

<complies>STD1,STD3</complies> or <complies>STD2</complies>

And how can I define this XSD type?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Minstrel
  • 369
  • 1
  • 2
  • 14

1 Answers1

6

The right way to design the XML structure for an element that has multiple values would be to individually tag each such value, standard elements in this case:

<complies>
  <standard>STD1</standard>
  <standard>STD2</standard>
</complies>

This will allow XML schemas (XSD, DTDs, etc) to validate directly. Here's the trivial XSD for such structure:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="complies">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="standard" minOccurs="0" maxOccurs="unbounded">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="STD1"/>
              <xs:enumeration value="STD2"/>
              <xs:enumeration value="STD3"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

This approach will also allow you to leverage XML parsers directly and thereby avoid having to micro-parse the complies element.


Update

Alternatively, if you don't want to introduce a separate standard element,

<complies>STD1 STD2</complies>

you could use XSD's xs:list construct:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="complies">
    <xs:simpleType>
      <xs:list>
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="STD1"/>
            <xs:enumeration value="STD2"/>
            <xs:enumeration value="STD3"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:list>
    </xs:simpleType>
  </xs:element>
</xs:schema>

Thanks to John Saunders for this helpful suggestion.

kjhughes
  • 106,133
  • 27
  • 181
  • 240