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

Post on 11-Jan-2016

220 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

XML for E-commerce III

Helena Ahonen-Myka

In this part...

Transforming XML Traversing XML Web publishing frameworks

Transforming XML

Extensible Stylesheet Language

a language for transforming XML documents

an XML vocabulary for specifying the formatting of XML documents

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

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

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

XPath: examples

Match the element named Book relative to the current element:

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

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” />

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” />

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

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

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.

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

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>

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>

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>

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)

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>

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>

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>...

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>

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>

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>

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>

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>

Sorting

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

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>

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>

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>

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

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

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)

Parsing using a DOM parser

import org.apache.xerces.parsers.DOMParser;

DOMParser parser = new DOMParser();

parser.parse(uri);

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();

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: ...

…the Document node

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

… 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 + ”>”);

… 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(”>”); ...

…textual nodes

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

Web publishing frameworks

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

top related