Introduction to XML by ewghwehws

VIEWS: 6 PAGES: 18

									Introduction to XML



 The eXtensible Markup Language
                    HTML Vs. XML
• HTML is a markup language
   – Fixed set of tags
   – Tags describe fixed set of elements
   – Covers formatting, semantics and structure
       • <b>; <STRONG>; <TD>
• XML is a meta-markup language
   – No fixed set of tags – all user defined
   – Describes structure and semantics – not formatting
      • Formatting is done by use of “style sheets” stored in a separate
        file
            XML Design Objectives
• W3C established 10 goals for XML
   –   Easily used on internet
   –   Must support wide variety of applications
   –   Compatible with SGML
   –   Easy to write programs that process XML document
   –   Minimal optional features (simplicity)
   –   Easy to understand by lay people
   –   XML design should prepared quickly
   –   Design of XML must be precise
   –   XML documents easy to create
   –   Terseness not important
   Creating an XML document: The Process




Create XML document

                 Parse document
                 using XML
                 processor


                                  Display document
            XML – First Try
<?xml version=“1.0”?>
<GREETING>
  Hello, World
</GREETING>
          The XML Declaration
• <?xml version=“1.0”?>
  – The tag is xml and version is the attribute
  – 1.0 is version of xml to which document conforms
  – Always start with this declaration
  – Note: it is case-sensitive
  – No white spaces allowed
                     The Element
                       <GREETING>
                          Hello, World
                      </GREETING>
• Above three lines form the GREETING element
   – <GREETING> - start tag
   – Hello, World – element content
   – </GREETING> -end tag
          Inserting Comments
• <!--This is a comment which will not be
  displayed-->
                <GREETING>
                  Hello, World
          <!-- This is a test xml file -->
               </GREETING>
                     CDATA Sections
• Used to display text as is without interpretation
        <![CDATA[
            Text to be entered
            ]]>
• may contain most markup characters (<,>,!,&
  etc.)
• May be placed anywhere in the document
• Cannot be nested in another CDATA
• Cannot be empty
      Example of CDATA section
<MESSAGE>
<![CDATA[
  This is a sample of how this tag & others like it may
    be used
  ]]>
<MESSAGE>
    To show formatted elements
• To have the element content show with
  formatting, must do two things
  – Create a stylesheet file
     • Specifies how to interpret tags
  – Add a processing instruction to the xml file.
     • Tells the xml file to look at the stylesheet file for
       formatting instructions
              Adding a stylesheet
• The interpretation of self-defined tags (like <GREETING>)
  done through use of linked CSS or XSL file
• Create another file named GREETING.css
   – Would be called GREETING.xsl for XSL file
• Define the formatting for the GREETING element
• GREETING {display: block; font-size: 16pt; font-weight:
  bold font-style: italic; color: red}
         Processing Instruction
• <?xml-stylesheet type=“text/css”
  href=“greeting.css”?>
• Insert after the opening XML declaration
• Can use same CSS for multiple xml files
• Type is set to “text/xml” if using XSL
• Can also be linked to a URL
           Structuring Data in XML
• Consider data about music
   – Genres of music
       • Hip Hop/Rap; Alternative; Blues; Country; Jazz; Rock; Latin;
         World
   – Singers
       • Hip Hop/Rap
           – Eminem; Busta Rhymes; Nelly; Ja Rule; Missy Elliot
   – Album
      • Busta Rhymes
           – It Ain’t Safe No More
   – Song
      • It Ain’t Safe No More
           – Make It Clap
           Some of the tags
• You can make up your own names – but they
  should be meaningful
  – <MUSIC>
  – <GENRE>
  – <ARTIST>
  – <ALBUM>
  – <SONG>
                Root Element
• Every “good” xml document has a root
  element
• Root elements contain all other elements
• In this case, the <MUSIC> start tag is the first
  tag in the document
• </MUSIC> end tag will be the last
• Tags must be nested , never overlap
    Nested vs Overlapping tags
<CONFERENCE>
  <TEAM>
    <PLAYER>
    </PLAYER>
  </TEAM>
</CONFERENCE>


        Example of NESTED TAGS
   Nested vs Overlapping tags
<CONFERENCE>
<TEAM>
<PLAYER>
</TEAM>
</CONFERENCE>
</PLAYER>


      Example of OVERLAPPING TAGS

								
To top