XSD: Nested XML with a choice on the root element

I've got a pair of XML's that I would to validate with one XSD:

<TestCase name="TC1">
     ...
</TestCase>

<TestSuite name="TS1">
     <TestCase name="TC1">
          ...
     </TestCase>
     <TestSuite name="TS2">
          <TestCase name="TC2">
               ...
          </TestCase>
     </TestSuite>
</TestSuite>

A TestSuite can simply contain 0 or more TestCases and TestSuites (in any order).

Is it possible (or advisable) to validate both the TestCase xml and the TestSuite xml with the same xsd?
forloopAsked:
Who is Participating?
 
rdcproConnect With a Mentor Commented:
Sure, you can do this.

<?xml version="1.0" encoding="UTF-8"?>
<TestCase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="F:\msharp\Pro Bono\schematest\test.xsd" name="TC3">
      <foo>Tarfu</foo>
</TestCase>


and

<TestSuite name="TS1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="F:\msharp\Pro Bono\schematest\test.xsd">
      <TestCase name="TC1"><foo>bar</foo></TestCase>
      <TestSuite name="TS2">
            <TestCase name="TC2"><foo>snafu</foo></TestCase>
      </TestSuite>
</TestSuite>

both validate against:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
      <xs:complexType name="TestCaseType">
            <xs:sequence>
                  <xs:element name="foo" type="xs:string" />
            </xs:sequence>
            <xs:attribute name="name" use="required" type="xs:NMTOKEN"/>
      </xs:complexType>
      <xs:complexType name="TestSuiteType">
            <xs:sequence>
                  <xs:element name="TestCase" type="TestCaseType"/>
                  <xs:element name="TestSuite" type="TestSuiteType" minOccurs="0" maxOccurs="1"/>
            </xs:sequence>
            <xs:attribute name="name" use="required" type="xs:NMTOKEN"/>
      </xs:complexType>
      <xs:element name="TestCase" type="TestCaseType"/>
      <xs:element name="TestSuite" type="TestSuiteType"/>
</xs:schema>


Regards,
Mike Sharp
0
All Courses

From novice to tech pro — start learning today.