Docstoc

xml

Document Sample
xml Powered By Docstoc
					Working with



StudentRockStars.com Xclusive
           What Is XML?
•Extensible    Markup     Language   is    a
 standardized text-based markup language for
 data interchange on the Web.
•It is based on same principles of markup
 languages that make HTML work.
•As with HTML, data is identified using tags
 (identifiers enclosed in angle brackets: <...>).
•Collectively, the tags are known as markup.
•But unlike HTML, XML tags identify the data
 rather than specify how to display it. SRS
         What Is XML?....
• Whereas an HTML tag says something like,
  "Display this data in bold font" (<b>...</b>),
  an XML tag acts like a field name in a
  program.
• It puts a label on a piece of data that
  identifies     it      (for    example,
  <message>...</message>).
• XML tags are not predefined in XML. We
  must define our own tags.
• www.StudentRockStars.com
      History of Markup languages
• The First Markup language (named as GML)
  was created by IBM in 1960’s.
• GML was used to markup internal
  documents at IBM.
• GML was later expanded into SGML, which
  became     a    standard for  information
  exchange.
• SGML is used in many different industries
  for different tasks.
• SGML was adopted as standard by ISO in
  1986. www.StudentRockStars.com
           About HTML
• Developed by Tim Berners Lee and Anders
  Berglund, both researchers at European
  Laboratory of Particle Physics (CERN).
• Purpose was to enable online linking and
  exchange of technical papers.
• Design motto behind HTML was to create a
  language having notations or tags what would
  be embedded in a document to inform a
  program (browser) how to display the text.
• Upgradation to meet growing requirements
  are maintained by W3C.
         XML vs HTML
• Both XML and HTML are applications of
  SGML. www.StudentRockStars.com

• XML is itself a markup language using
  similar tag notation as HTML.
• HTML is limited in some respects.
• HTML is flexible but rules are loose and
  hence require more processing to be
  done by the processing software.
• XML is extension of HTML created as a
  response to the deficiencies of HTML.
        XML vs HTML
• XML is known for its support for allowing a
  developer to define his/her own tags, hence
  flexible            than             HTML.
  www.StudentRockStars.com

• XML is not forgiving and very tight in the
  regard of rules.
• HTML tags specify functionality while XML
  tags identify nature of data.
• XML rules are beneficial for resource
  constrained devices.
• XML can be considered as a meta language.
            XML Features
• XML being a meta language, it allows to define our
  own      tags,     meaningful     to      a    given
  application/implementation.
• Tags could be given meaningful names, which could
  enable simplified searching.
• Similar to defining field names for a data structure,
  developer is free to use any XML tags that make
  sense for a given application.
• XML is described as a mechanism for specifying
  semantics (meaning) of the data.
• It is device or Language independent and can be
  used for Transmitting Data to any device.
• XML documents are well-formed.
XML usage structure
    XML documents contents
• An XML document is composed of
 1.Declarations (prolog, dtd reference)
 2.Elements
 3.Comments
   www.StudentRockStars.com
 4.Entities    (predefined,     custom
   defined, character entities)
             Rules for XML
• Names can contain letters, numbers, and other
  characters
• Names must not start with a number
• Names must not start with xml (or XML or Xml..)
• Names can not contain spaces, punctuation and
  other separation characters (parentheses, square
  brackets and curly brackets) cannot be used for a
  tag name. www.StudentRockStars.com
• Underscore, hyphens and digits can be used for a
  tag name.
• A colon can be used in case of referring xml
  namespace entities.
         XML well-formedness
• For an xml document to be compliant with xml rules:
   • Every opening tag must have a corresponding closing
     tag.
   • If a tag does not need closing tag, it must have a /.
   • Tag names are case sensitive.
Ex:
• <message>
•      <to>you@yourAddress.com</to>
•      <from>me@myAddress.com</from>
•      <subject>XML Is Really Cool</subject>
•      <text>
•   How many ways is XML cool? Let me count the ways...
•      </text>
• </message>
      XML well-formedness Contd..)
• Data between a tag and its matching end
  tag defines an element of the XML data.
• A tag can contain other tags that lets XML
  represent hierarchical data structures.
• In an element, as with HTML, whitespace is
  irrelevant, so elements can use white
  spaces for readability and yet still process
  it easily with a program.
      XML well-formedness Contd..)
• In XML every tag has a closing tag.
• For a tag <to> the </to> tag is not optional.
  Also the <to> element is never terminated
  by any tag other than </to>.
• A nested tag pair cannot overlap with
  another tag.
• All tags are completely nested. So
  <message>..<to>..</to>..</message>         is
  valid,                                   but
  <message>..<to>..</message>..</to>         is
  invalid.
         Tags and Attributes
• Tags can contain attributes - additional information
  included as part of the tag.
Ex:   <message     to="you@yourAddress.com"
                   from="me@myAddress.com"
                   subject="XML Is Really Cool">
             <text>
How many ways is XML cool? Let me count the ways...
             </text>
      </message>
• Unlike HTML, in XML commas between attributes are not
  ignored; if present, they generate a fatal error.
• Unlike HTML, values for XML attributes are always in
  quotation marks, and multiple attributes are never
  separated by commas.
             Empty Tags
• Sometimes it makes sense to have a tag
  that stands by itself. Ex: a tag that flags a
  message as important might be needed.
• It does not enclose any content, hence
  known as an empty tag.
• Empty tags must end with /> instead of >.
• Also a tag containing no data between
  opening and closing tag is equivalent to an
  empty tag. Ex: <message> </message>
  www.StudentRockStars.com
         XML file contents
1.XML Prolog - an XML file starts with a prolog.
  The minimal prolog contains a declaration
  that identifies the document as an XML
  document:      <?xml version="1.0"?>
• The declaration may also contain additional
  information:
    <?xml     version="1.0"
              encoding="ISO-8859-1"
              standalone="yes"?>
• The prolog is same as HTML header except
  that it uses <?..?>
