2
votes

I am facing the following problem. I need to create an XSD schema of my XML file. Let's say that I have several "Conversation Objects" nodes:

  • Message
  • End
  • Effect

which are simple elements that I am able to describe in my XSD. Then I have a special one called:

  • yesOrNoAnswer

This kind of element has the following structure:

<yesOrNoAnswer actor="" bar="" points="" percentage=""> message text
    <yes>
    ...
    </yes>
    <no>
    ...
    </no>
</yesOrNoAnswer>

Inside those "yes" and "no" nodes I can use repeatedly any of the simple "Conversation Objects" defined at the beginning, or also the "yesOrNoAnswer" node, allowing nesting in as many levels as I want. How can I define the whole "yesOrNoAnswer" node in XSD (including attributes, text, "yes" and "no" nodes) ? Thanks in advance!

1

1 Answers

2
votes

XSD 1.0

This XSD will represent the constraints you describe and validate your XML:

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

  <xs:group name="ConversationObjectGroup">
    <xs:choice>
      <xs:element name="Message"/>
      <xs:element name="End"/>
      <xs:element name="Effect"/>
    </xs:choice>
  </xs:group>

  <xs:element name="yesOrNoAnswer" type="YesOrNoAnswerType"/>

  <xs:complexType name="YesOrNoAnswerType" mixed="true">
    <xs:sequence>
      <xs:element name="yes" type="yesOrNoType"/>
      <xs:element name="no" type="yesOrNoType"/>
    </xs:sequence>
    <xs:attribute name="actor"/>
    <xs:attribute name="bar"/>
    <xs:attribute name="points"/>
    <xs:attribute name="percentage"/>
  </xs:complexType>

  <xs:complexType name="yesOrNoType" mixed="true">
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:group ref="ConversationObjectGroup"/>
      <xs:element ref="yesOrNoAnswer"/>
    </xs:choice>
  </xs:complexType>

</xs:schema>