syker
syker

Reputation: 11272

Including XML Schema Once

Let's say I define the following XML schema, author.xsd:

<?xml version="1.0" encoding="UTF-8"?>   
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">  

<xs:element name="author">       
 <xs:complexType>  
  <xs:sequence>    
     <xs:element name="firstname" type="xs:string"/>      
     <xs:element name="lastname"  type="xs:string"/>     
  </xs:sequence>   
 </xs:complexType>  
</xs:element>     

</xs:schema>  
</xml>

How would I go about just including the XML schema once when defining multiple author elements in my actual XML file? I would like to avoid having to define xmlns:xsi for every author element.

<?xml version="1.0"?>
<author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:noNamespaceSchemaLocation="author.xsd">
    <FirstName>Mark</FirstName>
    <LastName>Twain</LastName>
</author>
<author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:noNamespaceSchemaLocation="author.xsd">
     <FirstName>Awesome</FirstName>
     <LastName>Possum</LastName>
</author>

Upvotes: 0

Views: 88

Answers (1)

Aravind Yarram
Aravind Yarram

Reputation: 80176

From what I understood, you want the xml file to have more than 1 author. If that is the case then you need to define a parent element and have author as the child of it. Parent element acts like a container. So here is the rule of XML

An xml file can have only 1 root element. All other elements should be children of it.

So, I'd change your schema like this

<?xml version="1.0" encoding="UTF-8"?>   
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">  

<xs:element name="authors">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="author" maxOccurs="unbounded">       
        <xs:complexType>  
           <xs:sequence>    
              <xs:element name="firstname" type="xs:string"/>      
              <xs:element name="lastname"  type="xs:string"/>   
           </xs:sequence>   
        </xs:complexType>  
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema> 

NOTE If you want to limit authors to say 100 then change maxOccurs to that value.

Upvotes: 1

Related Questions