DTD syntax - allow sub element OR pcdata

1.3k views Asked by At

My .xml structure may have the following data:

<entry id="one-string">
            <class>nmWinMultiReports_main</class>
            <classParams>string</classParams>
         </entry>
<entry id="multiple-elements">
            <class>nmJavaScript_main</class>
            <classParams>
               <pluginid>monitorPlugin</pluginid>
               <bla>string</bla>
               <tag>abc</tag>
            </classParams>
         </entry>

How do I define the .dtd file to allow classParams to either 1. be a string or 2. multiple sub elements (each once)?

I tried:

<!ELEMENT class ( #PCDATA ) >
<!ELEMENT classParams ( #PCDATA | pluginid | bla | tag ) >
         <!ELEMENT pluginid ( #PCDATA ) >
         <!ELEMENT bla ( #PCDATA ) >
         <!ELEMENT tag ( #PCDATA ) >
3

There are 3 answers

1
Shlomo On BEST ANSWER

I made it:

<!ELEMENT classParams ( #PCDATA | pluginid | bla | tag )* >
1
C. M. Sperberg-McQueen On

DTDs cannot enforce the constraint you describe; the easiest way to get something like that constraint is to add a new element (call it string) and declare classParams as taking either string or the sequence of pluginid etc. as children:

<!ELEMENT string (#PCDATA) >
<!ELEMENT classParams (string 
                      | (pluginid, bla, tag))
>

Alternatively, if

<classParams><string>foo</string></classParams> 

seems too heavy, you could declare entry as taking either classParams or classParamString as content:

<!ELEMENT entry (class, (classParams | classParamString)) >
<!ELEMENT classParamString (#PCDATA) >
<!ELEMENT classParams (pluginid, bla, tag) >
1
黄雪晶 On

You can achieve it by this way as well:

<!ELEMENT classParams ANY>