xpath, xslt, fo xml techniques for e-commerce, budapest 2004€¦ · michael sonntag xml techniques...

54
© Michael Sonntag 2004 Presentation XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004 Mag. iur. Dr. techn. Michael Sonntag Institute for Information Processing and Microprocessor Technology (FIM) Johannes Kepler University Linz, Austria E-Mail: [email protected] http://www.fim.uni-linz.ac.at/staff/sonntag.htm

Upload: others

Post on 04-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

© Michael Sonntag 2004

PresentationXPath, XSLT, FO

XML Techniques for E-Commerce, Budapest 2004

Mag. iur. Dr. techn. Michael Sonntag

Institute for Information Processing andMicroprocessor Technology (FIM)

Johannes Kepler University Linz, AustriaE-Mail: [email protected]://www.fim.uni-linz.ac.at/staff/sonntag.htm

Page 2: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

© Michael Sonntag 2004

QuestionsQuestions??Please ask immediately!

? ?

??

??

Page 3: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 3XML Techniques for E-Commerce: Presentation

Content

The problems of HTML/XML and Reasons for XSLXSL OverviewXPath: Referencing parts of XML documents

PathesAxesOther constructs

XSLT: Transforming XML to XML, XHTML, HTML, ...Functional conceptsCreating content

FO: Specifying formatting semanticsBrief overview

Page 4: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 4XML Techniques for E-Commerce: Presentation

Problems of HTML/XML and Reasons for XSL

HTML is widely used for presentation, but mixes structure with presentation; no datatypes; etc.XML defines structure nicely, but which user is ANYHOWinterested in PURE XML?

Readability for non-expertsVisual presentation

Two versions for presentation of XML possible:Parser: Applications use it as input and present it in their own way (graphics, UI, text, ...) to the user

» Parsers exist for most programming languages and file formats» Not handled here!

Direct conversion to HTML: This is were XSL comes in!XSL converts XML back to HTML

Or something other, e. g. again to XML or any other format

Page 5: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 5XML Techniques for E-Commerce: Presentation

XSL Overview(1)

eXtensible Stylesheet Language Consists of three parts:

XSLT: XSL Transformation (manipulating data)» Version 2.0 currently in development!

XPath: Expression language to refer to parts of XML documents» Version 2.0 currently in development!

FO: XSL Formatting Objects (vocabulary for formatting semantics)XSL itself:

A language for „manipulating“ XML» In several ways, with different output formats

Written in XML themselfTakes XML and XSL as input and produces some output

Usually (X)HTML, but can also be SVG, XML, PDF, plain text, ...

Page 6: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 6XML Techniques for E-Commerce: Presentation

XSL Overview(2)

Uses XPath for refering to and extracting XML input dataWhich is itself very powerful and rather complex

» Separate specification„Navigation“ possible across several different axes/dimensions

» Parent/children, descendants, siblings, attributes, ...XSLT transforms one document tree to another (or somethingdifferent) using template rules or programmatically

Template rule: Matches a pattern to nodes in the source tree and constructs parts of a new tree according to production rules

» E. g.: For all <para> elements: Change them to "<p>" (resp. "</p>"!)Programmatically: Goes through the template sequentially and inserts the selected parts from the source tree

» E. g.: "The project name is <b>: " [insert attribute projectName of the first <project> element here] "</b>."

Page 7: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 7XML Techniques for E-Commerce: Presentation

HTML

XMLDocument 1:<paragraph>

...

...

XMLDocument 2:

<para>

PDF

HTML:<p>

XML:<para>

XML:<fo:block>

Text:\n

XPathXSLT

XPathXSL-FO

XSL Overview(3)

Page 8: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 8XML Techniques for E-Commerce: Presentation

Some XML technologies

XML

HTML

XML

FOXML

Schema

XMLName-space

XSLT

ebXML, SOAP,

SecurityMetadata,

...

XPath

Java

Page 9: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 9XML Techniques for E-Commerce: Presentation

XPath 1.0

For addressing parts of XML documentsFrom individual elements to whole trees, but also attributes

