开发者

Stop node containing subnodes and text in schema

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 xs开发者_高级运维d 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


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">

  <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)>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