Crash Course in XSLT. Beginning XSLT Jan. 17, 2007 David Z. Maze

Similar documents
Markup Sprachen und semi-strukturierte Daten

XSL - Introduction and guided tour

XSL Elements. xsl:copy-of

T XML in 2 lessons! %! " #$& $ "#& ) ' */,: -.,0+(. ". "'- (. 1

F O R U M N O K I A. Implementing Trucking Logistics with Nokia Handsets. Version 1.0; July 18, Java /XML

XSLT - A Beginner's Glossary

aconferenceonxml Lesser Town Campus Prague, Czech Republic June 17-18, 2006

By Nabil ADOUI, member of the 4D Technical Support team

Interactive, dynamic Scalable Vector Graphics

Chapter 3: XML Namespaces

XML and Tools. Muhammad Khalid Sohail Khan Mat #: University of Duisburg Essen Germany

INTEROPERABILITY BETWEEN VISUAL UML DESIGN APPLICATIONS AND AUTHORING TOOLS FOR LEARNING DESIGN

Extending XSLT with Java and C#

Filen ex_e.xml. Her kommer koderne Det der står skrevet med fed er ændret af grp <?xml version="1.0"?>

WTP-101 Developing Web Applications with Standards. using W3C org standard technologies such as, HTML, CSS, XML, XSD and XSL

IBM Tivoli Federated Identity Manager V6.2.2 Implementation. Version: Demo. Page <<1/10>>

Get Success in Passing Your Certification Exam at first attempt!

2009 Martin v. Löwis. Data-centric XML. Other Schema Languages

Data Integration through XML/XSLT. Presenter: Xin Gu

Formatting JATS. as easy as 1-2-3

XML WEB TECHNOLOGIES

Stylus Studio 2010 XML Feature Comparison Matrix

Electronic Bank Account Management - EBAM

XSLT Mapping in SAP PI 7.1

WTP-101 Developing Rich Internet Applications with Java Web Frameworks

Enabling Web Services

Extending the Linked Data API with RDFa

OOML: Structured Approach to Web Development

How to Develop Stylesheet for XML to XSL-FO Transformation

Cross Site Scripting (XSS) and PHP Security. Anthony Ferrara NYPHP and OWASP Security Series June 30, 2011

Building A Very Simple Website

Design and implementation of a mobile database for Java phones

Document Builder. Thien Tran Department of Computer Science San Jose State University

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Guile Present. version 0.3.0, updated 21 September Andy Wingo

Data XML and XQuery A language that can combine and transform data

Web Server Logs Analyze Using the XML Technologies

Algorithms and Data Structures Written Exam Proposed SOLUTION

Schematron Validation and Guidance

LabVIEW Internet Toolkit User Guide

Last Week. XML (extensible Markup Language) HTML Deficiencies. XML Advantages. Syntax of XML DHTML. Applets. Modifying DOM Event bubbling

Cleo Communications. CUEScript Training

Markup Languages and Semistructured Data - SS 02

ART 379 Web Design. HTML, XHTML & CSS: Introduction, 1-2

XQuery Language. Introduction to databases CSCC43 Spring 2012 Ryan Johnson

Creating a TEI-Based Website with the exist XML Database

David RR Webber Chair OASIS CAM TC (Content Assembly Mechanism)

Application development in XML

2 Problem description

Full and Complete Binary Trees

XML and Data Management

Oracle BI Publisher 10g Best Practices -- Session # 8633

10CS73:Web Programming

IMPLEMENTING AN XML COURSE IN THE COLLEGE OF BUSINESS

Offline Image Viewer Guide

TagSoup: A SAX parser in Java for nasty, ugly HTML. John Cowan (cowan@ccil.org)

XML for RPG Programmers: An Introduction

What is XSLT? XSLT: Using XML to transform. XSLT vs XQUERY. XSLT processing model. An XML based style sheet language for XML documents

Ordered Lists and Binary Trees

Introduction to Ingeniux Forms Builder. 90 minute Course CMSFB-V6 P

6. SQL/XML. 6.1 Introduction. 6.1 Introduction. 6.1 Introduction. 6.1 Introduction. XML Databases 6. SQL/XML. Creating XML documents from a database

XLIFF 1.2. A white paper on version 1.2 of the XML Localisation Interchange File Format (XLIFF)

How To Use X Query For Data Collection

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

XML Databases 6. SQL/XML

Advanced Customisation: Scripting EPrints. EPrints Training Course

XSLT File Types and Their Advantages

Exchanger XML Editor - Canonicalization and XML Digital Signatures

Agents and Web Services

Example. Represent this as XML

Data Structures and Algorithms

Structured vs. unstructured data. Motivation for self describing data. Enter semistructured data. Databases are highly structured

Web Caching With Dynamic Content Abstract When caching is a good idea

INTRO TO XMLSPY (IXS)

Semistructured data and XML. Institutt for Informatikk INF Ahmet Soylu