Manipulation of strings, numbers and booleansXPath is NOT written in XML itself!

Usually used as attribute value ⇒ therefore elements not possibleIf "<" is contained, it must be quoted (&lt;)!

XPath ≡ ExpressionTherefore attribute content

XPath works on "nodes": elements, attributes, text, etc.Evaluation of an expression yields one of

Set of nodes (unordered, no duplicates)Boolean (true, false)Number (integer, floating point)String (plain text)

Page 10: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 10XML Techniques for E-Commerce: Presentation

XPathLocation paths

A location path describes how to get to a position in an XML treeExample: "child::chapter/descendant::para"Selects the "para" element descendants of the "chapter" element children of the current (context) node

A path consists of one or several stepsSteps are separated by slashes ("/")Absolute paths: Start with "/", meaning the document root

» Example: "/" = the document elementRelative paths: No starting slash, begins evaluation at the current position (default: document element)

» Example: child::* = All children of the current element

Page 11: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 11XML Techniques for E-Commerce: Presentation

XPathLocation steps

Each step in a path consists of three partsAn axis: In which "direction" we should go

» Children, parent, …A node test: What should be selected from those nodes possible in the direction determined by the axis

» <para> elements, <doc> elements, …Optionally some predicates: Arbitrary expressions for further refinement (only those nodes matching certain requirements)

» First element whose attribute "color" has the value "red", …Syntax: axis-name "::" node-test { "[" predicates "]" }Example: child::para[position()=1]

Axis: From all children of the current elementNode test: Select all those which are <para> elementsPredicate: Take the first one of these

» Result: The first "para" child of the current node

Page 12: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 12XML Techniques for E-Commerce: Presentation

XPathAxes (1)

child: Children elements of the context (=current) nodedescendant: All child elements and recursively their child elements

Only elements, never attribute or namespace nodes!parent: The parent (only one possible; tree!)

If it exists: The document element has noneancestor: All elements up to and including the document elementfollowing-sibling: All siblings of the current node after itself

Currently at attribute or namespace node: Axis is emptypreceding-sibling: All siblings of the current node before itself

Currently at attribute or namespace node: Axis is emptyfollowing: All nodes textually after (source) the current node

Excluding all descendants of the current elementExcluding attribute and namespace nodes

Page 13: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 13XML Techniques for E-Commerce: Presentation

XPathAxes (2)

preceding: All nodes textually before the current nodeExcluding all ancestors of the current elementExcluding attribute and namespace nodes

attribute: All attributes of the current nodeEmpty if the current node is not an elementShort form: "@" attribute name (e. g. "@href")

namespace: All namespace nodes of the current nodeEmpty if the current node is not an element

self: The current node itselfdescendant-or-self: Current node and all descendants

Aggregate path: Combines "descendant" and "self"ancestor-or-self: Current node and all ancestors

Aggregate path: Combines "ancestor" and "self"

Page 14: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 14XML Techniques for E-Commerce: Presentation

XPathChild axis

A

AA AB AC

ABA ABB ABC ABD ABE

ABBA ABBB ABDA ABEAABCA ABCB

ABCAA ABCABCurrent node

Selected nodes

Page 15: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 15XML Techniques for E-Commerce: Presentation

XPathDescendant axis

A

AA AB AC

ABA ABB ABC ABD ABE

ABBA ABBB ABDA ABEAABCA ABCB

ABCAA ABCABCurrent node

Selected nodes

Page 16: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 16XML Techniques for E-Commerce: Presentation

XPathParent axis

A

AA AB AC

ABA ABB ABC ABD ABE

ABBA ABBB ABDA ABEAABCA ABCB

ABCAA ABCABCurrent node

Selected nodes

Page 17: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 17XML Techniques for E-Commerce: Presentation

XPathAncestor axis

A

AA AB AC

ABA ABB ABC ABD ABE

ABBA ABBB ABDA ABEAABCA ABCB

ABCAA ABCABCurrent node

Selected nodes

Page 18: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 18XML Techniques for E-Commerce: Presentation