• It is not a mandatory to be specified.
          XML file contents
• version: Identifies version of the XML markup
  language used. This attribute is mandatory.
• encoding: Identifies character set used to
  encode the data. Ex: ISO-8859-1 (default is
  8-bit Unicode: UTF-8.) UTF stands for Unicode
  or UCS (Universal character set) Transformation
  Format. www.StudentRockStars.com
• standalone: Tells whether or not this document
  references an external entity or an external
  data type specification. If there are no external
  references, then "yes" is appropriate.
          XML file contents
2. Comments
Ex: <!-- This is a comment -->
• Double hyphen '--' must not occur within comments.
• Comments cannot be nested.
• Comment        should   not   appear     before  XML
   declaration.
3. Root Tag
• After declarations, every XML file has exactly one
   element, known as root element. Any other
   elements in file are contained within that element.
   Ex:      <?xml version='1.0' encoding='utf-8'?>
            <!-- A SAMPLE set of slides -->
            <slideshow>       </slideshow>
              XML entities
• It is an XML structure (or plain text) that has a
  name.
• It is a sequence of characters used to represent
  another sequence of characters.
• Referencing an entity by name causes its value to
  be inserted into the document in place of the
  entity reference.
• To reference an entity, the entity name is
  surrounded by an ampersand and a semicolon,
  like this: &entityName;
• A developer can define entites in a DTD.
• Entity reference should not have white space.
  Ex: & SF; or &SF ; will cause errors.
Predefined Entities
         XML entities
• Character References entities
  It is an entity that has a hash mark
   followed by the digits that identify
   the character. Ex: &#147;
  The numbers are Unicode values.
        Working with CDATA

• CDATA is used when handling large blocks
  of  XML     that include many    special
  characters.
• CDATA usage is similar to <pre> </pre> tags.
• CDATA section starts       with   <![CDATA[
  and ends with ]]>.
          Working with CDATA
• Ex: <item> <![CDATA[Diagram:
    frobmorten <--------------- fuznaten
      |         <3>              ^
      | <1>                      | <1> = fozzle
      V                          | <2> = framboze
    staten-------------------------+ <3> = frenzle
           <2>
  ]]>     </item>
• Note that text in CDATA section arrives as it was
  written. Since parser doesn't treat angle brackets as
  XML, they don't generate errors as they would
  otherwise. (If the angle brackets weren't in a CDATA
  section, the document would not be well formed.)
           Error types in xml
• Fatal error : It occurs when a document is not well
  formed (Tags are not closed properly), or otherwise
  cannot be processed.
• Error : It occurs when an XML document is not
  valid i.e. it contains tags that are not permitted by
  the DTD, and order of tags does not conform to
  DTD specifications.
          It can also occur if the prolog specified in
  xml cannot be handled by the parser.
     Ex:      <?xml version='1.2' encoding='us-ascii'?>
• Warning : It is generated when the DTD contains
  duplicate definitions.
              They are not necessarily an error, but
  the document author might like to know about it.
       Document Type Definition
• They have a file extension dtd.
• A DTD is a document which serves the
  following purposes:
    Specify the valid tags that can be used in
     a XML document.
    Specify          the        valid      tag
     sequence/arrangements.
    Specifies     whether      whitespace   is
     significant or ignorable.
    The DTD used by a XML document is
     declared after the prolog.
      Document Type Definition
• DTD defines the structure of xml data and
  is similar to class template.
• A DTD contains the following:
  element declarations
  attribute list declarations
  entity                   declarations
   www.StudentRockStars.com
       Writing elements in a DTD

• Elements specify a tag name that is valid in
  a xml file that uses this dtd.
• Specifies the valid sub-tags for a given tag.
• Specifies the number of occurrence of
  sub-tags possible.
• As in HTML, elements         are   the   basic
  building blocks of XML.
        Writing elements in a DTD
• Ex: <!ELEMENT slideshow (slide+)>
    It starts with <!ELEMENT which is followed by
     name of the tag being defined.
    It is further followed by a pair of parentheses,
     where one or more items are specified that
     indicate the valid contents for the tag along with
     tag qualifiers.
• Above example says, slide tag can occur one or
  more times within slideshow tag.
• If tag qualifiers are not used, slide can be used only
  once in slideshow tag.
• Above example says slideshow tag can have only
  have <slide> but not any other tag or text.
           Tag qualifiers
* Indicates zero or more occurrence.

? Indicates Zero or one time occurrence.

+ Indicates one or more occurrence

( ) Indicates a group of expressions to be
matched together.

| Indicates an option.

, Specifies the exact order of occurrence.
       Writing a DTD (Contd…)
• Specifying multiple sub tags:
     <!ELEMENT slide (title, item*) >
     <!ELEMENT slideshow ( image*, title? ) >
• Comma-separated list tells which elements
  are valid and the order they should occur in.
• Tag modifiers can be applied to multiple tags.
    Ex:   <!ELEMENT slide ( (image, title)+ ) >
 The example says every image element in a
 slide must be paired with a title element. The
 + applies to image/title pair to indicate that
 one or more pairs of them can occur.
       Writing a DTD (Contd…)