1. To start Installation: To install the reporting tool, copy the entire contents of the zip file to a directory of your choice. Run the exe.

Java and XML parsing. EH2745 Lecture #8 Spring

Web Services Technologies

Human Resources (HR) Query Basics

An XML Based Data Exchange Model for Power System Studies

MarkLogic Server. Application Developer s Guide. MarkLogic 8 February, Last Revised: 8.0-4, November, 2015

Understand for FORTRAN

WWW. World Wide Web Aka The Internet. dr. C. P. J. Koymans. Informatics Institute Universiteit van Amsterdam. November 30, 2007

Deferred node-copying scheme for XQuery processors

Analysis report examination with CUBE

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Structured vs. unstructured data. Semistructured data, XML, DTDs. Motivation for self-describing data

Rapporto tecnico N.45

Using Database Metadata and its Semantics to Generate Automatic and Dynamic Web Entry Forms

Smooks Dev Tools Reference Guide. Version: GA

Call Center Reports Customization Guide

How To Use Query Console

The C Programming Language course syllabus associate level

Petri Net Markup Language: Implementation and Application

Introduction to XML Applications

Semantic Web Languages: RDF vs. SOAP Serialisation

by LindaMay Patterson PartnerWorld for Developers, AS/400 January 2000

Terms and Definitions for CMS Administrators, Architects, and Developers

Indexing XML Data in RDBMS using ORDPATH

Web Frameworks and WebWork

Transcription:

Crash Course in XSLT Beginning XSLT Jan. 17, 2007 David Z. Maze 1 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Last Time... * XML syntax * XML namespaces * XPath 2 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

A Trivial Stylesheet <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml"/> <xsl:template match="/"> <xsl:copy-of select="/"/> </xsl:template> </xsl:stylesheet> 3 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Making It Go * Command-line processor, like xsltproc * xsltproc stylesheet.xsl input.xml * Java javax.xml infrastructure * xml-stylesheet PI plus Web browser * <?xml-stylesheet type="text/xml" href="foo.xsl"?> 4 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

What Do You Get Out? * <xsl:output method="xml" indent="no"/> * Possible output methods: xml, html, text * XHTML is "xml"; default is either "xml" or "html" depending on name of root element * Some other options, but probably bad form to depend on them 5 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Templates * <xsl:template match="foo:bar" mode="baz"> * Think "subroutine" * Can take parameters, have local variables, contains XSLT "statements" * Optional mode controls when the template is invoked * match attribute is restricted XPath expression with child or attribute axes, // and operators, and arbitrary predicates 6 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Easy Ways To Get Output * <number>42</number> (literal result element): non-xslt XML (or text) is copied to output * <xsl:text>foo</xsl:text> * <xsl:copy-of select="..."/> copies entire selected subtree(s) (if any) * <xsl:value-of select="..."/> evaluates expression, takes string value (discarding XML tags), and outputs that 7 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Counting Things <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml"/> <xsl:template match="/"> <counts> <count item="slides"> <xsl:value-of select="count(//slide:slide)" xmlns:slide= "http://www.mit.edu/~dmaze/slide"/> </count> <count item="graphics"> <xsl:value-of select="count(//svg:svg)" xmlns:svg="http://www.w3.org/2000/svg"/> </count> </counts> </xsl:template> </xsl:stylesheet> 8 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Applying Templates * <xsl:apply-templates select="*" mode="baz"/> * There's always a context node * "Apply templates": find some set of other nodes by XPath expression; then invoke the matching template, but only if the mode matches * Could do nothing if no nodes match * Default for text is to copy to output 9 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Matching Multiple Templates <xsl:template match="xhtml:p"/> <xsl:template match="xhtml:*"/> * What do you do if multiple templates match? * Top-level stylesheet first * Highest priority (explicit xsl:template/@priority) wins * Default priority: 0 for child/attribute full-name match; -0.25 for child/attribute namespace match (pfx:*); -0.5 for child/element kind match (child::text()); 0.5 for anything else * Still a tie? "Error", but last one in stylesheet wins 10 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

HTML to XSL:FO <p>text with <i>italic</i> word</p> <xsl:template match="p"> <fo:block> <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="i"> <fo:inline font-style="italic"> <xsl:apply-templates/> </fo:inline> </xsl:template> <fo:block> Text with <fo:inline font-style="italic">italic</fo:inline> word </fo:block> 11 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Modes * Sometimes, just want to copy an element; other times, want to do full processing; other times... * Can create/invoke template with named mode * <xsl:template match="..." mode="..."> * <xsl:apply-templates select="..." mode="..."/> * Defaults to no mode at all 12 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Let's go backwards! <xsl:template match="*"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:apply-templates select="*[last()]"/> </xsl:copy> <xsl:apply-templates select="preceding-sibling::*[1]"/> </xsl:template> 13 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Named templates * Templates can have names as well as match expressions * <xsl:call-template name="..."/> * Context node same as in caller 14 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Let's go backwards! (again) <xsl:template name="attr-children"> <xsl:copy-of select="@*"/> <xsl:apply-templates select="*[last()]"/> </xsl:template> <xsl:template match="*" mode="child"> <xsl:call-template name="attr-children"> </xsl:template> <xsl:template match="*"> <xsl:call-template name="attr-children"> <xsl:apply-templates select="preceding-sibling::*" mode="child"/> </xsl:template> 15 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Attribute value templates (AVTs) <xsl:template match="x"> <x count="{count(child::*)}"> <xsl:apply-templates/> </x> </xsl:template> * Any XPath expression can go inside curly braces in many contexts * (Including attribute values in LREs) 16 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