XPathFollowing-sibling axis

A

AA AB AC

ABA ABB ABC ABD ABE

ABBA ABBB ABDA ABEAABCA ABCB

ABCAA ABCABCurrent node

Selected nodes

Page 19: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 19XML Techniques for E-Commerce: Presentation

XPathPreceding-sibling axis

A

AA AB AC

ABA ABB ABC ABD ABE

ABBA ABBB ABDA ABEAABCA ABCB

ABCAA ABCABCurrent node

Selected nodes

Page 20: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 20XML Techniques for E-Commerce: Presentation

XPathFollowing axis

A

AA AB AC

ABA ABB ABC ABD ABE

ABBA ABBB ABDA ABEAABCA ABCB

ABCAA ABCABCurrent node

Selected nodes

Page 21: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 21XML Techniques for E-Commerce: Presentation

XPathPreceding axis

A

AA AB AC

ABA ABB ABC ABD ABE

ABBA ABBB ABDA ABEAABCA ABCB

ABCAA ABCABCurrent node

Selected nodes

Page 22: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 22XML Techniques for E-Commerce: Presentation

XPathSelf axis

A

AA AB AC

ABA ABB ABC ABD ABE

ABBA ABBB ABDA ABEAABCA ABCB

ABCAA ABCABCurrent node

Selected nodes

Page 23: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 23XML Techniques for E-Commerce: Presentation

XPathDescendant-or-self axis

A

AA AB AC

ABA ABB ABC ABD ABE

ABBA ABBB ABDA ABEAABCA ABCB

ABCAA ABCABCurrent node

Selected nodes

Page 24: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 24XML Techniques for E-Commerce: Presentation

XPathAncestor-or-self axis

A

AA AB AC

ABA ABB ABC ABD ABE

ABBA ABBB ABDA ABEAABCA ABCB

ABCAA ABCABCurrent node

Selected nodes

Page 25: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 25XML Techniques for E-Commerce: Presentation

XPathCombined axis

A

AA AB AC

ABA ABB ABC ABD ABE

ABBA ABBB ABDA ABEAABCA ABCB

ABCAA ABCABAncestorDescendantFollowingPreceding Self

Current node

Selected nodesThese five axis together always

represent the complete document!

Page 26: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 26XML Techniques for E-Commerce: Presentation

XPathNode tests and predicates

Node tests: select nodes from the axisQName (Qualified name): All elements with exactly this name

» Example: "::para" = All <para> elements*: All nodes

» Example: "attribute::*" = All attributes of the context nodetext(): All text nodes (textual content)

» Example: "self::text()" = All directly contained text (not from within childs)!

Predicate: Evaluated for each node in the result setIf it returns "false", the node is removed from the result setPredicates are expressions: Can contain again location paths

» Example: child::para[attribute::type="warning"]» Operators: Logic (or;and;=,!=;<=,<,>=,>), numeric (+,-,*,div,mod)

Several functions are predefined: Not described here!» Functions arguments can again be complete XPath expressions!

Page 27: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 27XML Techniques for E-Commerce: Presentation

XPathAbbreviations

Omitted parts are replaced by context (child is default axis)"@name" = Attribute "name" of the context node"text()" = Text of the context node

Numbers alone specify the position: para[1] = para[position()=1]"//" Any number of intervening elements (0…N)

Example: "//b" = All <b> elements within the whole document"." is the current, ".." the parent node.

Example: "../title" = Selects all title children of our parentExample: "../@lang" = Selects lang attribute of parent element

Examples:para[@type="warning"][5]

» Selects fifth para child of type warningpara[5][@type="warning"]

» Selects the fifth para child if it is of type warning (empty otherwise!)

Page 28: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 28XML Techniques for E-Commerce: Presentation

XPathExamples

article[author="Jones" or author="Scott"]All article childs where their author childs contain the text "Jones" or "Scott"<book><article><author>Jones</author></article>

<article><author>Scott</author></article></book>article[@author="Jones" or @author="Scott"]

All article childs where the author attribute is one of the values<book><article author="Jones"/><article author="Scott"/></book>

