COMP249: XML

Steve Cassidy and Rolf Schwitter

XML

XML Structure

    <?xml version = "1.0"?>

    <wines>
      <wine grape = "chardonnay">
        <product> Carneros </product>  
        <year> 2002 </year>
        <price> 12.00 </price>
      </wine> 
    
      <wine grape = "merlot">
        ...
      </wine>
    </wines>
    

XML Editor

Today

Storing Data in XML

XML Parsers

XML Parsers: SAX

XML Parsers: SAX

How SAX works

SAX Example

Find a CD with a given title attribute in the document cd.xml:

  <?xml version='1.0'?>

  <collection>
    <cd artist="Canto Loco"
        title="Los Locos Computadores">
      <track title="Los Locos Computadores" 
             duration="3m22s"/>
      <track title="Los Locos Hombres" 
             duration="2m15s"/>
    </cd>
    <cd artist="Canto Loco"
	title="Los Todos Locos">
      <track title="Los Todos Locos" 
	     duration="3m22s"/>
    </cd>
  </collection>
      

SAX Example

import sys
from xml.sax import make_parser, handler

Define a content handler class:

class FindTitle(handler.ContentHandler):
    def __init__(self, title):
        self.search_title = title

    def startElement(self, name, attrs):
        """start element handler for finding titles"""

        # only do work for cd elements
        if name != 'cd': return

        # check the title attribute
        else: title = attrs.get('title', None)
        if (title == self.search_title):
            print title, 'found'

SAX Example

Main program:

if __name__ == '__main__':
    # Create a parser
    parser = make_parser()

    # Create the handler
    dh = FindTitle('Los Locos Computadores')

    # Tell the parser to use our handler
    parser.setContentHandler(dh)

    # Parse the command line input: saxparser.py cd.xml
    # parser.parse(sys.argv[1])

    # parse the XML document
    parser.parse('cd.xml')

XML Parsers: DOM

An Example DOM Program

from xml.dom import minidom

xmldoc = minidom.parse('cd.xml')

for node in xmldoc.getElementsByTagName('cd'):
    attrobject = node.attributes['title']
    if (attrobject.value == 'Los Locos Computadores'):
        print attrobject.value

Writing XML

XPath

XPath Examples

XPath Examples

  • Select all the artist elements that have an attribute named type with a value of 'group':
    //artist[@type = 'group']
  • Select all the title elements of the cd elements of the collection element that have a price element with a value smaller than 20.00:
    /collection/cd[price<20.00]/title
  • XML in Publishing

    Displaying XML Documents

    CSS and XML

    XML CSS Example

    <?xml version="1.0" ?>
    <?xml-stylesheet type="text/css" href="sample6.css"?>
    
    <THING> This is a thing with an inline stack:
    <STACK>
      <ROW>This is the <D>top</D> row.</ROW>
      <ROW>This is the <D>middle</D> row.</ROW>
      <ROW>This is the <D>bottom</D> row.</ROW>
    </STACK>
    which was displayed just there.
    </THING>
          

    View with Empty stylesheet, the stylesheet below

    THING { display: block; border: 1px solid red;}
    STACK { display: inline-table; }
    ROW   { display: table-row; background: blue; color: white; }
    D     { display: inline; font-weight: bolder; color: red; }
    

    CSS Limitations

    Transformation for Web Publication

    XSL = XML Style Sheet Language

    XSLT = XSL Transformations

    Transform to What?

    XML to XML via XSLT: Input

     
        <wines>
          <wine grape = "chardonnay">
            <product> Carneros </product>  
            <year> 2002 </year>
            <price> 12.00 </price>
          </wine>
        </wines>

    This example shows transformation of one XML dialect to another using an XSLT stylesheet.

    XML to XML via XSLT: Target

     
        <wines>
          <wine grape = "chardonnay">
            <product> Carneros </product>  
            <vintage> 2002 </vintage>
          </wine>
        </wines>

    XML to XML via XSLT: Stylesheet

    <xsl:stylesheet 
       xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
       version = "1.0">
    
    <xsl:template match = "year">
      <vintage>
        <xsl:apply-templates/>
      </vintage>
    </xsl:template>
    
    <xsl:template match = "price"></xsl:template>
    
    <!-- Copy all the other elements and attributes, and text nodes -->
    
    <xsl:template match = "*|@*|text()">
      <xsl:copy>
        <xsl:apply-templates select = "*|@*|text()"/>
      </xsl:copy>
    </xsl:template>
    </xsl:stylesheet>

    XML to XHTML via XSLT: Input

        <?xml version = "1.0"?>
    
        <document>
          <title> COMP249: Web Technology </title>  
          <para> This is the main page of COMP249 where all information ... </para>
        </document> 
    Here is an example of a transformation applied to a simple XML file to generate XHTML output:

    XML to XHTML via XSLT: Target

        <html>
          <head>
            <meta http-equiv = "Content-Type" content = "text/html; charset = utf-8">
            <title> COMP249: Web Technology </title>
          </head>
          <body>
            <h1> COMP249: Web Technology </h1>   
            <p> This is the main page of COMP249 
              where all information ... </p>
          </body>
        </html>

    XML to XHTML via XSLT: Stylesheet

     
    <xsl:transform
           xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0">
    
         <xsl:output method = "html"/>
    
         <xsl:template match = "document">
           <html><head>
            <title>
             <xsl:value-of select = "./title"/>
            </title>
           </head>
           <body>
             <xsl:apply-templates/>
           </body>
           </html>
         </xsl:template>
    
         
         

    XML to XHTML via XSLT: Stylesheet

         
         <xsl:template match = "title">
           <h1><xsl:apply-templates/></h1>
         </xsl:template>
    
         <xsl:template match = "para">
           <p><xsl:apply-templates/></p>
         </xsl:template>
    
    </xsl:transform>

    Style Sheet Declaration

    Linking the Style Sheet to the XML Document

    XSLT and Alternatives

    Other ways exist for transforming XML documents:

    Summary