eXtensible Markup Language
                (XML)

Serhii Kartashov
July 2012
SoftServe
IFDC IT Academy
Agenda
Introduction to XML

   Basic rules

     Parsing XML

     XML Namespaces

     XML Schemas

   XSLT Transformation

Where is XML applying? (examples)
Agenda
Introduction to XML

   Basic rules

     Parsing XML

     XML Namespaces

     XML Schemas

   XSLT Transformation

Where is XML applying? (examples)
Origin of XML
                       SGML
                       1986


            HTML 1.0          XML 1.0
              1992             1998


  HTML                                   XHTML
4.01 1999                               1.0 2000


                                         XHTML
                                        2.0 2010
XML Data Model (a tree)
1                       root



2              title    item      item



                        name      name



                       company   company

3
                        model     model



                        cost      cost
Agenda
Introduction to XML

   Basic rules

     Parsing XML

     XML Namespaces

     XML Schemas

   XSLT Transformation

Where is XML applying? (examples)
Basic rules
•   All XML Elements Must Have a Closing
•   XML Tags are Case Sensitive
•   XML Elements Must be Properly Nested
•   XML Attribute Values Must be Quoted
•   XML Documents Must Have a Root Element
•   Entity References
•   White-space is Preserved in XML
Agenda
Introduction to XML

   Basic rules

     Parsing XML

     XML Namespaces

     XML Schemas

   XSLT Transformation

Where is XML applying? (examples)
Parsing XML
XML parser -- Reads in XML data, checks for
syntactic constraints, and makes data available
to an application. There are three 'generic'
parser APIs
  – SAX Simple API to XML (event-based)
  – DOM Document Object Model (object/tree based)
XML Parser Processing Model


                  parser    XML-based
       parser
                interface   application
DOM APIs
           DocumentBuilder
               Factory                  Packages:
                                         org.w3c.dom
                                         javax.xml.parsers
              Document       Document      (DocumentBuilderFactory,
  XML          Builder        (DOM)        DocumentBuilder) READ
                                         javax.xml.transform
                                           (TransformerFactory, Trans
                                           former, Source, Result)
                                           WRITE
             Transformer
               Factory



Document     Transformer
 (DOM)                           XML
SAX APIs
           Packages:
            org.xml.sax
            org.xml.sax.ext
            org.xml.sax.helpers
            javax.xml.parsers
              (SAXParserFactory, SAXParse
              r)
Agenda
Introduction to XML

   Basic rules

     Parsing XML

     XML Namespaces

     XML Schemas

   XSLT Transformation

Where is XML applying? (examples)
Why namespaces?
HTML:
        <table>
                   <tr>
                           <td>Apples</td>
                           <td>Bananas</td>
                   </tr>                                We have the conflict
        </table>                                       between two elements
                                                         (table). And parser
XML:                                                     doesn’t know how
        <table>                                         handle this situation.
                   <name>African Coffee Table</name>
                   <width>80</width>
                   <length>120</length>
        </table>
Namespaces handle conflicts between
         element names.
HTML:
        <h:table xmlns:h="http://www.mysite.org/html>
                <h:tr>
                        <h:td>Apples</h:td>
                        <h:td>Bananas</h:td>   We added name prefix to
                </h:tr>                       each element (table). And
        </h:table>                                 declared they.

XML:
        <x:table xmlns:x="http://www.mysite.org/xml>
                <x:name>African Coffee Table</x:name>
                <x:width>80</x:width>
                <x:length>120</x:length>
        </x:table>
Agenda
Introduction to XML

   Basic rules

     Parsing XML

     XML Namespaces

     XML Schemas

   XSLT Transformation

Where is XML applying? (examples)
Defining language dialects
• Two ways of doing so:
   – XML Document Type Declaration (DTD)– Part of core XML
     spec.
   – XML Schema – allows for stronger constraints on XML
     documents.

• Adding dialect specifications implies two classes of
  XML data:
   – Well-formed An XML document that is syntactically
     correct
   – Valid An XML document that is both well-formed and
     consistent with a specific DTD (or Schema)