I don't know my name * <xsl:element name="..." namespace="..."> * <xsl:attribute name="..." namespace="..."> * Contents of element/attribute used as contents of element/attribute * Name and namespace attributes are both AVTs * This is also useful for only maybe generating attributes * Can't use xsl:attribute after first child has been output 17 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Picking a namespace <xsl:element name="..." namespace="..."> * Is namespace present? Use its value (possibly empty) * Does name have a prefix? Use its namespace from the stylesheet * Otherwise? Use the null namespace * The generated element will have no prefix if in the null namespace, or otherwise the processor's choice of namespace (possibly the one in name) 18 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Comments In The Output <!-- Comment in the stylesheet --> <xsl:comment>in the output</xsl:comment> 19 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

What if... <xsl:if test="...">... </xsl:if> * If/then, without an "else" * test is any (boolean) XPath expression * Contents are evaluated if test is true (non-empty) 20 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

But what if... <xsl:choose> <xsl:when test="...">...</xsl:when> <xsl:when test="...">...</xsl:when> <xsl:otherwise>...</xsl:otherwise> </xsl:choose> * If first xsl:when is true, do that; otherwise, if second is true, do that, otherwise... * Only way to have "else" 21 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

How many is many? <xsl:attribute name="quantity"> <xsl:choose> <xsl:when test="count(*)=0">none</xsl:when> <xsl:when test="count(*)=1">one</xsl:when> <xsl:when test="count(*)<8">some</xsl:when> <xsl:otherwise>many</xsl:otherwise> </xsl:choose> </xsl:attribute> <xsl:if test="*"> <xsl:attribute.../> </xsl:if> 22 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Variables <xsl:variable name="..." select="..."/> <xsl:variable name="...">...</xsl:variable> * Defines a variable for all following things in the same template, plus their descendants * No duplicate variables in the same template (might shadow globals) * select form can have arbitrary XPath * Otherwise create result tree fragment with root node and specified contents * <xsl:copy-of select="$variable"/> * No content? Defaults to empty string * select="/.." for empty nodeset * Get value later using XPath $name 23 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

How many is many? <xsl:variable name="children" select="*[@on='yes']"/> <xsl:variable name="count" select="count($children)"/> <xsl:attribute name="quantity"> <xsl:choose> <xsl:when test="$count=0">none</xsl:when> <xsl:when test="$count=1">one</xsl:when> <xsl:when test="$count<8">some</xsl:when> <xsl:otherwise>many</xsl:otherwise> </xsl:choose> </xsl:attribute> 24 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Parameters <xsl:param name="..." select="..."/> <xsl:param name="...">...</xsl:param> <xsl:apply-templates> <xsl:with-param name="..." select="..."/> <xsl:with-param name="...">...</xsl:with-param> </xsl:apply-templates> * Just like xsl:variable, but... * Must appear first in template content * xsl:param's value is only the default value * Callers may specify xsl:with-param to provide their own values 25 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Backwards (yet again) <xsl:template match="*"> <xsl:param name="pos" select="count(*)"/> <xsl:variable name="item" select="*[$pos]"/> <xsl:element name="{local-name($item)}"> <xsl:apply-templates select="$item"/> </xsl:element> <xsl:if test="$pos>1"> <xsl:apply-templates select="."> <xsl:with-param name="pos" select="$pos-1"/> </xsl:apply-templates> </xsl:if> </xsl:template> 26 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Globals * You can define global xsl:variable and xsl:param * Put them before the first template * They can depend on input * Global params can usually be set externally * xsltproc --param foo "'bar'" 27 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

For-each <xsl:for-each select="...">... </xsl:for-each> * Evaluates contents for each node in select node-set * Context node changes 28 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Sorting <xsl:apply-templates> <xsl:sort select="@name"/> </xsl:apply-templates * Specify an order besides document order for xsl:apply-templates and xsl:for-each * order="ascending" (or descending) * data-type="text" (or number) * Multiple sorts fine, first is "more important" 29 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Modularizing <xsl:include href="..."/> <xsl:import href="..."/> * Stylesheets you include can include other stylesheets * Circular includes are illegal * xsl:include is "the same stylesheet", xsl:import isn't 30 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007

Next Time * Gory details! * XPath types, function library * Extension elements and EXSLT 31 Crash Course in XSLT: Beginning XSLT Jan. 17, 2007