Mixed Content

It is actually possible to specify that an element can contain both PCDATA and other elements. Such a content model is called mixed. To specify a mixed-content model, just list #PCDATA along with the child elements you want to allow:

Listing ch03_05.xml
 <?xml version = "1.0" standalone="yes"?> <!DOCTYPE DOCUMENT [ <!ELEMENT DOCUMENT (CUSTOMER)*> <!ELEMENT CUSTOMER (NAME,DATE,ORDERS)> <!ELEMENT NAME (LAST_NAME,FIRST_NAME)> <!ELEMENT LAST_NAME (#PCDATA)> <!ELEMENT FIRST_NAME (#PCDATA)> <!ELEMENT DATE (#PCDATA)> <!ELEMENT ORDERS (ITEM)*> <!ELEMENT ITEM (PRODUCT, NUMBER, PRICE)>  <!ELEMENT PRODUCT (#PCDATA  PRODUCT_ID)*>  <!ELEMENT NUMBER (#PCDATA)> <!ELEMENT PRICE (#PCDATA)> <!ELEMENT PRODUCT_ID (#PCDATA)> ]> <DOCUMENT>     <CUSTOMER>         <NAME>             <LAST_NAME>Smith</LAST_NAME>             <FIRST_NAME>Sam</FIRST_NAME>         </NAME>         <DATE>October 15, 2003</DATE>         <ORDERS>             <ITEM>  <PRODUCT>Tomatoes</PRODUCT>  <NUMBER>8</NUMBER>                 <PRICE>.25</PRICE>             </ITEM>             <ITEM>  <PRODUCT>   <PRODUCT_ID>   124829548702121   </PRODUCT_ID>   </PRODUCT>  <NUMBER>24</NUMBER>                 <PRICE>.98</PRICE>             </ITEM>         </ORDERS>     </CUSTOMER>     <CUSTOMER>         <NAME>             <LAST_NAME>Jones</LAST_NAME>             <FIRST_NAME>Polly</FIRST_NAME>         </NAME>         <DATE>October 20, 2003</DATE>         <ORDERS>             <ITEM>  <PRODUCT>Bread</PRODUCT>  <NUMBER>12</NUMBER>                 <PRICE>.95</PRICE>             </ITEM>             <ITEM>  <PRODUCT>Apples</PRODUCT>  <NUMBER>6</NUMBER>                 <PRICE>.50</PRICE>             </ITEM>         </ORDERS>     </CUSTOMER>     <CUSTOMER>         <NAME>             <LAST_NAME>Weber</LAST_NAME>             <FIRST_NAME>Bill</FIRST_NAME>         </NAME>         <DATE>October 25, 2003</DATE>         <ORDERS>             <ITEM>  <PRODUCT>Asparagus</PRODUCT>  <NUMBER>12</NUMBER>                 <PRICE>.95</PRICE>             </ITEM>             <ITEM>  <PRODUCT>Lettuce</PRODUCT>  <NUMBER>6</NUMBER>                 <PRICE>.50</PRICE>             </ITEM>         </ORDERS>     </CUSTOMER> </DOCUMENT> 

However, there is a big drawback to using the mixed-content model: You can specify only the names of the child elements that can occur. You cannot set the child elements' order or number of occurrences. And inside the mixed-content model, you cannot use the + , * , or ? operators.

Because of these severe restrictions, I suggest avoiding the mixed-content model. You're almost always better off declaring a new element that can hold PCDATA and including that in a standard content model instead.

Why Use the Mixed Content Model?

One possible reason to use the mixed-content model is when you're translating simple text documents into XML. Using the mixed-content model can handle the case in which part of the document is in XML and part is in simple text.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net