1

I have below xml which basically contains test data required for automation tests.We were earlier using xls but since it is difficult for version controlling in GIT , we want to move to XML. Now , issue is - in XLS all rows are always consistent(they have same header) but in XML it is possible that member1 adds row-1 with 2 parameters and member2 adds row-2 with 4 parameters. as number of rows increases , it is difficult to see which parameters already exists.In Excel we can simply check header. I would like to restrict xml to have same parameters for all rows under same Datasheet node. Can someone tell me how to create XSD for this? I am new to xml.

<?xml version = "1.0"? >
<Workbook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Sample.xsd" >
   <Datasheet name = "sheet1">
    <Row rowid="1">
      <firstname>dinkar</firstname>
      <lastname>kad</lastname>
      <nickname>dinkar</nickname>
      <marks>85</marks>
   </Row>
   <Row rowid="1">
      <firstname>dinkar</firstname>
      <lastname>kad</lastname>
      <nickname>dinkar</nickname>
      <marks>85</marks>
   </Row>
   <Row rowid="2">
      <firstname>dinkar</firstname>
      <lastname>kad</lastname>
      <nickname>dinkar</nickname>
      <marks>85</marks>
   </Row>
   </Datasheet>

   <Datasheet name = "sheet2">
    <Row rowid="2">
      <firstname2>Vaneet</firstname2>
      <lastname2>Gupta</lastname2>
      <nickname2>vinni</nickname2>
      <marks2>95</marks2>
   </Row>
   </Datasheet>


</Workbook>
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
Nilesh G
  • 103
  • 1
  • 9

2 Answers2

0

This is a simple example tutorial to learn how to do it: XSD How To?

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
       <xs:element name="Workbook">
              <xs:complexType>
                     <xs:sequence>
                            <xs:element name="Datasheet" maxOccurs="unbounded">
                                   <xs:complexType>
                                          <xs:sequence>
                                                 <xs:element name="Row" maxOccurs="unbounded">
                                                        <xs:complexType>
                                                               <xs:sequence>
                                                                      <xs:element name="firstname" type="xs:string"></xs:element>
                                                                      <xs:element name="lastname" type="xs:string"></xs:element>
                                                                      <xs:element name="nickname" type="xs:string"></xs:element>
                                                                      <xs:element name="marks" type="xs:int"></xs:element>
                                                                  </xs:sequence>
                                                               <xs:attribute name="rowid" type="xs:int"></xs:attribute>
                                                           </xs:complexType>
                                                    </xs:element>
                                             </xs:sequence>
                                          <xs:attribute name="name" type="xs:string"></xs:attribute>
                                      </xs:complexType>
                               </xs:element>
                        </xs:sequence>
                     <xs:attribute name="xmlns:xsi" type="xs:string"></xs:attribute>
                     <xs:attribute name="xsi:noNamespaceSchemaLocation" type="xs:string"></xs:attribute>
                 </xs:complexType>
          </xs:element>
   </xs:schema>
it4Astuces
  • 432
  • 5
  • 17
  • I have already looked into similar pattern in another question and I believe , it will not help us. Because as per XSD all row elements need to have firstname,lastname,nickname,marks. In my case row in another Datasheet can have altogether differant columns. Rule is - All rows under same datasheet should have same parameters. But rows in another datasheet node can be different. There is similar example at https://stackoverflow.com/questions/47866026/xsd-conditional-type-assignment-default-type-confusion but when I am validating xml and xsd in example it is giving error.I need XSD for my XML. – Nilesh G Jan 18 '19 at 11:51
0

In XSD 1.1 you can require all rows to be consistent with:

<xs:element name="rows">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="row" maxOccurs="unbounded"/>
        <xs:complexType>
          <xs:sequence>
            <xs:any maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:assert test="every $h in head(rows), $t in tail(rows) 
                     satisfies deep-equal($h/*/node-name(.), $t/*/node-name(.)"/>
  </xs:complexType>
</xs:element>

It's not possible to define this constraint with XSD 1.0.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164