Nayan
Nayan

Reputation: 327

XML Schema validation using the XSD

I have an XML which looks like below in which the numbers of Items can vary from 0 to n. Is there a way to write XSD for verifying the Schema .

<?xml version="1.0" encoding="utf-8" ?> 
<ShoppingItems> 
  <CustomerName>John</CustomerName> 
  <Address>Walstreet,Newyork</Address> 
  <Item1>Milk</Item1> 
  <Price1>1$</Price1> 
  <Item2>IceCream</Item2> 
  <Price2>1$</Price2> 
  <Item3>Bread</Item3> 
  <Price3>1$</Price3> 
  <Item4>Egg</Item4> 
  <Price4>1$</Price4>   

  <Item..n>Egg</Item..n> 
  <Price..n>1$</Price..n> 
</ShoppingItems> 

Upvotes: 0

Views: 110

Answers (1)

tom redfern
tom redfern

Reputation: 31780

Not in it's current form. An XSD definition is very strict - in the above case you would have specify every possible ShoppingItems type (those including Item..n and Price..n) which is of course not possible.

What would be better is to change the XML file so that it is more logically structured:

<?xml version="1.0" encoding="utf-8" ?> 
<ShoppingItems> 
  <CustomerName>John</CustomerName> 
  <Address>Walstreet,Newyork</Address> 
  <Items>
    <Item price="1$">Milk</Item> 
    <Item price="3$">IceCream</Item>
    <Item price="1$">Bread</Item> 
    <Item price="1.5$">Egg</Item> 
  </Items>
</ShoppingItems> 

It is now completely possible to define this document with a schema.

Upvotes: 1

Related Questions