• Specifying text content for a tag
     <!ELEMENT title (#PCDATA)>
• It specifies title tag can encapsulate only
  parsed character data.
• In PCDATA all text will be parsed and special
  characters will be replaced with their
  corresponding characters.
• CDATA contains character data that is not
  parsed. www.StudentRockStars.com
• # specifies what follows is a special word and
  not a tag.
        Sample element declarations

• <!ELEMENT Book (Title+ , Author*, Publisher? )>
  Indicates that each Book Tag can have One/more
  Title Tag followed by zero/more Author Tags
  followed by zero/one Publisher Tag.

• <!ELEMENT Author (Main_Author, Sub_Author*)>
  Indicates that Author Tag must have one
  Main_Author Tags followed by Multiple Sub_Author
  Tag. www.StudentRockStars.com
       Sample element declarations
• <!ELEMENT Main_Author (FullName |
  PhoneNumber | Email | Country) * >
  Indicates that Main_Author tag can have
  zero/more FullName, PhoneNumber, Email and
  Country tags in any order.

• <!ELEMENT customer
  (name,company?,(fax|email|phone)*, address+)>
  Indicates name must occur once, followed by
  zero/one company, followed by zero/more
  fax/email/phone tags followed by one/more
  address tags.
       Writing a DTD (Contd…)

• Mixed Model:
     <!ELEMENT item (#PCDATA | detail)* >
• The character | indicates an or condition.
• Star (*) at the End indicates that Zero or more
  sequences of either item tag or text can occur in
 the XML file.
• In mixed models, #PCDATA must appear
  first.
      Limitations of DTDs
• PCDATA     usage    does   not   allow
  specifying of the content type.
• DTD offers no sense of hierarchy.
• www.StudentRockStars.com
           Special tag Values in DTD
• Tag definition can have following instead of sub-tags:
• ANY
    Indicates that the tag can contain any other
     defined element or PCDATA.
    Usually used for the root element.
    Elements can occur in any order in such a
     document. www.StudentRockStars.com
    Not recommended to be used.
• EMPTY
    It says that the element contains no contents (and
     consequently no corresponding end-tag)
    Ex: to allow a tag flag used as <flag/> in a xml file
     the DTD entry should be
            <!ELEMENT flag EMPTY>
          Referencing a DTD
• To refer a dtd in a xml file, use DOCTYPE tag.
  Ex:          <!DOCTYPE    slideshow   SYSTEM
  "slideshow.dtd">
• It starts with <!DOCTYPE
• It must occur after XML prolog and before root
  element.

• SYSTEM identifier specifies location of DTD file
  is local.
• Since it does not use prefix such as http:/ or
  file:/, the path is relative to the location of XML
  document.
        Referencing a DTD

• A PUBLIC identifier may be used to specify
  non-local dtd using a unique name, but the
  parser would have to resolve the location.
  Ex: <!DOCTYPE ejb-jar PUBLIC
  'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
       Defining Attributes in the DTD
<!ELEMENT slideshow (slide+)>
<!ATTLIST slideshow
    title     CDATA #REQUIRED
    date      CDATA #IMPLIED
    author    CDATA "unknown“ >

• DTD     tag   ATTLIST     provides     attribute
  definitions.
• The name following ATTLIST specifies the tag
  for which the attributes are being defined.
      Defining Attributes in the DTD
• Each attribute is defined by a series of three
  space-separated values.
• Commas and other separators are not
  allowed.
• First entry among such three values is the
  name of attribute.
• Second entry indicates the type of the data.
• Last entry determines the attribute's default
  value, if any, and tells whether or not the
  attribute             is             required.
  www.StudentRockStars.com
        Defining Attributes in the DTD
•Choices in attributes
Ex: <!ELEMENT slide (title, item*)>
    <!ATTLIST slide    type (tech | exec | all) #IMPLIED >

•When an attribute consists of a parenthesized list of
choices separated by vertical bars, the attribute must
use one of the specified values.
•The above example says that the slide tag can have an
attribute named type, which can be used as follows:
   •type="tech", type="exec", or type="all". No other
   values are acceptable.
   •If the attribute is not specified, an implied value
   will be considered, because #IMPLIED is used.
                  Attribute-Specification
                        Parameters
Specification                    Specifies...
                    The attribute value must be specified
#REQUIRED
                    in the document.
                    The value need not be specified in
#IMPLIED            the document. If it isn't, the
                    application will use a default value.
                    The default value to use if a value is
"default Value"
                    not specified in the document.
                    The value to use. If the document
#FIXED "fixed
                    specifies any value at all, it must be
Value"
                    the same.
       Defining Entities in the DTD
• Ex:
     <!ENTITY product “Java Bean">
     <!ENTITY products “Java Beans">

• The ENTITY tag says that an entity is being
  defined.
• Next comes the name of the entity being
  defined.
• It accomodates changes very well. Change is
  required in only one place.
       Defining Entities in the DTD
• Last part is the substitution string that
  replaces the entity name whenever it is
  referenced in the XML document.
• The substitution string is defined in
  quotes, which are not included when the
  text is inserted into the document.
• To refer the above entity definitions in a
  xml          file        -      &product;
 www.StudentRockStars.com
• Entity can be referenced in element's
  contents as well as in an attribute value.
        More about entities
<!ENTITY lquo "&#147;">
     <!-- Left Double Quote -->

<!ENTITY rquo "&#148;">
     <!-- Right Double Quote -->

<!ENTITY tm "&#153;">
     <!-- Trademark Symbol (TM) -->

<!ENTITY copyr "&#169;">
     <!-- Copyright Symbol -->
      Defining inline dtd’s
1.<!DOCTYPE slideshow
  [
     <!ENTITY product "WonderWidget">
     <!ENTITY products "WonderWidgets">
  ]>
  <tag1> &product; <tag2>
        Referencing External Entities
1. <!ENTITY currentDate SYSTEM
   "http://www.example.com/servlet/Today?fmt=dd-
      MMM-yyyy">
2. <!ENTITY License SYSTEM
   "http://www.mydomain.com/lice.xml">
3. <!ENTITY License SYSTEM “test.txt">


• External    entity  references    are   used
  for replacing a long text that is kept in
  another file.
• They can specify either text or binary data.
       Referencing External Entities
• If they contain text, it is inserted at the
  point of reference and parsed as part of
  the referring document.
• The SYSTEM keyword is used to specify
  that the referenced content exists
  externally.
• The XML processor will parse the content
  of that file as if its content had been
  typed at the location of the entity
  reference.
          Using Namespaces
• In case of using tags having multiple
  definitions, to avoid tag name conflicts, the
  dtds must specify different names.
• If names cannot be directly changed, XML
  namespace technique is applied to resolve
  conflict between elements having multiple
  definitions.
• Primary goal - to let document author tell the
  parser which definition to use. Parser then
  consults appropriate element definition.
           Namespaces in usage
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/xsl">
  <html> <body> <table border="2" bgcolor="yellow">
              <tr> <th>Title</th>         <th>Artist</th>   </tr>
              <xsl:for-each select="CATALOG/CD">
                  <tr>
                     <td><xsl:value-of select="TITLE"/></td>
                     <td><xsl:value-of select="ARTIST"/></td>
                 </tr>
              </xsl:for-each>
  </table> </body> </html>
</xsl:stylesheet> www.StudentRockStars.com

• In the document above most tags are HTML. Non-
  HTML     tags  identified  by   the     namespace
  http://www.w3.org/TR/xsl have the prefix xsl:
          Displaying XML documents
• XML documents can have any tags, the meaning of
  which are not directly understood by browsers.
• Hence there is a need for an explicit mechanism to
  describe how the document should be displayed.
• There are two techniques available to this end:
   CSS (Cascading Style Sheets)
   XSL (eXtensible Stylesheet Language)
• XSL is the preferred style sheet language of XML
• XSL is far more sophisticated than the CSS.
• CSS was designed and is best suited for HTML.
      Introduction to CSS
• Cascading    Style   Sheets   is   an
  extension of HTML in which users can
  redefine the functionality/behavior of
  a tag. www.StudentRockStars.com
          HTML-CSS sample code 1
<html>
<head>
<style type="text/css" >
<!--
   h1{ color:red;    font:14 pt; }
   h2{ color:blue;   font:16 pt; }
   h3{ color:silver; font:18 pt; }
//-->
</style>
</head> www.StudentRockStars.com

<body>
  <h1> red colored text </h1> <br>
  <h2> blue colored text </h2> <br>
  <h3> silver colored text </h3>
</body>
</html>
          HTML-CSS sample code 2
<html>
<head>
<style type="text/css" >
<!--
    SPAN.red{ color:red;       font:14 pt;   }
    SPAN.blue{ color:blue;     font:16 pt;   }
    SPAN.silver{ color:silver; font:18 pt;   }
//-->
</style>
</head> www.StudentRockStars.com

<body>
  <SPAN CLASS="red"> red colored text </SPAN> <br>
   <SPAN CLASS="blue"> blue colored text </SPAN> <br>
   <SPAN CLASS="silver"> silver colored text </SPAN>
</body>
</html>
               HTML-CSS sample code 3
<html>
<head>
<style type="text/css" >
@import url( test.css );
</style>                                                import_css.html
</head>

<body>
   <SPAN CLASS="red"> red colored text </SPAN> <br>
   <SPAN CLASS="blue"> blue colored text </SPAN> <br>
   <SPAN CLASS="silver"> silver colored text </SPAN>
</body>
</html>

SPAN.red{       color:red;      font:14 pt;   }
SPAN.blue{      color:blue;     font:16 pt;   }            test.css
SPAN.silver{    color:silver;   font:18 pt;   }
          HTML-CSS sample code 4
<html>
<head>
<LINK HREF="test.css" REL=STYLESHEET TYPE="text/css" >
</style>
</head>

<body>
  <SPAN CLASS="red"> red colored text </SPAN> <br>
  <SPAN CLASS="blue"> blue colored text </SPAN> <br>
  <SPAN CLASS="silver"> silver colored text </SPAN>
</body>
</html>

SPAN.red{    color:red;      font:14 pt;   }
SPAN.blue{   color:blue;     font:16 pt;   }
SPAN.silver{ color:silver;   font:18 pt;   }
                  XML-CSS sample code 1
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="catalog.css" type="text/css"?>
<CATALOG>
    <CD>
            <TITLE>Empire Burlesque</TITLE>
            <ARTIST>Bob Dylan</ARTIST>
            <COUNTRY>USA</COUNTRY>
            <COMPANY>Columbia</COMPANY>
            <PRICE>10.90</PRICE>
                                                                          catalog_css.xml
            <YEAR>1985</YEAR>
    </CD>
    <CD>
            <TITLE>Hide your heart</TITLE>
            <ARTIST>Bonnie Tyler</ARTIST>
            <COUNTRY>UK</COUNTRY>
            <COMPANY>CBS Records</COMPANY>
            <PRICE>9.90</PRICE>
            <YEAR>1988</YEAR>
    </CD>
    <CD>
            <TITLE>Greatest Hits</TITLE>
            <ARTIST>Dolly Parton</ARTIST>
            <COUNTRY>USA</COUNTRY>
            <COMPANY>RCA</COMPANY>
            <PRICE>9.90</PRICE>
            <YEAR>1982</YEAR>
    </CD>
</CATALOG>

CD{ display: block;     }
TITLE{      color: #FF0000;         font-size: 20pt;    }                        catalog.css
ARTIST{     color: #0000FF;         font-size: 20pt;    }
COUNTRY,PRICE,YEAR,COMPANY{         display: block;     color: #00FFFF;     margin-left: 20pt;   }
          XML-CSS sample code 2
<?xml version="1.0" encoding="ISO-8859-1"?>        home_css.xml
<?xml-stylesheet href="home.css" type="text/css"?>
<page>
  <title>A Simple XML Home Page</title>
  <para>Welcome to one of the few Home Pages written in
  XML!</para>
  <para>This page does not contain much text.</para>
  <para>It is just an example that it is possible to create a
  homepage written in XML!</para>
</page>

title {color:red; font-size: 24pt; font-weight: bold; display:block;}
para {display:block; color:blue; font-size: 12pt; padding-top: 6pt;}
                                               home.css
          Introduction to XSL
• XSL handles two responsibilities:
  XSL Transforming (XSLT)
     o Handling of display/presentation of XML Data.
     o Specifies presentation by transforming XML file
       into a format recognizable by a browser.
  XSL Formatting Objects (XSL-FO)
     o Rearranging of elements.
     o Adding new elements into output file.
     o Removing elements.
     o Sorting. www.StudentRockStars.com
     o Filtering - Test and making decisions about
       which elements to display.
                 XSL Programming
                  (XSL Elements)
• XSL is also XML based – hence begin with xml prolog.
• xsl:stylesheet
    Indicates that this document is a style sheet.
• xsl:value-of
    Specifies the element whose content must be
     extracted and inserted.
• xsl:for-each
    Used to apply a template for each occurrence of
     element(s) in the XML document.
    It uses select attribute to identify element(s) in
     source document to which template should be
     applied. www.StudentRockStars.com
    The order-by attribute defines sorting order.
      o Ex: <xsl:for-each select="CATALOG/CD" order-
        by="+ ARTIST">
         XSL Programming – Filtering
• Ex:
<xsl:for-each select="CATALOG/CD[name=‘rajashekar']">

• Legal filter operators are:
    = (equal)
    =! (not equal)
    &LT& less than
    &GT& greater than www.StudentRockStars.com
       XSL Programming – Filtering

• xsl:if
    Allows conditional evaluation:
    <xsl:if match="[ARTIST='Bob Dylan']">
     ... some output ...
     </xsl:if>
         XSL Programming – IF Else
                (Choose tag)
• xsl:choose xsl:when xsl:otherwise
• Ex:
  <xsl:choose>
     <xsl:when match="[name=‘rajashekar']">
                ... code ...
     </xsl:when>
     <xsl:otherwise>
                ... code ....
     </xsl:otherwise>
  </xsl:choose> www.StudentRockStars.com
          XML-XSL sample code 1
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="one.xsl" ?>
<source>
  <title>XSL</title>
  <author>John Smith</author>                      one.xml
</source> www.StudentRockStars.com


<?xml version="1.0" encoding="ISO8859-1" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

<xsl:template>
   <h1>       <xsl:value-of select="//title" />   </h1>
   <h2>        <xsl:value-of select="//author" />    </h2>
</xsl:template>
                                                   one.xsl
</xsl:stylesheet>
           XML-XSL sample code 2
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="two.xsl" ?>

<source>
  <title>XSL</title>                                 two.xml
  <author>John Smith</author>
</source> www.StudentRockStars.com


<?xml version="1.0" encoding="ISO8859-1" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

  <xsl:template>
   <h2>        <xsl:value-of select="//author"/>   </h2>
   <h1>        <xsl:value-of select="//title"/>  </h1>
  </xsl:template>
</xsl:stylesheet>                                  two.xsl
              XML-XSL sample code 3
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="three.xsl" ?>

<source>
   <bold>Hello, world.</bold>
   <red>I am </red>                                             three.xml
   <italic>fine.</italic>
</source>


<?xml version="1.0" encoding="ISO8859-1" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

  <xsl:template>
   <p>       <b>           <xsl:value-of select="//bold"/>       </b>       </p>
   <p style="color:red">       <xsl:value-of select="//red"/>    </p>
   <p>        <i>          <xsl:value-of select="//italic"/>      </i>      </p>
  </xsl:template>
</xsl:stylesheet>                                               three.xsl
                            XML-XSL sample code 4
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="simple.xsl" ?>
<breakfast_menu>
      <food>
                <name>Belgian Waffles</name>
                <price>$5.95</price>
                <description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
                <calories>650</calories>
      </food>
      <food>
                <name>Strawberry Belgian Waffles</name>
                <price>$7.95</price>
                <description>light Belgian waffles covered with strawberries and whipped cream</description>
                                                                                                               simple.xml
                <calories>900</calories>
      </food>
      <food>
                <name>French Toast</name>
                <price>$4.50</price>
                <description>thick slices made from our homemade sourdough bread</description>
                <calories>600</calories>
      </food>
</breakfast_menu>


<?xml version="1.0" ?>
<HTML xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<BODY STYLE="font-family:Arial, helvetica, sans-serif; font-size:12pt; background-color:#EEEEEE">

  <xsl:for-each select="breakfast_menu/food">
   <DIV STYLE="background-color:teal; color:white; padding:4px">
     <SPAN STYLE="font-weight:bold; color:white"><xsl:value-of select="name"/></SPAN>
     - <xsl:value-of select="price"/>
   </DIV>
   <DIV STYLE="margin-left:20px; margin-bottom:1em; font-size:10pt">
     <xsl:value-of select="description"/>
     <SPAN STYLE="font-style:italic">
                                (<xsl:value-of select="calories"/> calories per serving)
     </SPAN>
   </DIV>
  </xsl:for-each> www.StudentRockStars.com                                                                     simple.xsl
</BODY>
</HTML>
                                 XML-XSL sample code 5
<?xml version="1.0" encoding="ISO8859-1" ?>
<?xml-stylesheet type="text/xsl" href="catalog.xsl"?>
<CATALOG>
 <CD>
  <TITLE>Empire Burlesque</TITLE>
  <ARTIST>Bob Dylan</ARTIST>
  <COUNTRY>USA</COUNTRY>
  <COMPANY>Columbia</COMPANY>
  <PRICE>10.90</PRICE>
  <YEAR>1985</YEAR>
 </CD>
 <CD>
  <TITLE>Hide your heart</TITLE>
  <ARTIST>Bonnie Tylor</ARTIST>                              catalog.xml
  <COUNTRY>UK</COUNTRY>
  <COMPANY>CBS Records</COMPANY>
  <PRICE>9.90</PRICE>
  <YEAR>1988</YEAR>
 </CD>
 <CD>
  <TITLE>Greatest Hits</TITLE>
  <ARTIST>Dolly Parton</ARTIST>
  <COUNTRY>USA</COUNTRY>
  <COMPANY>RCA</COMPANY>
  <PRICE>9.90</PRICE>
  <YEAR>1982</YEAR>
 </CD>
</CATALOG>



<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template>

  <table border="2" bgcolor="yellow" color="blue">
   <tr>      <th>Title</th>      <th>Artist</th>    </tr>
   <xsl:for-each select="CATALOG/CD">
      <tr>
                 <td><xsl:value-of select="TITLE" /></td>
                 <td><xsl:value-of select="ARTIST" /></td>
      </tr>
   </xsl:for-each>
  </table>

</xsl:template>
                                                             catalog.xsl
</xsl:stylesheet>
                                              XML-XSL sample code 6
<?xml version="1.0" encoding="ISO8859-1" ?>
<?xml-stylesheet type="text/xsl" href="choose.xsl" ?>
<CATALOG>
 <CD>
                                                                     <?xml version='1.0'?>
  <TITLE>Empire Burlesque</TITLE>
  <ARTIST>Bob Dylan</ARTIST>                                         <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-
                                                                     xsl">
  <COUNTRY>USA</COUNTRY>
  <COMPANY>Columbia</COMPANY>
  <PRICE>10.90</PRICE>
  <YEAR>1985</YEAR>
 </CD>
                                                                     <xsl:template >
 <CD>
  <TITLE>Hide your heart</TITLE>                                        <table border="2" bgcolor="yellow">
  <ARTIST>Bonnie Tylor</ARTIST>
  <COUNTRY>UK</COUNTRY>
  <COMPANY>CBS Records</COMPANY>
                                                                         <tr>         <th>Title</th>       <th>Artist</th>
                                                                     </tr>
                                                        choose.xml
  <PRICE>9.90</PRICE>
  <YEAR>1988</YEAR>

                                                                         <xsl:for-each select="CATALOG/CD">
 </CD>
 <CD>
  <TITLE>Greatest Hits</TITLE>
  <ARTIST>Dolly Parton</ARTIST>
  <COUNTRY>USA</COUNTRY>
                                                                                 <tr>
  <COMPANY>RCA</COMPANY>
  <PRICE>9.90</PRICE>                                                                        <td><xsl:value-of
  <YEAR>1982</YEAR>
 </CD>
 <CD>
                                                                     select="TITLE"/></td>
  <TITLE>Still got the blues</TITLE>
  <ARTIST>Gary More</ARTIST>                                                                 <xsl:choose>
                                                                                                        <xsl:when
  <COUNTRY>UK</COUNTRY>
  <COMPANY>Virgin redords</COMPANY>
  <PRICE>10.20</PRICE>
  <YEAR>1990</YEAR>
 </CD>
                                                                     match=".[ARTIST='Bonnie Tylor']">
 <CD>
  <TITLE>Eros</TITLE>                                                                                               <td
  <ARTIST>Eros Ramazzotti</ARTIST>
  <COUNTRY>EU</COUNTRY>
  <COMPANY>BMG</COMPANY>
                                                                     bgcolor="#ff0000"><xsl:value-of select="ARTIST"/></td>
  <PRICE>9.90</PRICE>
  <YEAR>1997</YEAR>                                                                                     </xsl:when>
                                                                                                        <xsl:otherwise>
 </CD>
 <CD>
  <TITLE>One night only</TITLE>
  <ARTIST>Bee Gees</ARTIST>
  <COUNTRY>UK</COUNTRY>
  <COMPANY>Polydor</COMPANY>
  <PRICE>10.90</PRICE>                                                           <td><xsl:value-of select="ARTIST"/></td>
  <YEAR>1998</YEAR>
 </CD>
 <CD>
                                                                                                        </xsl:otherwise>
  <TITLE>Sylvias Mother</TITLE>
  <ARTIST>Dr.Hook</ARTIST>                                                                  </xsl:choose>
                                                                                 </tr>
  <COUNTRY>UK</COUNTRY>
  <COMPANY>CBS</COMPANY>
  <PRICE>8.10</PRICE>
  <YEAR>1973</YEAR>
 </CD>
                                                                        </xsl:for-each>
 <CD>
  <TITLE>Maggie May</TITLE>                                            </table>
  <ARTIST>Rod Stewart</ARTIST>
  <COUNTRY>UK</COUNTRY>                                              </xsl:template>
                                                                                                   choose.xsl
  <COMPANY>Pickwick</COMPANY>
  <PRICE>8.50</PRICE>
  <YEAR>1990</YEAR>                                                  </xsl:stylesheet>
 </CD>
 <CD>
  <TITLE>Romanza</TITLE>
  <ARTIST>Andrea Bocelli</ARTIST>
  <COUNTRY>EU</COUNTRY>
  <COMPANY>Polydor</COMPANY>
  <PRICE>10.80</PRICE>
  <YEAR>1996</YEAR>
                                            XML-XSL sample code 7
<?xml version="1.0" encoding="ISO8859-1" ?>
<?xml-stylesheet type="text/xsl" href="filter.xsl" ?>
<CATALOG>                                                            <?xml version='1.0'?>
                                                                     <xsl:stylesheet
 <CD>
  <TITLE>Empire Burlesque</TITLE>
  <ARTIST>Bob Dylan</ARTIST>

                                                                     xmlns:xsl="http://www.w3.org/TR/WD-xsl">
  <COUNTRY>USA</COUNTRY>
  <COMPANY>Columbia</COMPANY>
  <PRICE>10.90</PRICE>
  <YEAR>1985</YEAR>
 </CD>
 <CD>
                                                                     <xsl:template>
  <TITLE>Hide your heart</TITLE>
  <ARTIST>Bonnie Tylor</ARTIST>
  <COUNTRY>UK</COUNTRY>
                                                                      <html>
                                                                      <body>
                                                        filter.xml
  <COMPANY>CBS Records</COMPANY>
  <PRICE>9.90</PRICE>

                                                                        <table border="2" bgcolor="yellow">
  <YEAR>1988</YEAR>
 </CD>
 <CD>

                                                                         <tr>
  <TITLE>Greatest Hits</TITLE>
  <ARTIST>Dolly Parton</ARTIST>
  <COUNTRY>USA</COUNTRY>
  <COMPANY>RCA</COMPANY>
  <PRICE>9.90</PRICE>
  <YEAR>1982</YEAR>
                                                                           <th>Title</th>
 </CD>
 <CD>
  <TITLE>Still got the blues</TITLE>
                                                                           <th>Artist</th>
  <ARTIST>Gary More</ARTIST>
  <COUNTRY>UK</COUNTRY>                                                  </tr>
                                                                         <xsl:for-each select="CATALOG/CD[ARTIST
  <COMPANY>Virgin redords</COMPANY>
  <PRICE>10.20</PRICE>
  <YEAR>1990</YEAR>

                                                                     = 'Bob Dylan']">
 </CD>
 <CD>
  <TITLE>Eros</TITLE>
  <ARTIST>Eros Ramazzotti</ARTIST>
  <COUNTRY>EU</COUNTRY>
  <COMPANY>BMG</COMPANY>
                                                                         <tr>
  <PRICE>9.90</PRICE>
  <YEAR>1997</YEAR>
 </CD>
                                                                           <td><xsl:value-of select="TITLE"/></td>
 <CD>
  <TITLE>One night only</TITLE>                                            <td><xsl:value-of select="ARTIST"/></td>
                                                                         </tr>
  <ARTIST>Bee Gees</ARTIST>
  <COUNTRY>UK</COUNTRY>
  <COMPANY>Polydor</COMPANY>

                                                                         </xsl:for-each>
  <PRICE>10.90</PRICE>
  <YEAR>1998</YEAR>
 </CD>
 <CD>
  <TITLE>Sylvias Mother</TITLE>
  <ARTIST>Dr.Hook</ARTIST>
                                                                        </table>
  <COUNTRY>UK</COUNTRY>
  <COMPANY>CBS</COMPANY>
  <PRICE>8.10</PRICE>
                                                                      </body>
  <YEAR>1973</YEAR>
 </CD>                                                                </html>
                                                                     </xsl:template>
 <CD>
  <TITLE>Maggie May</TITLE>
  <ARTIST>Rod Stewart</ARTIST>

                                                                     </xsl:stylesheet>           filter.xsl
  <COUNTRY>UK</COUNTRY>
  <COMPANY>Pickwick</COMPANY>
  <PRICE>8.50</PRICE>
  <YEAR>1990</YEAR>
 </CD>
 <CD>
  <TITLE>Romanza</TITLE>
  <ARTIST>Andrea Bocelli</ARTIST>
  <COUNTRY>EU</COUNTRY>
  <COMPANY>Polydor</COMPANY>
  <PRICE>10.80</PRICE>
                                     XML-XSL sample code 8
<?xml version="1.0" encoding="ISO8859-1" ?>
<?xml-stylesheet type="text/xsl" href="if.xsl" ?>
<CATALOG>
                                                             <?xml version='1.0'?>
 <CD>
  <TITLE>Empire Burlesque</TITLE>                            <xsl:stylesheet
  <ARTIST>Bob Dylan</ARTIST>
  <COUNTRY>USA</COUNTRY>                                     xmlns:xsl="http://www.w3.org/TR/WD-xsl">
  <COMPANY>Columbia</COMPANY>
  <PRICE>10.90</PRICE>
  <YEAR>1985</YEAR>
                                                             <xsl:template >
 </CD>
 <CD>                                                           <table border="2" bgcolor="yellow">
  <TITLE>Hide your heart</TITLE>
                                                                     <tr>      <th>Title</th>
  <ARTIST>Bonnie Tylor</ARTIST>
  <COUNTRY>UK</COUNTRY>                             if.xml
  <COMPANY>CBS Records</COMPANY>
  <PRICE>9.90</PRICE>
                                                             <th>Artist</th>      </tr>
  <YEAR>1988</YEAR>
 </CD>                                                               <xsl:for-each select="CATALOG/CD">
 <CD>
  <TITLE>Greatest Hits</TITLE>                                         <xsl:if match=".[ARTIST='Bob
  <ARTIST>Dolly Parton</ARTIST>
  <COUNTRY>USA</COUNTRY>
  <COMPANY>RCA</COMPANY>
                                                             Dylan']">
  <PRICE>9.90</PRICE>
  <YEAR>1982</YEAR>                                                         <tr>
 </CD>
 <CD>                                                                             <td><xsl:value-of
  <TITLE>Still got the blues</TITLE>
  <ARTIST>Gary More</ARTIST>
  <COUNTRY>UK</COUNTRY>
                                                             select="TITLE"/></td>
  <COMPANY>Virgin redords</COMPANY>
  <PRICE>10.20</PRICE>                                                            <td><xsl:value-of
  <YEAR>1990</YEAR>
 </CD>                                                       select="ARTIST"/></td>
 <CD>
  <TITLE>Eros</TITLE>
  <ARTIST>Eros Ramazzotti</ARTIST>
                                                                            </tr>
  <COUNTRY>EU</COUNTRY>
  <COMPANY>BMG</COMPANY>                                               </xsl:if>
  <PRICE>9.90</PRICE>
  <YEAR>1997</YEAR>                                                  </xsl:for-each>
 </CD>
 <CD>
  <TITLE>One night only</TITLE>
                                                                </table>
  <ARTIST>Bee Gees</ARTIST>
  <COUNTRY>UK</COUNTRY>                                      </xsl:template>
  <COMPANY>Polydor</COMPANY>
  <PRICE>10.90</PRICE>                                       </xsl:stylesheet>
  <YEAR>1998</YEAR>
 </CD>
 <CD>
  <TITLE>Sylvias Mother</TITLE>
  <ARTIST>Dr.Hook</ARTIST>
  <COUNTRY>UK</COUNTRY>
                                                                                        if.xsl
  <COMPANY>CBS</COMPANY>
  <PRICE>8.10</PRICE>
  <YEAR>1973</YEAR>
 </CD>
 <CD>
  <TITLE>Maggie May</TITLE>
XML Parsing
         Why Parser?
• XML allows us to encode information
  in textual form and structuring it
  using descriptive tags.
• This information must at some point
  be read by some program to do
  something    useful,  like  viewing,
  modifying, printing….
        XML Parser types
• Non-Validating Parser:
  They do not check an xml document
    against any DTD.
  It is used only to check whether the
    document is well-formed or not.
www.StudentRockStars.com
• Validating Parser:
  It checks whether the document is valid.
  It    verifies   whether the  document
    conforms to a specific DTD (either
    internal or external).
       XML parser vs Compiler
• Compilers convert source code into a
  executable program in three steps.
   First, a lexer module groups characters into
    words or tokens that the compiler
    recognizes -- a process known as tokenizing.
   A second module, the parser, analyzes
    groups of tokens in order to recognize legal
    language                         constructs.
    www.StudentRockStars.com
   Last, a third module, the code generator,
    takes a set of legal language constructs and
    generates executable code.
        XML parser vs Compiler
• In XML processing, the document is first
  analyzed to recognize legal XML tokens such as
  start & end tags, attributes, and CDATA
  sections.
• Second, token well-formed is verified.
• Also, if a DTD is available, content is verified for
  legality in terms of DTD definitions.
• Finally, the data contained in the XML
  document is used to accomplish something
  useful. www.StudentRockStars.com
        Simple API for XML (SAX)
• SAX is one among the specifications for XML
  parsing.
• SAX was developed by the         members      of
  XML-DEV            mailing                 list.
  www.StudentRockStars.com
• Their purpose was to provide a natural means
  for working with XML that does not have the
  overheads of DOM.
• SAX is available in a number of programming
  languages, including Java, Perl, C++ & Python.
• The concepts are substantially similar in all
                        SAX…
• SAX is an API for event-driven, serial-access
  mechanism for parsing XML documents.
• SAX is the fastest, efficient and least memory-
  intensive mechanism currently available to deal
  with XML documents.
• It is designed for state-independent processing,
  where the handling of an element does not
  depend on the elements that came before.

• It is based on event model - a SAX parser sends
  events, such as start or end of an element, to an
  event handler, which processes the information.
        SAX functionality
• A SAX parser being event based, invokes
  specific methods for different events.
• SAX allows XML documents          to   be
  processed as it is being read.
• The application itself can then deal with
  the data. www.StudentRockStars.com
• The original document remains untouched,
  but SAX provides a means for manipulating
  data, which can then be directed to
  another process or document.
       SAX disadvantages
• SAX    is   an   event-driven     model
  (developer provides callback methods,
  and the parser invokes them as it
  reads the XML data).
• Cannot go back to an earlier position
  or leap ahead to a different position.
• SAX works fine for state-independent
  processing and not suitable for
  state-dependent             processing.
  www.StudentRockStars.com
      Other parsers
1.DOM
2.JAX
3.StAX
4.JDOM
5.Dom4j
  www.StudentRockStars.com
6.Sun TR2 XML Parser
7.Datachannel XJ2
8.IBM XML Parser for Java
9.OpenXML
                      DOM
• DOM is feature-rich, and seems better than SAX.
• However, DOM has serious efficiency problems that
  can hurt performance-sensitive applications.
• DOM implements an in-memory object model by
  creating many objects that represent DOM nodes.
• One of the most expensive operations in Java is the
  new operator. For every new operator used, the
  garbage collector must eventually deallocate them
  when no references to the object remain.
• DOM scans the XML document twice. The first pass
  creates the DOM structure in memory, the second
  locates all XML data the program is interested in.
             SAX Features
• SAX does not have a default object model.
• SAX based parsers simply reads the XML
  document and fires events based on the things it
  encounters in the XML document.
• Events are fired when the following         things
  happen: www.StudentRockStars.com
  Open element tags are encountered.
  Close element tags are encountered.
  #PCDATA and CDATA sections are encountered.
  Comments,       entity     declarations,      are
   encountered.
                SAX vs DOM
• DOM involves reading      • SAX     allows     XML
  the entire XML file         documents to be
  and storing it in a         processed     as    it's
  tree structure.             being read.
• Provides    a   default   • No need to wait for
  object model.               all of it to be stored
• Inefficient & slow.         before taking action.
• Heavy on resources.       • Efficient and fast
• Requires    2    passes     since     no     initial
  www.StudentRockStar         setup and storing.
  s.com                     • Requires a single
                              pass.
            SAX API

• javax.xml.parsers
• org.w3c.dom
• org.xml.sax
• org.xml.sax.ext
• org.xml.sax.helpers
           javax.xml.parsers
Provides classes allowing creation of following parsers:
   •SAX (Simple API for XML)
   •DOM (Document Object Model)
               SAXParserFactory
A factory API to configure and obtain a
SAX based parser.

•protected              SAXParserFactory()

•static      SAXParserFactory        newInstance()
•abstract    SAXParser newSAXParser()
•abstract    void       setFeature(String name, boolean val)
•abstract    boolean    getFeature(String name)
•            void       setNamespaceAware(boolean aware)
•            boolean    isNamespaceAware()
•            void       setValidating(boolean validating)
•            boolean    isValidating()
                       SAXParser
Allows parsing of XML documents by invoking
  specific methods of the given DefaultHandler.

• protected SAXParser()

•   abstract   Parser getParser()
•   abstract   Object getProperty(String name)
•   abstract   void   setProperty( String name, Object value)
•   abstract   XMLReader getXMLReader()
•   abstract   boolean isNamespaceAware()
•   abstract   boolean isValidating()
•   void               parse(File f, DefaultHandler dh)
•   void               parse( InputStream is, DefaultHandler dh)
•   void               parse(String uri, DefaultHandler dh)
• www.StudentRockStars.com
Package org.xml.sax
  Class hierarchy
                 SAX API usage
                         new DocumentHandler()          Document
Application
                                                         Handler

              new Parser()
                             Parser
         setDefaultHandler()          startDocument()

                 Parse                startElement()

                                       characters()

                                       endElement()

                                      endDocument()

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:133
posted:9/4/2010
language:English
pages:92