Solved

XSD: Nested XML with a choice on the root element

Posted on 2004-10-21
335 Views
Last Modified: 2013-11-19
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?
0
Question by:forloop
    1 Comment
     
    LVL 26

    Accepted Solution

    by:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
    Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
    The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
    HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now