article|book: All article childs and all book childs<literature><book/><article/></literature>

count(students[@mark="SGT1"])Number of students where the attribute "mark" has the value "SGT1" (E.g. "15")

Page 29: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 29XML Techniques for E-Commerce: Presentation

XPathSome remarks

Powerful language, but can get complicatedTry to keep expressions short!

Several functions, but no complete "programming language"Not everything can be expressed

Try not to mix too many axis!

Successor: XPath 2.0 / XQueryEven more powerfulSpecification in developmentAlmost a programming language

» My opinion: Too complicated ⇒ Use a program instead!» Some elements however very useful: datatypes

Page 30: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 30XML Techniques for E-Commerce: Presentation

What's new in Xpath 2.0

XPath 2.0: Next version; not yet finalized (last draft: 12.11.2003)"Merger" with XQuery: Significant similar parts

» XPath is a subset of XqueryMostly backwards compatible

Supports full schema datatypes!"Node-set" replaced by a more general "sequence"Iterator for sequences ("for"), conditional expression ("if")More operators

Quantifiers: Existential and All» some $var in $sequence satisfies $var=$val» every $var in $sequence satisfies $var=$val

Sets: Union, intersection, differenceMore functionsComments: "(:" comment text; may be nested ":)"

Page 31: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 31XML Techniques for E-Commerce: Presentation

Some XML technologies

XML

HTML

XML

FOXML

Schema

XMLName-space

XSLT

ebXML, SOAP,

SecurityMetadata,

...

XPath

Java

Page 32: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 32XML Techniques for E-Commerce: Presentation

XSLT:Document structure

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/xhtml1/strict">

Synonym for "stylesheet": "transform" (exactly the same meaning)Example defines the standard namespace to be XHTML, so anything not prefixed will be XHTML (e. g. in the output parts)Version is obligatory, everything else can be omitted

No character text allowed within (only whitespaces)!Except when creating text (⇒ only within certain elements)!

Contains:Declarations: Including other files, parameters, formatting, ...User-defined data elements: The actual transformation rules

A simplified version exists (just avoids some typing)

Page 33: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 33XML Techniques for E-Commerce: Presentation

XSLT:Expressions

Expressions are XPath expressionsThree uses:

Selecting nodes for processing» On what should be acted» See templates below!

Specifying conditions for different ways of processing a node» How to act» E. g. conditional output

Generating text to be inserted in the result tree» What to produce» Producing plain text, position numbers, etc.

Page 34: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 34XML Techniques for E-Commerce: Presentation

XSLT:Serialization

Specifies how the output should be created<xsl:output/>

Method: xml, html, xhtml, text, <user-defined>» html for example will not write an end tag for empty elements

Encoding: Preferred character encodingIndent: yes, no (additional whitespaces allowed in output or not?)Media-type, doctype-system, doctype-public, omit-xml-declaration, standalone, Cdata section element

Example:<xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>

» Output will be an indented XML document of version 1.0 with certain character encoding (Latin 1 – Western Europe)

OutputMethod.xml, OutputMethod.xsl, OutputMethod_res.xml

Page 35: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 35XML Techniques for E-Commerce: Presentation

Two ways to go …

In XSL there are two main ways of creating output:Imperative programming and functional programming

Imperative programming:Stylesheet-drivenOutputting some text, decisions (if, choose), …Selecting content from arbitrary positions for handling (→ XPath)Document structure exactly known

Functional programming:Input-driven“All elements of a certain name are handled in this way”Applying templates to elements (expressions)

» Preferred method for most uses; more difficult to learn for programmersDocument structure generally similar, but not exactly the same

Both can be used and even mixed: Careful design needed!

Page 36: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 36XML Techniques for E-Commerce: Presentation

XSLT:Content construction

Within templates, content for the output is constructedThis can be either elements, attributes or general textElements can be created very easily:

Everything not in the XSLT namespace is copied to the result tree!» This applies to elements as well as attributes!» Only if it is not an extension element of XSLT

