XML Element Content

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anii
    New Member
    • Oct 2007
    • 8

    XML Element Content

    Hi there, I'm new to writing XML (just a couple of weeks old) so please bear with me.

    I was wondering when it would be the right time to use PCDATA in the DTD. I think there are another two data types? CDATA and ANY?

    It says on this website that the difference between the two DATA ones is for parsing and I'm sure sure if in this context that thats what I'd like to do.

    This is what I've got at the moment:
    Code:
    <?xml version="1.0"?>
    
    <!DOCTYPE inventory [
    	<!ELEMENT inventory (vehicle?)>
    	<!ELEMENT vehicle (make, model, km, descr?, photo+)>
    	<!ELEMENT make (#PCDATA)>
    	<!ELEMENT model (#PCDATA)>
    		<!ATTLIST model year ID #REQUIRED>
    	<!ELEMENT km (#PCDATA)>
    		<!ATTLIST km usagetype (private | business) #REQUIRED>
    	<!ELEMENT descr (bodytype, colour)>
    		<!ATTLIST vehicle rego ID #REQUIRED>
    ]>
    
    <inventory>
       <vehicle rego="__">
          <make></make>
          <model year="__"></model>
          <km usagetype="___"></km>
          <descr>
               <bodytype></bodytype>
               <colour></colour>
          </descr>
          <photo></photo>
        </vehicle>
    </inventory>
    I'm in the process of validating it but haven't quite finished doing so. Please let me know if what i've got so far is looking right. All suggestions and feedback is appreciated! =)
  • saran3b2
    New Member
    • Aug 2007
    • 19

    #2
    Hi,

    I can't catch u ur queries. Anyway i found some error in ur xml. I list here for ur reference.

    Element bodytype, colour & photo not be declared.
    Like this

    <!ELEMENT bodytype #PCDATA)>
    <!ELEMENT colour #PCDATA)>
    <!ELEMENT photo #PCDATA)>


    Year declare as id.So we must follow id rule for the year attribute of model. Suppose u want to create attribute like this then u want to change

    <!ATTLIST model year CDATA #REQUIRED>

    else

    <model year="m1"></model>

    Final File:

    <?xml version="1.0"?>

    <!DOCTYPE inventory [
    <!ELEMENT inventory (vehicle?)>
    <!ELEMENT vehicle (make, model, km, descr?, photo+)>
    <!ELEMENT make (#PCDATA)>
    <!ELEMENT model (#PCDATA)>
    <!ATTLIST model year ID #REQUIRED>
    <!ELEMENT km (#PCDATA)>
    <!ATTLIST km usagetype (private | business) #REQUIRED>
    <!ELEMENT descr (bodytype, colour)>
    <!ELEMENT bodytype ( #PCDATA)>
    <!ELEMENT colour (#PCDATA)>
    <!ELEMENT photo (#PCDATA)>
    <!ATTLIST vehicle rego ID #REQUIRED>
    ]>

    <inventory>
    <vehicle rego="__">
    <make></make>
    <model year="cr2"></model>
    <km usagetype="busi ness"></km>
    <descr>
    <bodytype></bodytype>
    <colour></colour>
    </descr>
    <photo></photo>
    </vehicle>
    </inventory>
    ---------------------------------------------------------------------------------------------------------------

    Comment

    • anii
      New Member
      • Oct 2007
      • 8

      #3
      Hi there! Thanks a bunch for your quick reply.

      Just wondering why you used PCDATA when declaring the elements
      eg. <!ELEMENT make (#PCDATA)>
      <!ELEMENT model (#PCDATA)>

      instead of the others this website has:
      #CDATA means the element contains character data that is not supposed to be parsed by a parser.
      #PCDATA means that the element contains data that IS going to be parsed by a parser.
      The keyword ANY declares an element with any content.

      If a #PCDATA section contains elements, these elements must also be declared.
      I mean like how did you know to choose PCDATA for all of them?

      Also thank you for the code that you've put up. =) Its really helpful.

      Comment

      Working...