The XPath to XSL Style

565 views
504 views

Published on

XPath is a powerful and intuitive way to retrieve information from, or apply style to, your document. In this session, you’ll discover how to use XPath whether you’re styling your documents with FOSI, XSLT/XSL-FO, or Arbortext Styler. Also presented will be the basics of writing XPath expressions, including examples of common use cases.

Attendees in this session will get an overview of how the XPath language can be used in Styler, FOSIs, and XSLT transforms for document processing tasks. This overview will provide the context needed to understand how the information presented in the session can be used in day-to-day work.

The second part of the session will provide an introduction to writing XPath expressions. Topics will include:
- The data model used to navigate a document with XPath
- The data types supported by the language and conversions between them
- Syntax of path, numeric and string expressions
- Tips and tricks for common tasks and pitfalls to avoid

Examples will be provided to demonstrate the concepts discussed in the first part of the session.

Published in: Software
3 Comments
1 Like
Statistics
Notes
No Downloads
Views
Total views
565
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
3
Likes
1
Embeds 0
No embeds

No notes for slide

The XPath to XSL Style

  1. 1. The XPath to XSL Style LizFraley,Single-SourcingSolutions 5/23/2014TC Dojo Open Session 1
  2. 2. Agenda • Background: XML, XSL, XYZ, … ? • XPath: Get your data • XSLT:Transform your data • Just the basics and avoiding the pitfalls STC Summit 2014-Fraley 2
  3. 3. Pieces of the Puzzle • XML: Structure and Syntax • DTD/XML Schema: Vocabulary and Grammar • XHTML and SVG: Presentation Applications • XSL-FO: Page-based (Print) Presentation • XSLT/XPath: Transformation and Styling • Xquery: Query, Analysis, Database Integration Information Architecture Technologies 3STC Summit 2014-Fraley
  4. 4. Vocabulary • Instance: A concrete representation of a DTD or Schema being executed • Source: Instance containing content (text, images, etc) to be transformed • Styling: Transforming and formatting information • Stylesheet: Instance defining the transformation from XML to some CVML • CVML: Custom Vocabulary Markup Language (text, html, FO, input to a compiler like CHM, eclipse, etc) • Result Tree: Result of transformation built by instantiated templates • “Templates” (1.0) “Sequence Constructors” (2.0): Defines how result tree is constructed (XPath) • Template Rule: Describes the pattern that matches notes in the source tree (XSL + Instance nodes) 4STC Summit 2014-Fraley
  5. 5. Processing Model 5STC Summit 2014-Fraley Concept and diagram from G. Ken Holman, “Practical Transformation Using XSLT and XPath”
  6. 6. XPath Data Model: Node Types • Root: Contains document element, PIs and comments • Element: Contains elements, text, PIs and comments • Attribute: Has an element parent, name, and string value • Namespace: Like attribute, name=prefix and string=URI • Text: Has string value • PI: Has name (PI target) and string value • Comment: Has string value What types of information are there? 6STC Summit 2014-Fraley
  7. 7. XPath Data Model: Axes • Down: child, descendant, descendant-or-self • Up: parent, ancestor, ancestor-or-self • Forward: following-sibling, following • Backward: preceding-sibling, preceding • Other: self, attribute, namespace How are these pieces of information connected to each other? 7STC Summit 2014-Fraley
  8. 8. STC Summit 2014 8 G. Ken Holman, “Practical Transformation Using XSLT and Xpath” Concept and diagram from G. Ken Holman’s “Practical Transformation Using XSLT and XPath” 8STC Summit 2014-Fraley
  9. 9. XPath Language: Context • Context node: Can change as expression is evaluated • Context position: Position of node within context set • Context size: Number of nodes n context set • Name and value of each variable available for use • Prefix and URI of each namespace available for use • Functions available for use Where do we start? 9STC Summit 2014-Fraley
  10. 10. XPath Language: Paths • Absolute: Leading “/” or “//” means start at the root node • Relative: No leading “/” means start at the “current” node • Steps: For each node in context set, select matching nodes in a given “direction” – step/axis::test[predicate] • Axis: The direction to “step” in, defaulting to “child” • Node Test: name, *, prefix:name, prefix:*, node(), text(), comment(), processing-instruction([target]) • Shortcuts: Child axis (default), attribute axis (@), self::node()(.), parent::node()(..), descendant-or- self::node()(//) How do I navigate my document(s)? 10STC Summit 2014-Fraley
  11. 11. XPath Language: Predicates • Test each node from the step individually • Evaluate the expression, then convert to a boolean • Node sets (usually a path) and strings must be non-empty – Note: [@abc] will be true for <e abc=“”/>! Use [string(@abc)] to test for actual content • Numbers: Filter by position; last(); forward/reverse axis How can I get just the nodes I want? 11STC Summit 2014-Fraley
  12. 12. XPath Path/Predicate Examples 12STC Summit 2014-Fraley • If you ever get the chance to take an in-person training from G. Ken Holman, take it! • The examples on the following slides are from his book: – “Practical Transformation Using XSLT and XPath” – Get it here: http://www.cranesoftwrights.com/training/index.htm
  13. 13. XPath Path/Predicate Examples child::note note ./note *[self::note] Child elements of current node with name “note” .//note Descendant elements from current node with name “note” //note Descendant elements from root node with name “note” ..//note Descendant elements of current node’s parent (possibly including the current node) with name “note” self::note Current node if name is “note” otherwise empty node set (not an error) 13STC Summit 2014-Fraley From G. Ken Holman’s “Practical Transformation Using XSLT and XPath”
  14. 14. XPath Path/Predicate Examples @type attribute::type attribute(type) [XPath 2.0] Attribute nodes with name “type” ../@type Parent’s attribute nodes with name “type” ../note/@type Attribute nodes named “type” of child nodes named “note” note/@type Attribute nodes named “type” of child nodes named “note” /.. Guaranteed empty node set (root never has a parent node) note[@type] Child nodes named “note” which have an attribute node named “type” note[@type=‘caution’]/@impor tance Attribute node named “importance” of child nodes who are named “note” and who also have an attribute node named “type” that has a value equal to the string “caution” 14STC Summit 2014-Fraley From G. Ken Holman’s “Practical Transformation Using XSLT and XPath”
  15. 15. XPath Path/Predicate Examples note[position()=3] The third child node named “note” note[3] | note[5] note[position()=3] | note[position()=5] note[position()>=3] | note[position()<=5] The 3rd and 5th child nodes named “note” (“union”) note[3 or 5] ALL child nodes named note because “3 or 5” always reduces to “true() or true()” (//note)[1] First “note” in entire document //note[1] /descendant-or-self::node()/note[1] Descendant element nodes from the root with the name “note” that are the first amongst sibling “note” elements *[note(self::note | self::ul)] All child elements except “note” or “ul” elements 15STC Summit 2014-Fraley From G. Ken Holman’s “Practical Transformation Using XSLT and XPath”
  16. 16. XPath Path/Predicate Examples preceding::note The set of all preceding elements named “note” to the start of the document (not just siblings) preceding-sibling::note[1] The closest preceding element node named “note” that is a sibling (“next oldest to me”) preceding-sibling::note[@type] The set of all preceding element nodes that are named “note” and that have a “type” attribute preceding-sibling::note[@type][1] The closest preceding sibling element node named “note” that has a “type” attribute preceding-sibling::note[1][@type] The closest preceding sibling element node named “note” if and only if it has a “type” attribute (if it doesn’t the result is empty set) preceding-sibling::note[@type][1]/@type The type attribute of the closest preceding sibling element node named “note” that has a “type” attribute preceding-sibling::note/@type[1] The attributes named “type” of all preceding element sibling nodes with the name “note” (the predicate has no effect) (preceding-sibling::note/@type)[1] The attribute named “type” of the furthest preceding element node with the name “note” (document order) (preceding-sibling::note/@type)[last()] The attribute named “type” of the closest preceding element node with the name “note” (document order) 16STC Summit 2014-Fraley From G. Ken Holman’s “Practical Transformation Using XSLT and XPath”
  17. 17. XPath Path/Predicate Examples preceding::note The set of all preceding elements named “note” to the start of the document (not just siblings) preceding-sibling::note[1] The closest preceding element node named “note” that is a sibling (“next oldest to me”) preceding-sibling::note[@type] The set of all preceding element nodes that are named “note” and that have a “type” attribute preceding-sibling::note[@type][1] The closest preceding sibling element node named “note” that has a “type” attribute preceding-sibling::note[1][@type] The closest preceding sibling element node named “note” if and only if it has a “type” attribute (if it doesn’t the result is empty set) preceding-sibling::note[@type][1]/@type The type attribute of the closest preceding sibling element node named “note” that has a “type” attribute preceding-sibling::note/@type[1] The attributes named “type” of all preceding element sibling nodes with the name “note” (the predicate has no effect) (preceding-sibling::note/@type)[1] The attribute named “type” of the furthest preceding element node with the name “note” (document order) (preceding-sibling::note/@type)[last()] The attribute named “type” of the closest preceding element node with the name “note” (document order) 17STC Summit 2014-Fraley From G. Ken Holman’s “Practical Transformation Using XSLT and XPath”
  18. 18. XPath Path/Predicate Examples ancestor-or-self::*[1] Self or the closest ancestral element node of any name (ancestor-or-self::*/@type)[1] The element named “type” of the furthest ancestral element node of any name id(@href)/@type The attribute named “type” of the node that has a unique identifier with value equal to the current node’s “href” attribute node 18STC Summit 2014-Fraley From G. Ken Holman’s “Practical Transformation Using XSLT and XPath”
  19. 19. XPath Language: Data Types and Conversions • Boolean: true (non-zero or non-empty) or false • Number: floating point (1/0, “-1.234”) • String: Unicode (true/false, 0/-Infinity/Infinity/NaN, string value of the first node in document order) • Node Set: Unordered set of unique nodes (no duplicates) • Result Tree Fragment (RTF): Non-navigable node set (XSLT variable value) What types of data are there and how do conversions between them work? 19STC Summit 2014-Fraley
  20. 20. XPath Language: Operators and Functions • Boolean: =, !=, <, <=, >, >=, and, or, not(), true(), false() • Number: +, -, *, div, mod, sum(), floor(), ceiling(), round() • String: concat(), starts-with(), contains(), substring(), substring-before(), substring-after(), string-length(), normalize-space(), translate() • Node Set: /, //, [], | , name(), namespace-uri(), local-name(), id(), count(), position(), last() • XSLT: document(), current(), generate-id(), format-number(), key() How do I compare and manipulate data? 20STC Summit 2014-Fraley
  21. 21. XSLT: Background • DSSSL: The XSL (XSLT and XSL-FO) of SGML • FOSI and CSS: Style for print and the web • ASP, JSP, PHP, etc.: Markup template languages • Functional Programming: Simple, efficient and… well, not obvious • Syntax Compromise: Mostly XML with limited custom syntax mostly just XPath) for conciseness and simplicity What were XSLT’s influences and main ideas? 21STC Summit 2014-Fraley
  22. 22. XSLT: Stylesheet • XSLT instructions are elements in the http://www.w3.org/1999/XSL/Transform namespace usually with the “xsl” prefix • An <xsl:stylesheet> primarily consists of <xsl:template>s that tell the processor what to do with each part of the document • Output format is controlled by an <xsl:output/> instruction How do I start my stylesheet? 22STC Summit 2014-Fraley
  23. 23. XSLT: Templates • The processor starts by looking for a template for the document’s Root node • Most templates have a “match” attribute containing a pattern, which is a restricted type of XPath expression that matches “right to left” – it must select the given node when evaluated in the context of the node itself or an ancestor • In the case of multiple matching templates, XSLT provides rules for precedence (when <xsl:import/> is used) and priority to resolve the conflict How does XSLT process my document? 23STC Summit 2014-Fraley
  24. 24. XSLT Templates (Cont) • A template may also (or instead) be named with a “name” attribute so it can be called with <xsl:call-template> • A template may have a “mode” attribute to allow alternate processing (i.e., document body vs. table of contents) • A template may have <xsl:param> instructions to allow additional information to be passed when the template is invoked • Each template can recursively invoke additional templates • Built-in templates exist that just output all text, no structure. 24STC Summit 2014-Fraley
  25. 25. XSLT: Literal Result Elements • Any element inside a template that is not in the XSLT (or extension) namespace will be copied to the result of the stylesheet • Attributes of such elements (as well as certain attributes of XSLT instructions) are interpreted as Attribute Value Templates (AVTs) • In AVTs, an XPath expression surrounded by {curly braces} is replaced with the result of the expression, converted to a string How do I generate output? 25STC Summit 2014-Fraley
  26. 26. XSLT: Not-So-Literal Results • <xsl:element> allows creation of elements whose name is determined at run time • <xsl:attribute> allows conditional creation of attributes whose name is determined at run time • <xsl:text> provides slightly better control over whitespace in the result • The <xsl:processing-instruction> and <xsl:comment> instructions provide the only way to generate PIs and comments, other than copying them from the input How else can I create output? 26STC Summit 2014-Fraley
  27. 27. XSLT: Copying and Calculating • <xsl:copy> copies the current node to the result, with its attributes and content provided by the contents of the instruction • <xsl:copy-of> copies a specific node-set, result tree fragment, or string (numbers and booleans are converted to strings) into the result • <xsl:value-of> works like <xsl:copy-of>, but converts the value to a string first • <xsl:variable> stores the result of an XPath expression (select attribute) or a result tree fragment (instruction content) in a named variable for later use How do I output part of my document or the result of an XPath expression? 27STC Summit 2014-Fraley
  28. 28. XSLT: Conditionals • <xsl:>choose contains one or more <xsl:when> instructions and an optional <xsl:otherwise> instruction • The first <xsl:when> whose “test” attribute contains an XPath expression that evaluates to true will have its output included in the result; the <xsl:otherwise> instruction’s content will be included if no “test” expression is true • <xsl:if> is a shortcut for <xsl:choose> with just a single <xsl:when> How do I conditionally generate input 28STC Summit 2014-Fraley
  29. 29. XSLT: Repetition • <xsl:for-each> includes its contents in the result once for each node selected by the expression in its “select” attribute • <xsl:apply-templates> invokes the best match template for each node selected by the expression in its “select” attribute, optionally specifying the “mode” of templates to consider • <xsl:with-param> may be used to pass values for the target template’s parameters, either via the “select” attribute or instruction content, similar to an <xsl:variable> • When to use <xsl:for-each> vs. <xsl:apply-templates>? • One or more <xsl:sirt> instructions can be included as the initial child of an <xsl:for-each> or <xsl:apply-templates> instruction to provide a specific ordering for the selected nodes How do I repeat parts of my stylesheet? 29STC Summit 2014-Fraley
  30. 30. XSLT: Delegation • <xsl:call-template> invokes a named template • <xsl:with-param> may also be used here • Named templates can be recursive, providing the most powerful means to accomplish things that XSLT doesn’t provide direct suport for How can I organize my stylesheet and do more complex processing? 30STC Summit 2014-Fraley
  31. 31. XSLT: Miscellaneous • <xsl:number> can generate various styles of numbering based on the structure of the input document • <xsl:decimal-format> provides a way to specify exactly how numbers should be formatted • <xsl:key> provides a more powerful and flexible alternative to XML’s built-in ID mechanism • <xsl:message> is used to provide feedback during stylesheet processing, or even abort processing under certain conditions • <xsl:include> and <xsl:import> are used to organize a stylesheet into modules What are the rest of the instructions for? 31STC Summit 2014
  32. 32. XSLT: Miscellaneous (Cont) • <xsl:apply-imports> is used to combine templates into multiple stylesheet modules • <xsl:attribute-set> is used with the “use-attribute-sets” attribute on several isnructions to reuse attribute specifications • <xsl:transform> is a synonym for <xsl:stylesheet> • <xsl:fallback> provides alternative processing for cases where a certain feature is not provided by the XSLT processor • <xsl:strip-space> and <xsl:preserve-space> are used to control whitespace in the input • <xsl:namespace-alias> is for advanced namespace control 32STC Summit 2014
  33. 33. Now What? • Quick ref: http://www.mulberrytech.com/quickref/index.html • Summary: – http://zvon.org/xxl/XPathTutorial/General/examples.html – http://zvon.org/xxl/XSLTreference/Output/ • XSL FAQ: http://www.dpawson.co.uk.xsl • DocBook Stylesheets: http://www.docbook.sourceforge.net/ • XSL-List: http://www.mulberrytech.com/xsl/xsl-list/index.html • Specifications – XPath: http://www.w3.org/TR/xpath – XSLT: http://www.w3.org/TR/xslt • G Ken Holman, Crane Softwrights: ebooks (perpetual updates included free), links to training videos, and upcoming class schedule: – http://www.cranesoftwrights.com/training/index.htm Where can I get help and learn more? 33STC Summit 2014-Fraley
  34. 34. Contact us! Phone 408-660-3219 E-Mail liz.fraley@single-sourcing.com Or visit us on the web at www.single-sourcing.com Need an XSL/XPath mentor?
  35. 35. o Over a decade of experience in dynamic product information creation, publication, and delivery. o Experts for every phase of the single-source development process. o Deep community relationships to ensure your success. o Unique customer focus with concierge level service. o Advisory Line o Blogs o Podcasts o Code repositories Single-Sourcing Solutions http://single-sourcing.com/

×