XSLT Part 3B. id() The id() function returns a node-set containing the node or nodes with a given ID attribute. An ID attribute in this context is any.

Slides:



Advertisements
Similar presentations
Chungnam National University DataBase System Lab
Advertisements

XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents.
Advanced XSLT II. Iteration in XSLT we sometimes wish to apply the same transform to a set of nodes we iterate through a node set the node set is defined.
Advanced XSLT. Branching in XSLT XSLT is functional programming –The program evaluates a function –The function transforms one structure into another.
Advanced XSLT II. Iteration in XSLT we sometimes wish to apply the same transform to a set of nodes we iterate through a node set the node set is defined.
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
+ XSL eXtensible Stylesheet Language. + 2 XML Lecture Adapted from the work of Prof Mark Baker ACET, University of Reading.
XML and XSL Transforming your XML documents with eXtensible Stylesheet Language Transformations [Optional Lecture]
1 CP3024 Lecture 9 XML revisited, XSL, XSLT, XPath, XSL Formatting Objects.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 13-1 COS 346 Day 25.
Rendering XML documents with XSL The most powerful approaches to rendering XML documents involve using XSL (eXtensible Stylesheet Language) XSL enables.
Processing XML Processing XML using XSLT Processing XML documents with Java (DOM) Next week -- Processing XML documents with Java (SAX)
XSL Transformations (XSLT) Meghasyam Bokam April’1 st, 2002.
XSL Concepts Lecture 7. XML Display Options What can XSL Transformations do? generation of constant text suppression of content moving text (e.g., exchanging.
1 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Transform XML Documents Roger L. Costello XML Technologies.
XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.
Internet Technologies XSLT Processing XML using XSLT Using XPath Escaping to Java.
CS 4408 Lecture 9. XSLT instruction elements We have seen that a template can contain non-XSLT text, canned text that it inserts into the result tree.
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
1 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Using Extension Elements and Extension Functions with XSLT and XPath Roger L. Costello.
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
17 Apr 2002 XML Stylesheets Andy Clark. What Is It? Extensible Stylesheet Language (XSL) Language for document transformation – Transformation (XSLT)
MC 365 – Software Engineering Presented by: John Ristuccia Shawn Posts Ndi Sampson XSLT Introduction BCi.
1 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Using Extension Elements and Extension Functions with XSLT and XPath Roger L. Costello.
Using XSLT and XPath to Enhance HTML Documents Reference: Roger L. Costello
10/06/041 XSLT: crash course or Programming Language Design Principle XSLT-intro.ppt 10, Jun, 2004.
Sheet 1XML Technology in E-Commerce 2001Lecture 6 XML Technology in E-Commerce Lecture 6 XPointer, XSLT.
CSE3201/CSE4500 XPath. 2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
1 XML Schemas. 2 Useful Links Schema tutorial links:
XSLT for Data Manipulation By: April Fleming. What We Will Cover The What, Why, When, and How of XSLT What tools you will need to get started A sample.
CSE3201/CSE4500 Information Retrieval Systems
XP New Perspectives on XML Tutorial 6 1 TUTORIAL 6 XSLT Tutorial – Carey ISBN
XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.
WORKING WITH XSLT AND XPATH
1 XML Data Management 7. XLST Werner Nutt. Kernow and Saxon To demo XSLT, we use Kernow –Kernow is a (graphical) front end for Saxon Saxon is an XSLT,
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL.
Processing of structured documents Spring 2002, Part 2 Helena Ahonen-Myka.
CSE3201/CSE4500 Information Retrieval Systems XSLT – Part 2.
1 XSLT An Introduction. 2 XSLT XSLT (extensible Stylesheet Language:Transformations) is a language primarily designed for transforming the structure of.
ECA 228 Internet/Intranet Design I XSLT Example. ECA 228 Internet/Intranet Design I 2 CSS Limitations cannot modify content cannot insert additional text.
CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "
XSLT part of XSL (Extensible Stylesheet Language) –includes also XPath and XSL Formatting Objects used to transform an XML document into: –another XML.
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Transforming Documents „a how-to of transforming xml documents“ Lecture on Walter Kriha.
Lecture 11 XSL Transformations (part 1: Introduction)
 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 – XSL: Extensible Stylesheet Language Transformations (XSLT) Outline 12.1Introduction 12.2Setup.
Lecture 15 XSL Transformations (part V) Acknowledgment:
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Applying eXtensible Style Sheets (XSL) Ellen Pearlman Eileen Mullin Programming.
1 Overview of XSL. 2 Outline We will use Roger Costello’s tutorial The purpose of this presentation is  To give a quick overview of XSL  To describe.
XP New Perspectives on XML, 2 nd Edition Tutorial 8 1 TUTORIAL 8 CREATING ELEMENT GROUPS.
1 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Transform XML Documents Roger L. Costello XML Technologies.
XSLT. XSLT stands for Extensible Stylesheet Language Transformations XSLT is used to transform XML documents into other kinds of documents. XSLT can produce.
More XML XPATH, XSLT CS 431 – February 23, 2005 Carl Lagoze – Cornell University.
 XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
CSE3201/CSE4500 Information Retrieval Systems XSLT – Part 2.
XSLT: How Do We Use It? Nancy Hallberg Nikki Massaro Kauffman.
C Copyright © 2011, Oracle and/or its affiliates. All rights reserved. Introduction to XML Standards.
1 Whitespace Handling Roger L. Costello XML Technologies.
1 Extensible Stylesheet Language (XSL) Extensible Stylesheet Language (XSL)
XML Schema – XSLT Week 8 Web site:
1 XSL Transformations (XSLT). 2 XSLT XSLT is a language for transforming XML documents into XHTML documents or to other XML documents. XSLT uses XPath.
1 Extensible Stylesheet Language (XSL) Extensible Stylesheet Language (XSL)
CH 15 XSL Transformations 1. Objective What is XSL? Overview of XSL transformations Understanding XSL templates Computing the value of a node with xsl:value-of.
Beginning XML 4th Edition. Chapter 8: XSLT Chapter 8 Objectives How XSLT can be used to convert XML for presentation or restructure XML for business-to-business.
Using XSLT and XPath to Transform XML Documents
Using Extension Elements and Extension Functions with XSLT and XPath
ACG 4401 XSLT Extensible Stylesheet Language for Transformations
Presentation transcript:

XSLT Part 3B

id() The id() function returns a node-set containing the node or nodes with a given ID attribute. An ID attribute in this context is any attribute declared in DTD as having type ID The id() function provides an efficient means of locating nodes given the value of ID attribute. Example:

generate-id() The generate-id() function generates a string that uniquely identifies a node. Generate-id() can be used to create links in documents. Generate-id() can be used to compare whether two nodes are identical.

Keys The ID/IDREF mechanism for locating elements in XML documents has been generalized in XSL to the notion of keys. ID/IDREF is only useful when: –The document has declarations that identify the ID and IDREF attributes and –The processor is capable of processing the declarations Using select expressions (XPath) to locate elements may be inefficient Declaring keys gives the stylesheet processor an indication of what elements should be cached for fast access.

Club.xml Smith Jones Boggs

Examples Club Members Name …. Home Phone Number

Output Club Members Name Smith Jones Boggs Home Phone Number

xsl:key Xsl:key is a top-level element used to declare a named key, for use with the key() functions in expressions and patterns. Global variables cannot be used in defining the key The name attribute specifies the name of the key. The match attribute is a pattern – specifies the nodes to which the key value applies. If a node matches the pattern, then the node will have zero or more values for the named key, as determined by use attribute. Example: Species an expression to determine the value or values of the key

Key() The key() function is used to find the nodes with a given value for a named key. It is used in conjunction with the element. The key() function is provided to make associative access to nodes more convenient and more efficient. Because keys provide an efficient way of retrieving all the nodes that share a common value, they are used to group nodes with common values.

Using an attribute for a key value <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:variable name="prodName" select ="key('prodId','7777')"/> Name: Price:

Using an element for a key value <xsl:stylesheet version="1.0" xmlns:xsl="

Multi-valued keys A key can be multi-valued, in that a single node can have several values each of which can be used to find the node independently. The use expression, instructor/name is a node-set expression, so the string value of each of its nodes (each instructor name) is used as one of the values in the set of node value pairs that make up the key. Example

Course.xml CPP 123 Jones Smith Undergraduate 4 XML 345 Smith Mills graduate 2 Java 432 Boggs Pratt graduate 2 Output (All courses where Smith is an instructor): CPP 123 Jones Smith Undergraduate 4 XML 345 Smith Mills graduate 2

Example with catalog.xml, authors.xml, authors.xsl and books.xml Fun With XML John Robot Xml and Java Mary Jones John Robot 7856 C++ James Mills 7777 John Robot USA Mary Jones UK James Mills Australia

Authors.xsl <xsl:stylesheet xmlns:xsl=" version="1.0">

It is possible to have several keys for the same node. It is also possible to have several key definitions with the same name.

Stylesheet Reuse via xsl:include and xsl:import The elements xsl:include and xsl:import enable you to reuse other stylesheets. These elements are “top-level elements”. This means that they must be immediate children of the xsl:stylesheet element (i.e., they cannot be within a template rule) The xsl:include element is basically a macro substitution - the element is replaced by the contents of stylesheet it references

<xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:stylesheet xmlns:xsl=" version="1.0"> Replace the xsl:include element with the contents of the referenced stylesheet (i.e., all the children of xsl:stylesheet) toUpperCase.xsl

xsl:import xsl:import acts just like xsl:include - the stylesheet that it references is macro-substituted. However, there is a difference: –With xsl:include the stuff that is macro-substituted into the stylesheet has the same precedence as the rest of the stylesheet. It is as though you had one stylesheet. –With xsl:import the stuff that is macro-substituted into the stylesheet has lower precedence than the rest of the stylesheet. Also, all xsl:import elements must come first in the stylesheet.

Xsl:document The is used to create a new output file (introduced in XSLT1.1). The facility allows transformation to produce multiple output files. When the xsl:document instruction is instantiated, a new result tree is created and the new result tree becomes the current output destination for all the nodes output until the end of xsl:document element. The location of the new output file is determined by the value of href attribute. This attribute is mandatory. The href may contain an absolute or relative (to the parent document) URI.

xsl:document What is the output on the input file, club.xml

Input: Club.xml Smith Jones Boggs

Function- document() The most common usage of document() is to access a document referenced from the source document (typically in an attribute such as href). The document() finds an external XML document by resolving an URI reference, parses the XML into a tree structure and returns its tree node. Example: document(“example.xml”) looks for file example.xml in the same directory as the stylesheet, parses it and returns the root node of the resulting tree.

document() <xsl:stylesheet version="1.1" xmlns:xsl=" 1999/XSL/Transform"> Reference: Output: Reference: Logic In Maintenance This is a review of Logic in Maintenance Reference: XSLT Design Patterns This is a review of XSLT Design Patterns

Extension Elements The XSL processor understands how to process xsl:template, xsl:apply-templates, xsl:if, xsl:for-each, etc –That is, it understands the vocabulary in the XSL namespace XSL Processor implementers oftentimes provide additional elements that you may use in your stylesheet –These extension elements will belong to a namespace defined by the implementer

The functions available in XPath cover only basic functionality. Sometimes, you may want to invoke code written in other languages from your stylesheet. The draft XSLT 1.1 specification defines a general mechanism for calling extension functions written in any language, and defines detailed interfaces for Java and Javascript. Interfaces for other languages may be defined by independent vendors.

When are extension functions needed To get data held in a database You may need to access services that are not directly available in XSLT or XPath. To perform complex operations that is cumbersome in XSLT.

Function-available() Using function-available() to test whether a particular function is available for use. Example: <xsl:stylesheet version="1.1" xmlns:xsl=" True

Example <xsl:stylesheet version="1.0" xmlns:xsl=" xmlns:date=" ">

Example <xsl:stylesheet version="1.0" xmlns:xsl=" xmlns:Math="

Example Extension Element: instruct the xsl processor to output to another file Many of the xsl processor implementers provide an extension element that instructs the xsl processor to output the contents of the element to another file. –Thus, your stylesheet can generate multiple output files! XSL Processor XML XSL

Vendor-specific Each implementor gives the extension element a different name: –saxon calls it: output –xalan calls it: write

How to use an extension element 1. Declare the namespace that the extension element belongs to: saxon: xmlns:saxon=" xalan: xmlns:xalan=" 2. Indicate that any element that is namespace qualified by the prefix is an extension element, i.e., it has a specific meaning and should be processed using the implementer's code: saxon: extension-element-prefixes="saxon" xalan: extension-element-prefixes="xalan" 3. Use the extension element: saxon: -- anything in here will go to the file specified --- xalan: -- anything in here will go to the file specified ---

Problem Write a stylesheet which outputs the platinum members in one file, the gold members in another file, and the third file is an index to the other two files.

FitnessCenter.xsl FitnessCenter.xml XSL Processor gold.xml new-FitnessCenter.xsl platinum.xml

FitnessCenter.xml Jeff lightgrey David lightblue Roger lightyellow

FitnessCenter.xsl ?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" xmlns:saxon=" extension-element-prefixes="saxon" version="1.0">

extension-element-prefixes The extension-element-prefixes is used to tell the xsl processor, "whenever you encounter an element with any of these prefixes listed here you are to treat it as an extension element, and process it using the implementer's code" If you fail to do so the xsl processor will simply output the element literally

Dynamic (run-time) Evaluation Many xsl processor implementers give you an extension function that enables you to dynamically evaluate an expression. –That is, you can generate the expression on the fly, or read it in from an external file. SAXON provides an extension function called evaluate to do this.

Example <xsl:stylesheet xmlns:xsl=" xmlns:saxon=" extension-element-prefixes="saxon" version="1.0">

Database Connectivity Using Saxon To use the SQL extension elements in a stylesheet, you need to define a namespace prefix (for example "sql") in the extension-element-prefixes attribute of the xsl:stylesheet element. This extension defines four new stylesheet elements: sql:connect, sql:insert, sql:column, and sql:close: sql:connect creates a database connection. It has attributes "driver", "database", "user", and "password", all of which are attribute value templates (so the values can be passed in as parameters). The driver attribute names the JDBC driver class to be used. The database name must be name that JDBC can associate with an actual database, and in the sample stylesheet this database must contain a a table "Book" with three character columns, "Title", "Author", and "Category". sql:insert performs an SQL INSERT statement. This causes a row to be added to the table identified by the "table" attribute. sql:column is used as a child element of sql:insert, and identifies the name and value of a column to be included in the INSERT statement. The name of the column is identified by the "name" attribute, the value may be indicated either by evaluating the expression contained in the "select" attribute, or as the expanded contents of the sql:column element. The value is always interpreted as a String. (Remember this is purely a demonstration of extensibility, in a real system there would be a need to cater for SQL columns of other data types). sql:close closes the database connection.

To populate an Access database from Books.xml using Books- sql.xsl <xsl:stylesheet xmlns:sql=" LElementFactory" xmlns:xsl=" version="1.0" xmlns:saxon=" extension-element-prefixes="saxon"> sql:connect is not available Connecting to... <sql:connect driver="{$driver}" database="{$database}" user="{$user}" password="{$password}" xsl:extension-element-prefixes="sql"> SQL extensions are not installed

To populate an Access database from Books.xml using Books- sql.xsl