1.12k likes | 1.23k Views
Introduction to XSLT. Justin Tilton instructional media + magic, inc. As presented at the JA-SIG Winter Conference: "All Java, All the Time" December 8 th 2002, Orlando, Florida. The Abstract….
E N D
Introduction to XSLT Justin Tilton instructional media + magic, inc. As presented at the JA-SIG Winter Conference: "All Java, All the Time" December 8th 2002, Orlando, Florida
The Abstract… Looking for a methodology to quickly and effectively create Transformations? Interested in the basics of XSLT and Xpath, and a good way to get started? If so, this workshop is for you! We will be discussing the fundamental concepts of XSLT and Xpath, and the methodologies that have emerged from months of developing stylesheet transformations for the uPortal 2.0 project. We will discuss the design aspects related to converting structured information in XML into device-dependent markup languages such as HTML, and WML, and the guidelines and best practices evolving from this experience. No prior XSLT experience is necessary.
Overview • Introduction • uPortal • Basic XPath • Basic XSLT • Markup: XHTML • Cascading Style Sheets • Tools • The Creation Process • Hands-on
Background • Who: W3C • What: XPath and XSLT Specs. • When: 11/16/1999 • Why: A need arose for a specification to define the syntax and semantics for transforming XML documents.
References • “The” definitivereference… • Michael Kay • Wrox Press Inc • ISBN: 1861005067
References • Great practicalreference… • Jeni Tennison • Hungry Minds • ISBN: 0764547763
References • Practical use oftransformationsin Java code • Eric Burke • O'Reilly & Assoc. • ISBN: 0596001436
What’s new in 2.x? • Abstraction of layout • Structure/theme transformations • Standard channel events • Standard CSS classes • More flexible publish/subscribe • User profile management • Groups and Permissions updates
Basic Architecture Framework Structure XSLT Theme XSLT Skins - CSS
Basic Architecture Permissions iPlanet LDAP authentication User preferences Channel registry Other uPortal Data Oracledb2 mySQL
Channel • Elementary unit of presentation, defined by the IChannel interface User InteractionExternal Information Channel Content(Presentation) IChannel
IChannel content must • Be well-formed XML such as XHTML, RSS, SVG, SMIL, or a SOAP message (HTML is not well formed XML) • Rendered by an XSL transformation using an XSL stylesheet
Framework Organization User Interaction Presentation uPortal Framework Channel Channel Channel
User Layout • User Layout is an abstract structure defining the overall content available to the user • userLayout is a tree structure consisting of “folders” and “channels”, the later always being the leaf nodes
User Layout Tab Tab Tab Column Channel Channel Channel Column Channel Channel Channel Channel Channel Structure Transformation
Theme Transformation User Layout Tab Tab Tab Jim Smith Financial Aid Library Column Column Channel Channel Channel Channel Channel Channel Dictionary.com Bookmarks Cartoon
Compiling the Presentation userLayout Structuretransformation XSLT structuredLayout setRuntimeData() XSLT Channels Themetransformation renderXML() HTML, WML VoiceML...
Content Transformation XML XSLT Processor XHTML: Web Browser HTML: PDA Stylesheet WML: Cell Phone
Flexible Layouts • Structures • Tab / column • Tree / column • Themes • Multi-column • Multi-row • Skins • Matrix, Java
User Preferences • Swappable layout and preference management modules • Profile management module • Tab-column specific prefs. module • Skin selection
Publish/Subscribe • Channel publishing document • Channel parameters • Default values • Modification permissions • Descriptions • Publish/Subscribe steps • Step sequence • Instructions, help • A complex channel with multiple XSL views
Nodes and Node Trees • When an application wants to operate on an XML document it builds an internal model of what the document looks like. • This model is known as a document object model or DOM. • In XPath and XSLT, it's called a node tree.
Types of Nodes • Root nodes • The top of the node tree • Element nodes • XML elements • Attribute nodes • XML attributes • Text nodes • Textual content in XML elements • Comment nodes • XML comments • Processing instruction nodes • XML processing instructions • Namespace nodes • The in-scope namespaces on an element R E E E E A T
document para para para note warning warning content content content Node Tree Example <document> <para type=“note”> content </para> <para type=“warning”> content </para> <para type=“warning”> content </para> </document>
XPath Definition • XPath is a language for addressing parts of an XML document, designed to be used by XSLT. • Example XPath: child::para[attribute::type='warning'][position()=2] • In English: • select the second parachild of the context node that has a type attribute with a value of warning.
Context node Axis Dissecting the Example child::para[attribute::type='warning'][position()=2] Axis: child::para Filter: [attribute::type='warning'] Filter: [position()=2]
Dissecting the Example child::para[attribute::type='warning'][position()=2] Axis: child::para Filter: [attribute::type='warning'] Filter: [position()=2] Context node Axis Filtered
Dissecting the Example child::para[attribute::type='warning'][position()=2] Axis: child::para Filter: [attribute::type='warning'] Filter: [position()=2] Context node Axis Filtered Filtered
Dissecting the Example child::para[attribute::type='warning'][position()=2] Axis: child::para Filter: [attribute::type='warning'] Filter: [position()=2] Context node Axis Filtered Filtered Selected
Types of XPaths • Expressions: Return a value, which might be a node set that is processed or a string that is output. <xsl:when test="@type=‘warning’"> • Patterns: Either match a particular node or don't match that node. <xsl:template match="para">
XPath Expressions • Select Nodes <xsl:for-each select=“child::Z”> • Conditional <xsl:if test=“position()=2”> • Calculation <xsl:value-of select=“position()+4"> C Z Z C 1 2 3 4
Node Set Expressions • The most common way that XSLT uses XPaths is to select node sets. These XPaths usually occur within a select attribute, for example on xsl:for–each or xsl:apply–templates, and are known as location paths. <xsl:for-each select=“child::para”> <xsl:apply-templates select="paragraph"/>
Location Paths • The purpose of location paths is to select node sets from a node tree. • Location paths can be absolute or relative • Absolute location pathsstart from a known locationsuch as the root node <xsl:for-each select=“/R/N”> • Relative location paths start from the context node. <xsl:for-each select=“N”>note: same as “child::N” R N X Y N R N X Y C ontext N Z
Steps • A location path is made up of a number of steps. • Each step takes you from a node to a node set. • Each step is separated from the one before it with a “/”.