views:

27

answers:

1

If I have some xml like this:

<mynode>
    <mysubnode>
        <mysubsubnode>hello world</mysubsubnode>
        some more text
    </mysubnode>
</mynode>

As you can see, mysubnode contains both a subnode and some text data.

What I want to know is, is it possible to prevent this happening in a schema? I don't want nodes to contain subnodes and text, just subnodes or text.

Is there an option in my xsd I can specify to force this?

My program to that uses this xml is written in .NET, so I'll tag it as well incase there's anything of use in .net that I can utilise for this, though I'd much rather that the issue was fixed in the schema itself.

Cheers

+1  A: 

Edit: In re-reading the question, the OP expects to use XSD, not DTD (which btw is a good thing!). I intially though that DTD was requested; here is the XML Schema (XSD) version

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;

  <xs:complexType name="mysubnodetype" mixed="false">
    <xs:sequence>
      <xs:element name="mysubsubnode" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="mynodeType" mixed="false">
    <xs:sequence>
      <xs:element name="mysubnode" type="mysubnodetype"/>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="mynodeType" type="mynodeType"/>
</xs:schema>

Notes: the mixed="false" attribute added to the complexTypes is redundant, since by default complexType mixed mode is false (hence, by default, preventing the mixing of elements and text between elements.)


(original answer, with DTD instead)
The following DTD would prevent this. Note the fact that mysubnode can only contain a subsubnode, there is no reference of PCDATA w/r mysubnode and therefore the "some more text" of the XML snippet in the question would be invalid.

<!ELEMENT mynode (mysubnode)>
<!ELEMENT mysubnode  (mysubsubnode)>
<!ELEMENT mysubsubnode (#PCDATA)>

To make the XML snippet of the question valid, something like this would be needed

<!ELEMENT mynode (mysubnode)>
<!ELEMENT mysubnode   (#PCDATAT | mysubsubnode)>
<!ELEMENT mysubsubnode (#PCDATA)>
mjv
@mjv What about if I wanted to apply this setting globally, to all nodes in my xml file?
AndyC
@AndyC: When you define the schema explicitly (i.e. a closed schema), there's no additional effort in specifying that simpleTypes can't contain elements (impossible!) and complexTypes contain text (mixed=false by default). However, if you define an open schema, it may be possible to restrict say the root element to be an open sequence of complexTypes and/or simpleTypes where the complexTypes are restricted to not allowing mixed. I'll check into this...
mjv
@mjv Thanks! :)
AndyC