DTD
<!ELEMENT root (title?, item*)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT item (name, company, model, cost)>
<!ATTRIBUTE id (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT model (#PCDATA)>
<!ELEMENT cost (#PCDATA)>
XML Schema
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name=“root">
 <xs:complexType>
  <xs:sequence>
   <xs:element name=“title" type="xs:string"/>
   <xs:element name=“item“ maxOccurs="unbounded">
    <xs:complexType>
      <xs:sequence>
       <xs:element name=“name" type="xs:string"/>
       <xs:element name=“company" type="xs:string"/>
       <xs:element name="model" type="xs:string"/>
       <xs:element name="cost" type="xs:decimal"/>
      </xs:sequence>
    </xs:complexType>
   </xs:element>
  </xs:sequence>
  <xs:attribute name=“id" type="xs:positiveInteger " use="required"/>
 </xs:complexType>
</xs:element>

</xs:schema>
XML Parser Processing Model With
       Validation Schema


                    parser    XML-based
         parser
                  interface   application



           XML
         Schema
Agenda
Introduction to XML

   Basic rules

     Parsing XML

     XML Namespaces

     XML Schemas

   XSLT Transformation

Where is XML applying? (examples)
XSLT Transformation
XSLT (EXtensible Stylesheet Language
Transformation):
• <template>
• <value-of>
• <for-each>
• <sort>
• <if>
• <choose>
XML file
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="class.xsl"?>
<class>
      <student>Jack</student>
      <student>Harry</student>
      <student>Rebecca</student>
      <teacher>Mr. Bean</teacher>
</class>
XSL file
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

            <xsl:template match="teacher">
                        <p><u><xsl:value-of select="."/></u></p>
            </xsl:template>

            <xsl:template match="student">
                        <p><b><xsl:value-of select="."/></b></p>
            </xsl:template>
            <xsl:template match="/">
                        <html>
                        <body>
                        <xsl:apply-templates/>
                        </body>
                        </html>
            </xsl:template>

</xsl:stylesheet>
XSLT Transformation
Agenda
Introduction to XML

   Basic rules

     Parsing XML

     XML Namespaces

     XML Schemas

   XSLT Transformation

Where is XML applying? (examples)
Web Design and Applications
• XML and CSS (example)
@CHARSET "UTF-8";
title{
            display: block;
            text-align: center;
            font-style: italic;
}
item:after{
            font-size: 250%;
            content: attr(id);
}
item{
            display: block;
            padding:20px;
            border-style: solid;
            font-size: 12px;
}
XML Technology
• XML Transformation
• XML Schema
• XSLT (eXtensible Stylesheet Language
  Transformations)
• MathML (Mathematical Markup Language)
MathML (example)
Web of Services
• HTTP (HyperText Transfer Protocol)
• SOAP (Simple Object Access Protocol)
• WSDL (Web Services Description Language)
Web of Devices
• Mobile Web
• VXML (Voice XML)
Browsers and Authoring Tools
•   Social networks
•   Forums
•   CMSs (Content Management System)
•   RSS (Really Simple Syndication)
Questions
and
Answers
Useful links
• Introduction to XML
   – http://www.itwriting.com/xmlintro.php
   – The development history http://www.w3.org/XML/hist2002
• Basic rules
   – http://www.w3.org/TR/2006/REC-xml11-20060816/
• XML Namespaces
   – http://www.w3.org/TR/REC-xml-names/
• Defining language dialects
   – http://www.w3.org/TR/xhtml1/dtds.html
   – http://www.w3.org/TR/xmlschema-0/
• Parsing XML
   – http://www.w3.org/DOM/DOMTR
• XSLT Transformation
   – http://www.w3.org/TR/xslt
Contacts

• http://it-academy-sk.blogspot.com/




                                       Thank you!

eXtensible Markup Language (XML)

  • 1.
    eXtensible Markup Language (XML) Serhii Kartashov July 2012 SoftServe IFDC IT Academy
  • 2.
    Agenda Introduction to XML Basic rules Parsing XML XML Namespaces XML Schemas XSLT Transformation Where is XML applying? (examples)
  • 3.
    Agenda Introduction to XML Basic rules Parsing XML XML Namespaces XML Schemas XSLT Transformation Where is XML applying? (examples)
  • 4.
    Origin of XML SGML 1986 HTML 1.0 XML 1.0 1992 1998 HTML XHTML 4.01 1999 1.0 2000 XHTML 2.0 2010
  • 5.
    XML Data Model(a tree) 1 root 2 title item item name name company company 3 model model cost cost
  • 6.
    Agenda Introduction to XML Basic rules Parsing XML XML Namespaces XML Schemas XSLT Transformation Where is XML applying? (examples)
  • 7.
    Basic rules • All XML Elements Must Have a Closing • XML Tags are Case Sensitive • XML Elements Must be Properly Nested • XML Attribute Values Must be Quoted • XML Documents Must Have a Root Element • Entity References • White-space is Preserved in XML
  • 9.
    Agenda Introduction to XML Basic rules Parsing XML XML Namespaces XML Schemas XSLT Transformation Where is XML applying? (examples)
  • 10.
    Parsing XML XML parser-- Reads in XML data, checks for syntactic constraints, and makes data available to an application. There are three 'generic' parser APIs – SAX Simple API to XML (event-based) – DOM Document Object Model (object/tree based)
  • 11.
    XML Parser ProcessingModel parser XML-based parser interface application
  • 12.
    DOM APIs DocumentBuilder Factory Packages:  org.w3c.dom  javax.xml.parsers Document Document (DocumentBuilderFactory, XML Builder (DOM) DocumentBuilder) READ  javax.xml.transform (TransformerFactory, Trans former, Source, Result) WRITE Transformer Factory Document Transformer (DOM) XML
  • 13.
    SAX APIs Packages:  org.xml.sax  org.xml.sax.ext  org.xml.sax.helpers  javax.xml.parsers (SAXParserFactory, SAXParse r)
  • 14.
    Agenda Introduction to XML Basic rules Parsing XML XML Namespaces XML Schemas XSLT Transformation Where is XML applying? (examples)
  • 15.
    Why namespaces? HTML: <table> <tr> <td>Apples</td> <td>Bananas</td> </tr> We have the conflict </table> between two elements (table). And parser XML: doesn’t know how <table> handle this situation. <name>African Coffee Table</name> <width>80</width> <length>120</length> </table>
  • 16.
    Namespaces handle conflictsbetween element names. HTML: <h:table xmlns:h="http://www.mysite.org/html> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> We added name prefix to </h:tr> each element (table). And </h:table> declared they. XML: <x:table xmlns:x="http://www.mysite.org/xml> <x:name>African Coffee Table</x:name> <x:width>80</x:width> <x:length>120</x:length> </x:table>
  • 17.
    Agenda Introduction to XML Basic rules Parsing XML XML Namespaces XML Schemas XSLT Transformation Where is XML applying? (examples)
  • 18.
    Defining language dialects •Two ways of doing so: – XML Document Type Declaration (DTD)– Part of core XML spec. – XML Schema – allows for stronger constraints on XML documents. • Adding dialect specifications implies two classes of XML data: – Well-formed An XML document that is syntactically correct – Valid An XML document that is both well-formed and consistent with a specific DTD (or Schema)
  • 19.
    DTD <!ELEMENT root (title?,item*)> <!ELEMENT title (#PCDATA)> <!ELEMENT item (name, company, model, cost)> <!ATTRIBUTE id (#PCDATA)> <!ELEMENT name (#PCDATA)> <!ELEMENT company (#PCDATA)> <!ELEMENT model (#PCDATA)> <!ELEMENT cost (#PCDATA)>
  • 20.
    XML Schema <?xml version="1.0"encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name=“root"> <xs:complexType> <xs:sequence> <xs:element name=“title" type="xs:string"/> <xs:element name=“item“ maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name=“name" type="xs:string"/> <xs:element name=“company" type="xs:string"/> <xs:element name="model" type="xs:string"/> <xs:element name="cost" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name=“id" type="xs:positiveInteger " use="required"/> </xs:complexType> </xs:element> </xs:schema>
  • 21.
    XML Parser ProcessingModel With Validation Schema parser XML-based parser interface application XML Schema
  • 22.
    Agenda Introduction to XML Basic rules Parsing XML XML Namespaces XML Schemas XSLT Transformation Where is XML applying? (examples)
  • 23.
    XSLT Transformation XSLT (EXtensibleStylesheet Language Transformation): • <template> • <value-of> • <for-each> • <sort> • <if> • <choose>
  • 24.
    XML file <?xml version="1.0"encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="class.xsl"?> <class> <student>Jack</student> <student>Harry</student> <student>Rebecca</student> <teacher>Mr. Bean</teacher> </class>
  • 25.
    XSL file <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="teacher"> <p><u><xsl:value-of select="."/></u></p> </xsl:template> <xsl:template match="student"> <p><b><xsl:value-of select="."/></b></p> </xsl:template> <xsl:template match="/"> <html> <body> <xsl:apply-templates/> </body> </html> </xsl:template> </xsl:stylesheet>
  • 26.
  • 27.
    Agenda Introduction to XML Basic rules Parsing XML XML Namespaces XML Schemas XSLT Transformation Where is XML applying? (examples)
  • 28.
    Web Design andApplications • XML and CSS (example) @CHARSET "UTF-8"; title{ display: block; text-align: center; font-style: italic; } item:after{ font-size: 250%; content: attr(id); } item{ display: block; padding:20px; border-style: solid; font-size: 12px; }
  • 29.
    XML Technology • XMLTransformation • XML Schema • XSLT (eXtensible Stylesheet Language Transformations) • MathML (Mathematical Markup Language)
  • 30.
  • 31.
    Web of Services •HTTP (HyperText Transfer Protocol) • SOAP (Simple Object Access Protocol) • WSDL (Web Services Description Language)
  • 32.
    Web of Devices •Mobile Web • VXML (Voice XML)
  • 33.
    Browsers and AuthoringTools • Social networks • Forums • CMSs (Content Management System) • RSS (Really Simple Syndication)
  • 34.
  • 35.
    Useful links • Introductionto XML – http://www.itwriting.com/xmlintro.php – The development history http://www.w3.org/XML/hist2002 • Basic rules – http://www.w3.org/TR/2006/REC-xml11-20060816/ • XML Namespaces – http://www.w3.org/TR/REC-xml-names/ • Defining language dialects – http://www.w3.org/TR/xhtml1/dtds.html – http://www.w3.org/TR/xmlschema-0/ • Parsing XML – http://www.w3.org/DOM/DOMTR • XSLT Transformation – http://www.w3.org/TR/xslt
  • 36.