Namespace aliasing possible (changing element's namespace)» E. g. when creating a result which is in the XSLT namespace

Elements (and their attributes) can also be created explicitlyImportant e. g. for creating attributes with a computed value

» See below!

Page 37: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 37XML Techniques for E-Commerce: Presentation

XSLT:Selecting templates

<xsl:template match="pattern">content constructor</xsl:template>Templates are applied

Best matching ones firstRecursively until no more templates matchProcessing context is the current node

Patterns are used for deciding whether templates matchAre a subset of XPath expressions and result is always a node-setDifference: Only child and attribute axis and "//" and "/" are allowed!

» But predicates within can contain ANY expression!A template matches a certain pattern; if invoked, it checks thispattern and produces the specified result if they matchTemplates are not invoked/checked automatically!

Must be done manually where desired (=applying them)

Page 38: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 38XML Techniques for E-Commerce: Presentation

XSLT:Applying templates

Templates are not applied/invoked automatically<xsl:apply-templates select=„expression“/>Applies templates to all nodes matching the selection criteria

» Selection: Expression, not a pattern (⇒ full power of XPath available)!If this rule is missing, the template continues

After its end other templates are checkedA template can contain any numer of apply-template rules

E. g. reordering: First all "a" childs, then all "b" child elements, ...May select children, but also arbitrary other nodes

If going up, endless loops might happen! Careful design needed!» Example: <xsl:template match="foo"><xsl:apply-templates select="."/></xsl:template>

Default templates (if nothing matches):Recursively applies templates to child elementsPrints text content and attribute values

Page 39: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 39XML Techniques for E-Commerce: Presentation

XSLT:Template Examples (1)

Example (Stylesheet):<xsl:template match="paragraph"><p><xsl:apply-templates select="line"/></p>

</xsl:template>Example (Input):

<paragraph><line>Line 1</line><comment>First line</comment><line>Line 2</line></paragraph><paragraph><line>Line 3</line></paragraph>

Example (Output):<p>{Template for "Line 1"}{Template for "Line 2"}</p><p>{Template for "Line 3"}</p>

» Comment does not appear and is not handeled at all;– Because it is not selected (doesn't match the templates pattern)!

» Outputting the text with linebreak after each line:– <xsl:template match="line"><xsl:value-of select="."/><br/></xsl:template>

Handles contained line elements by printing their textual content.This is done by a default template!

Page 40: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 40XML Techniques for E-Commerce: Presentation

XSLT:Template Examples (2)

Simple (surnames of all authors in a group are put in italic):<xsl:template match="author-group"><i><xsl:apply-templates select="author/surname"/></i></xsl:template>

» All names on a single line, no whitespace inbetween!Better try: <xsl:template match="author-group"><i><xsl:apply-templates select="author/surname"/></i>, </xsl:template>

» Wrong! A single comma after all names (including the last)!Correct version: See example file (complex XPath+two rules)!

Multiple apply-template rules: Put domestic and foreign sales (mixed because ordered by time) in separate tables:

<xsl:template match="product"><table><xsl:apply-templates select="sales/domestic"/></table><table><xsl:apply-templates select="sales/foreign"/></table></xsl:template>

Page 41: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 41XML Techniques for E-Commerce: Presentation

XSLT:Template Examples (3)

Going up ("employee"s are somewhere within "department"s; "department"s have "group" child elements):

<xsl:template match="employee"><p>Employee <xsl:apply-templates select="name"/> belongs to group(s) <xsl:apply-templates select="ancestor::department/group"/></p></xsl:template>

» Take care: Does an employee belong to groups or does his department have these groups?

– Semantic difference!Next version: Print the employees group

» Problem: What about employees NOT in a group?– I.e. the department has no group; employee directly contained

Third version: Print only employees belonging to a group

Templates.xml, Templates.xsl, Templates.html

Page 42: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 42XML Techniques for E-Commerce: Presentation

XSLT:value-of

<xsl:value-of select=“expr”>content constructor</xsl:value-of>Allows inserting text from somewhere in the source tree (or a variable): Computes an arbitrary XPath expression

Creates text only: If it should be an attribute value, combination with <xsl:attribute> is required (or see below)!

Short version for attribute values:"{" expression "}": Replaced by the result of the expression

Example:<p><xsl:value-of select="@given-name"/><xsl:text> </xsl:text><xsl:value-of select="@family-name"/></p>

» Print given name, a space and family name in a paragraph» Data is in attributes of the current element» Remove the "@": value is stored in direct subelements (children)

Value-of.xml, Value-of.xsl, Value-of.html

Page 43: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 43XML Techniques for E-Commerce: Presentation

XSLT:Programmatically creating elements

<xsl:element name="elemName" namespace="nsName">Content </xsl:element>

Name and namespace define exactly what element to createThe content of the "xsl:element" element is where attributes for the element, contained text, and its children are created

» Attributes are created separately, not within the "element" start tag!Does not create tags but complete elements (start AND end tag)!

Examples:<xsl:element name="br"/>: "<br/>" (an empty <br> element)<xsl:element name="p">Chapter</xsl:element>: "<p>Chapter</p>"<xsl:element name="ypos"><xsl:value-of select="vertical"/></xsl:element>

» Source: <vertical><measurement unit="cm">12</measurement></vertical>» Result: "<ypos>12</ypos>"

– “measurement” and "unit" are lost, as only text content is selectedElements.xml, Elements.xsl, Elements.html

Page 44: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 44XML Techniques for E-Commerce: Presentation

XSLT:Programmatically creating attributes

<xsl:attribute name="attrName" namespace="nsName">Content</xsl:attribute>Resulting value of the attribute is the content (must be text only!)

Other values are ignored or produce errorsAdding attributes to an element

E. g. <p><xsl:attribute name="align">right</xsl:attribute>Right</p>» Result: <p align="right">Right</p>

Must appear within the element content before any children!Example: Creating a hyperlink to a details page:

» <a><xsl:attribute name="href">page?ID=<xsl:value-of select="@id"/> </xsl:attribute>Details</a>

» Source: <detaillink id="356s7"/>; » Result: <a href="details?ID=356s7">Details</a>

Attributes.xml, Attributes.xsl, Attributes.html

Page 45: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 45XML Techniques for E-Commerce: Presentation

XSLT:Element+attributes example

Prints the colours name in exactly this colour<xsl:template match="colour">

Colour:<xsl:element name="font">

<xsl:attribute name="color"> <xsl:value-of select="."/>

</xsl:attribute> <xsl:value-of select="."/>

</xsl:element><br/>

</xsl:template>

Colour.xml, Colour.xsl, Colour.html

Create color attribute

Create color name (=text)

}

Page 46: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 46XML Techniques for E-Commerce: Presentation

XSLT:Creating text

Text can be created either directly or explicitlyEvery text remaining after whitespace-stripping is just copied

<xsl:text>content constructor</xsl:text>Will create literal text content

Output escaping can be set manually» Not described here in detail!

Examples:<xsl:text disable-output-escaping=„yes“>&lt;</xsl:text>Produces the string “&lt;” (End result: “<“)<xsl:text disable-output-escaping=„no“>&lt;</xsl:text>Produces the string „&amp;lt;“ (End result: “&lt;”)

T_PI_C.xml, T_PI_C.xsl, T_PI_C.html

Page 47: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 47XML Techniques for E-Commerce: Presentation

There’s much more to discover in XSLT!

Copying from the source treeCreating other content: Processing instruction, comments, …Numbering, sorting, number formattingVariables and parametersNamespace aliasing:

E. g. creating a stylesheet by a stylesheetMultiple source documentsKeysMessagesExtensionsCombining stylesheets: Include, import, overriding, prioritiesWhitespace stripping: Keep or remove WS in the stylesheet?Procedural output creation: calling templates, conditionals, repetition......

Page 48: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 48XML Techniques for E-Commerce: Presentation

Some XML technologies

XML

HTML

XML

XML

Schema

XMLName-space

XSLT

ebXML, SOAP,

SecurityMetadata,

...

XPath

Java

FO

Page 49: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 49XML Techniques for E-Commerce: Presentation

XSL (= XSL-FO):

An XML vocabulary for specifying formatting semanticsMedia independent presentation lang., concerned with visual result

Based on CSS2 and DSSSLMost things taken, others changed, some added

Supports page layoutCan be a scrolling window or a printed page (e. g. PDF document)Content is converted to a tree of areas

» An area has content, padding and bordersAreas can be block (similar to <p>) or inline (similar to <b>)

Supports internationalization: Unicode, writing direction, etc.Not all software products supports all aspects, however!

Many formatting elements with high number of parametersVERY complicated: Possibly a reason for low widespread usage!Especially suited for automated publishing of dynamic data

Page 50: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 50XML Techniques for E-Commerce: Presentation

FO - Basic structure

Layout master set: Defines geometry and sequencing of pagesPage master: Describes a single page and ist subdivisionPage sequence master: Sequence of page masters to use for generating the content (e.g. first page might use a different master)

Static content (appears on many pages): static-content"Real" content (to be distributed over the pages): flowBlock level objects: Paragraphs, titles, etc.

Will always be a rectangle in rendering» Example: <fo:block margin-left="1cm">Indented paragraph.</fo:block>

Inline level objects: portion of text, e.g. italic, font, etc.Can be of different shape

» Example: <fo:inline font-weight="bold">italic text</fo:inline>

Page 51: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 51XML Techniques for E-Commerce: Presentation

XSL (= XSL-FO):

Typical use: XML ⇒ XSL-FO ⇒ PDF/PS/...Free software available: Apache FOP

Example: Parts from Shakespeares "As you like it"Actual content: Book.xml (Book.dtd)Stylesheet for conversion to XSL-FO: Book_FO.xslResult of conversion to XSL-FO by XSLT: Book_FO.xmlResult of conversion to PDF by FOP: Book.pdf

For comparison:» Stylesheet for conversion to HTML: Book_HTML.xsl» Result of conversion to HTML by XSLT: Book.html

FOXSLTXML FOP PDF

...

Page 52: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 52XML Techniques for E-Commerce: Presentation

Literature:Specifications

XPath 1.0 Specificationhttp://www.w3.org/TR/xpath/XSL Transformations (XSLT) 1.0 Specificationhttp://www.w3.org/TR/xslt/XSL 1.0 Specificationhttp://www.w3.org/TR/xsl/

XPath 2.0 Specificationhttp://www.w3.org/TR/xpath20/XSL Transformations (XSLT) 2.0 Specificationhttp://www.w3.org/TR/xslt20/XSL 1.1 Specification (Working draft!)http://www.w3.org/TR/2003/WD-xsl11-20031217/

Page 53: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 53XML Techniques for E-Commerce: Presentation

Literature:XSLT

W3Schools XSLT Tutorialhttp://www.w3schools.com/xsl/default.aspChapter 17 of XML Bible: XSL Transformationshttp://www.ibiblio.org/xml/books/bible2/chapters/ch17.htmlMiloslav Nic: XSLT Tutorialhttp://www.zvon.org/xxl/XSLTutorial/Output/index.html

Page 54: XPath, XSLT, FO XML Techniques for E-Commerce, Budapest 2004€¦ · Michael Sonntag XML Techniques for E-Commerce: Presentation 6 XSL Overview (2) zUses XPath for refering to and

Michael Sonntag 54XML Techniques for E-Commerce: Presentation

Literature:XSL-FO

Apache FOPhttp://xml.apache.org/fop/XSL-FO Tutorialhttp://www.renderx.com/tutorial.htmlW3Schools XSL-FO Tutorialhttp://www.w3schools.com/xslfo/default.aspTwo articles with a brief introduction to FOhttp://www.xml.com/pub/a/2001/01/17/xsl-fo/index.htmlhttp://www.xml.com/pub/a/2001/01/24/xsl-fo/index.htmlChapter 18 of XML Bible: XSL Formatting Objectshttp://www.ibiblio.org/xml/books/bible2/chapters/ch18.html