Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining an XSD so that XML element will be type="array"

Tags:

arrays

xml

xsd

My question relates to defining an XSD document. My specific issue is how to define the XSD so that when the XML is generated an element will have type="array".

Desired result would be something like:

<names type="array">
  <name>
  ......
  </name>
</names>

I have experimented using methods recommended on several forums, but from I have found it seems to me like there may not even be a type for array, which confuses me since a resulting XML element can have a type of array.

like image 351
npersad84 Avatar asked Oct 31 '22 03:10

npersad84


1 Answers

There are tools that will take an XSD and generate a sample XML document that adheres to the XSD, but you should understand that the primary purpose of an XSD is to validate an XML document.

This XSD will validate your XML document:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="names">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="type"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

Note also that using a type="array" attribute-value pair is unconventional in the XML as type information is conveyed in the XSD in the content model for names and needn't be repeated explicitly in the XML document.

like image 124
kjhughes Avatar answered Nov 09 '22 07:11

kjhughes