xml for e-commerce iii helena ahonen-myka. in this part... n transforming xml n traversing xml n web...

41
XML for E-commerce III Helena Ahonen-Myka

Upload: briana-hill

Post on 11-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

XML for E-commerce III

Helena Ahonen-Myka

Page 2: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

In this part...

Transforming XML Traversing XML Web publishing frameworks

Page 3: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Transforming XML

Page 4: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Extensible Stylesheet Language

a language for transforming XML documents

an XML vocabulary for specifying the formatting of XML documents

Page 5: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

XSLT

specifies the conversion of a document from one format to another

XSLT transformation (stylesheet) is a valid XML document

based on hierarchical tree structure mechanism for matching patterns within

the original XML document and applying formatting to that data

Page 6: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

XML Path Language (XPath)

a mechanism for referring to the wide variety of element and attribute names and values in an XML document

also non-validated documents have to be able to be transformed: DTD cannot be used to outline the structure

tree-based: specify the path to the element

Page 7: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

XPath

specify the part relative to the current element being processed

…or relative to the root: reference an element that is not in the current element’s scope

…or using pattern matching: find an element whose parent is element E and which has a sibling element F

Page 8: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

XPath: examples

Match the element named Book relative to the current element:

<xsl:value-of select:”Book” />

Page 9: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

XPath: Examples

Match the element named Contents nested within the Book element

<xsl:value-of select=”Book/Contents” />

Match the Contents element using an absolute path:

<xsl:value-of select=”/Book/Contents” />

Page 10: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

XPath: examples

Match the focus attribute of the current element:

<xsl:value-of select=”@focus” />

Match the focus attribute of the Chapter element:

<xsl:value-of select=”Chapter/@focus” />

Page 11: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

XPath: examples Match any Para element with an Appendix

ancestor element: … select=”Appendix//Para”

id(”W11”) matches the element with unique ID ´W11´

Para[1] matches any Para element that is the first Para child element of its parent

Page 12: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

XPath: examples

Para[last()=1] matches any Para element that is the only Para child element of its parent

Items/Item[position()>1] matches any Item element that has an Items parent and that is not the first Item child of its parent

Page 13: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

XPath

Because often the input document is not fixed, an Xpath expression can result in the evaluation of no input data, one input element or attribute, or multiple input elements or attributes

the result of evaluating an Xpath expression is referred to as a node set.

Page 14: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

XSL stylesheet is an XML document must be well-formed must contain an XML declaration must declare all the namespaces it uses the XSL namespace (prefix xsl:) defines

elements that are needed for performing transformations

Page 15: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Skeleton XSL stylesheet

<?xml version=”1.0” ?>

<xsl:stylesheet

xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”

xmlns:JavaXML=”http://www.oreilly.com/catalog/javaxml/”

version=”1.0”>

</xsl:stylesheet>

Page 16: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

XSL Template

locates a particular element or set of elements within the input XML document and applies a rule or set of rules to the resulting XML

<xsl:template match=”[XPath expression]”>

<!-- here are the rules and formatting -->

</xsl:template>

Page 17: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Printing all the data:

<?xml version=”1.0” ?>

<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”xmlns:JavaXML= ”http://www.oreilly.com/catalog/javaxml/”version=”1.0”>

<xsl:template match=”JavaXML:Book”> <xsl:apply-templates /></xsl:template>

</xsl:stylesheet>

Page 18: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Notes: JavaXML:Book is the root element xsl:apply-templates tells the processor

to match the children of the current element and apply their templates

each element has a default template, which contains xsl:apply-templates and printing the data content

result (prev slide): all the data of the document is printed (unformatted)

Page 19: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Generating HTML

<xsl:template match=”JavaXML:Book”> <html> <head> <title>Here is my HTML page!</title> </head> <body> <xsl:apply-templates /> </body> </html></xsl:template>

Page 20: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

HTML Output

<html><head> <title>Here is my HTML page!</title></head> <body> Java and XML

Introduction What Is It? How Do I Use It? ...

</body></html>

Page 21: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

xsl:value-of element<xsl:template match=”JavaXML:Book”> <html> <head> <title><xsl:value-of select:”JavaXML:Title” /> </title> </head> <body> <xsl:apply-templates /> </body> </html></xsl:template>

Produces: ...<head><title>Java and XML</title></head>...

Page 22: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Looping and iteration

xsl:for-each

<xsl:template match=”JavaXML:Contents”> <center> <h2>Table of Contents</h2> </center> <hr /> <ul> <xsl:for-each select=”JavaXML:Chapter”> <li><xsl:value-of select=”JavaXML:Heading” /></li> </xsl:for-each> </ul></xsl:template>

Page 23: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Conditional processing: if

xsl:if : nodes that conform to both an XPath expression and some user-defined criteria

only chapters with focus=Java:

<xsl:for-each select=”JavaXML:Chapter”> <xsl:if test=”@focus=’Java’”> <li><xsl:value-of selecct=”JavaXML:Heading” /></li> </xsl:if></xsl:for-each>

Page 24: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Conditional processing: choose

<xsl:for-each select=”JavaXML:Chapter”> <xsl:choose> <xsl:when test=”@focus=’Java’”> <li><xsl:value-of select=”JavaXML:Heading” /> (Java Focus) </li> </xsl:when> <xsl:otherwise> <li><xsl:value-of select=”JavaXML:Heading” /> (XML Focus)</li> </xsl:otherwise> </xsl:choose><xsl:for-each>

Page 25: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Adding elements and attributes

xsl:element, xsl:attribute

<xsl:element name=”myElement”> <xsl:attribute name=”myAttribute”> XML </xsl:attribute> is great!</xsl:element>

Produces: <myElement myAttribute=”XML”>is great!</myElement>

Page 26: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Numbering

<xsl:template match=”items”> <xsl:for-each select=”item”> <xsl:sort select=”.” /> <p> <xsl:number value=”position()” format=”1. ” /> <xsl:value-of select=”.” /> </p> <xsl:for-each></xsl:template>

Page 27: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Sorting

<employees> <employee> <name> <given>James</given> <family>Clark</family> </name> … </employee></employees>

Page 28: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Sorting<xsl:template match=”employees”> <ul> <xsl:apply-templates select=”employee”> <xsl:sort select=”name/family” /> <xsl:sort select=”name/given” /> </xsl:apply-templates> </ul></xsl:template>

<xsl:template match=”employee”> <li> <xsl:value-of select=”name/given” /> <xsl:text> </xsl:text> <xsl:value-of select=”name/family” /> </li></xsl:template>

Page 29: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Copying parts without transforming sometimes a part should be passed as

such, without any transformation assume: JavaXML:Copyright contains

some HTML formatting:

<xsl:template match=”JavaXML:Copyright”> <xsl:copy-of select=”*” /></xsl:template>

Page 30: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Formatting objects (e.g.for PDF)

<xsl:template match=”JavaXML.Title”> <fo:block font-size=”24pt” text-align-last=”centered” space-before.optimum=”24pt”> <xsl:apply-templates> </fo:block></xsl:template>

Produces:

<fo:block font-size=”24pt” text-align=”centered” space-before.optimum=”24pt”> Java and XML </fo:block>

Page 31: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Traversing XML

In transforming documents, random access to an document is needed

SAX cannot look backward or forward difficult to locate siblings and children DOM: access to any part of the tree

Page 32: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

DOM

Level 1: navigation of content within a document

Level 2: modules and options for specific content models, such as XML, HTML, and CSS

Level 3

Page 33: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

DOM Java bindings

Interfaces and classes that define and implement the DOM

http://www.w3.org/TR/DOM-Level-2/ java-binding.html

bindings often included in the parser implementations (the parser generates a DOM tree)

Page 34: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Parsing using a DOM parser

import org.apache.xerces.parsers.DOMParser;

DOMParser parser = new DOMParser();

parser.parse(uri);

Page 35: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Output is important

the entire document is parsed and added into the output tree, before any processing takes place

handle: org.w3c.dom.Document object = one level above the root element in the document

parser.parse(uri);Document doc = parser.getDocument();

Page 36: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Printing a document

Private static void printTree(Node node) { switch (node.getNodeType()) { case Node.DOCUMENT_NODE: // Print the contents of the Document object break;

case Node.ELEMENT_NODE: // Print the element and its attributes break;

case Node.TEXT_NODE: ...

Page 37: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

…the Document node

Case Node.DOCUMENT_NODE: System.out.println(”<xml version=\”1.0\”>\n”); Document doc = (Document)node; printTree(doc.getDocumentElement()); break;

Page 38: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

… elementsCase Node.ELEMENT_NODE: String name= node.getNodeName(); System.out.print(”<” + name); // Print out attributes… (see next slide…) System.out.println(”>”);

// recurse on each child NodeList children = node.getChildNodes(); if (children != null) { for (int i=0; i<children.getLength(); i++) { printTree(children.item(i)); } } System.out.println(”</” + name + ”>”);

Page 39: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

… and their attributes

case Node.ELEMENT_NODE: String name = node.getNodeName(); System.out.print(”<” + name); NamedNodeMap attributes = node.getAttributes(); for (int i=0; i<attributes.getLength(); i++) { Node current = attributes.item(i); System.out.print(” ” + current.getNodeName() + ”=\”” + current.getNodeValue() + ”\””); } System.out.println(”>”); ...

Page 40: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

…textual nodes

case Node.TEXT_NODE:case Node.CDATA_SECTION_NODE: System.out.print(node.getNodeValue()); break;

Page 41: XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks

Web publishing frameworks

See: http://www.oreilly.com/catalog/javaxml/ chapter/ch09.html