1 / 69

Introduction to XSLT

Orion Ifland / 2008-11-04. Introduction to XSLT. Introduction to XSLT. Agenda The X* Ecosystem (XSL, XPath, XQuery...) XML Refresh/Terminology XPath: Addresses for XML documents XSLT Basics: template and value-of XSLT Loops: apply-templates and for-each

victoria
Télécharger la présentation

Introduction to XSLT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Orion Ifland / 2008-11-04 Introduction to XSLT

  2. Introduction to XSLT • Agenda The X* Ecosystem (XSL, XPath, XQuery...) XML Refresh/Terminology XPath: Addresses for XML documents XSLT Basics: template and value-of XSLT Loops: apply-templates and for-each XSLT Decisions: if and choose XSLT Variables: variable and param XSLT Extras: sort, copy-of, document… Why/When XSLT?

  3. The X* Ecosystem • XSL(T): Extensible Stylesheet Language (Transforms)‏ • Like a giant function with a domain of XML and a range of XML, HTML and Text • XSL-FO (Formatting Objects) is a related, graphics/print-oriented language • XPath: XML Path (addressing)‏ • Like a filesystem path in an XML document • Also like RegEx

  4. The X* Ecosystem • XQuery / XPath 2.0 / XSLT 2.0 • Kind of jumbled together • XQuery looks more like a mix of XSLT, SQL, and Javascript (including lots of XPath)‏ • XMLNS (XML Namespaces)‏ • Like programming language namespaces, distinguishes between elements/attributes with the same name • Used by XSLT and many other XML dialects

  5. XML Refresh/Terminology • XML Nodes • Processing instruction: <?pi ... ?> • Element: <element /> or <element></element> • Attribute: <element attribute="value" /> • Comment: <!-- comment --> • Entity: &amp; • Text node (just plain text)

  6. XML Refresh/Terminology • XML Syntax Rules • Escape < > & (turn them into entities &lt; &gt; &amp;) • Every document has exactly one root element • Attribute values are always quoted • Elements are case-sensitive (and are typically lowercase) • You don't have to put an xml processing instruction at the top of your document

  7. XML Namespaces • Reason • Gives context to the meaning of elements • Disambiguates between elements with the same name • Challenges • Easy to forget to specify namespaces • Non-forgiving

  8. XML Namespaces • Syntax • Default namespace, e.g. xmlns="http://www.w3.org/xhtml/1999"<html /> • Prefixed namespace, e.g. xmlns:dc="http://purl.org/dc/elements/1.1/"<dc:title />

  9. XML Namespaces • Example: XHTML • <html xmlns="http://www.w3.org/1999/xhtml"> • <head> • <title></title> • </head> • <body> • <p> </p> • </body> • </html>

  10. XML Namespaces • Example: XSLT • <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns="http://www.w3.org/1999/xhtml"> • <xsl:template match="/*"> • <html> … </html> • </xsl:template> • </xsl:stylesheet>

  11. XPath: Addresses for XML XPath Expressions /library /library/book /library/book/@name /library/book[@name="The Fourth Civilization"] /library/book[1] //book[2] <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  12. XPath: Addresses for XML XPath Expressions /library /library/book /library/book/@name /library/book[@name="The Fourth Civilization"] /library/book[1] //book[2] <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  13. XPath: Addresses for XML XPath Expressions /library /library/book /library/book/@name /library/book[@name="The Fourth Civilization"] /library/book[1] //book[2] <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization"/> </library>

  14. XPath: Addresses for XML XPath Expressions /library /library/book /library/book/@name /library/book[@name="The Fourth Civilization"] /library/book[1] //book[2] <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library> Predicate

  15. XPath: Addresses for XML XPath Expressions /library /library/book /library/book/@name /library/book[@name="The Fourth Civilization"] /library/book[1] //book[2] <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  16. XPath: Addresses for XML XPath Expressions /library /library/book /library/book/@name /library/book[@name="The Fourth Civilization"] /library/book[1] //book[2] <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  17. XPath: Addresses for XML XPath Node selectors /library/* /library/book[1]/text() /library/node()[1] . <library> <!-- comment --> <book>The Principles of Computer Hardware</book> <book name="The Fourth Civilization" /> </library>

  18. XPath: Addresses for XML XPath Node selectors /library/* /library/book[1]/text() /library/node()[1] . <library> <!-- comment --> <book>ThePrinciples of Computer Hardware</book> <book name="The Fourth Civilization" /> </library>

  19. XPath: Addresses for XML XPath Node selectors /library/* /library/book[1]/text() /library/node()[1] . <library> <!-- comment --> <book>The Principles of Computer Hardware</book> <book name="The Fourth Civilization" /> </library>

  20. XPath: Addresses for XML XPath Node selectors /library/* /library/book[1]/text() /library/node()[1] . <library> <!-- comment --> <book>The Principles of Computer Hardware</book> <book name="The Fourth Civilization" /> </library> Selects the "current" node

  21. XPath: Addresses for XML XPath Axes /library/child::book(or /library/book for short) /descendent-or-self::book(or //book for short)‏ //book[1]/parent::*(or //book[1]/.. for short) //book[2]/preceding-sibling::book //book[1]/attribute::name(or //book[1]/@name for short) <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  22. XPath: Addresses for XML XPath Axes /library/child::book(or /library/book for short) /descendent-or-self::book(or //book for short) //book[1]/parent::*(or //book[1]/.. for short) //book[2]/preceding-sibling::book //book[1]/attribute::name(or //book[1]/@name for short) <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  23. XPath: Addresses for XML XPath Axes /library/child::book(or /library/book for short) /descendent-or-self::book(or //book for short) //book[1]/parent::*(or //book[1]/.. for short) //book[2]/preceding-sibling::book //book[1]/attribute::name(or //book[1]/@name for short) <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  24. XPath: Addresses for XML XPath Axes /library/child::book(or /library/book for short) /descendent-or-self::book(or //book for short) //book[1]/parent::*(or //book[1]/.. for short) //book[2]/preceding-sibling::book //book[1]/attribute::name(or //book[1]/@name for short) <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  25. XPath: Addresses for XML XPath Axes /library/child::book(or /library/book for short) /descendent-or-self::book(or //book for short)‏ //book[1]/parent::*(or //book[1]/.. for short) //book[2]/preceding-sibling::book //book[1]/attribute::name(or //book[1]/@name for short) <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  26. XPath: Addresses for XML XPath Functions //book[last()] count(//book) name(/*) //book[contains(@name, "C++")] //book[not(contains(@name, "C++"))] <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  27. XPath: Addresses for XML XPath Functions //book[last()] count(//book) name(/*) //book[contains(@name, "C++")] //book[not(contains(@name, "C++"))] Returns: 2 <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  28. XPath: Addresses for XML XPath Functions //book[last()] count(//book) name(/*) //book[contains(@name, "C++")] //book[not(contains(@name, "C++"))] Returns: "library" <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  29. XPath: Addresses for XML XPath Functions //book[last()] count(//book) name(/*) //book[contains(@name, "C++")] //book[not(contains(@name, "C++"))] <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  30. XPath: Addresses for XML XPath Functions //book[last()] count(//book) name(/*) //book[contains(@name, "C++")] //book[not(contains(@name, "C++"))] <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  31. XPath: Addresses for XML Other Useful XPath Functions position() – provides the position in a list (nice for numbering a sequence of nodes) sum(xpath) – takes a sequence of nodes and adds up their numerical values – see also avg(), min(), max() concat(string, string, …) – exactly what you think string-length(string) – returns the number of characters substring(string, start[, length]) – the first char is at 1 translate(source-string, find-string, replace-string) – looks for individual chars and replaces themExample: translate("ABCD", "BD", "bd")  "AbCd"

  32. XPath: Addresses for XML XPath Operators //book[last() or contains(@name, "C++")] //book[string-length(@name) > 5] //book[1] | //book[2] Other operators: and, =, >, <=, >=, !=, +, -, *, div, mod Don't forget to escape < >! <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  33. XPath: Addresses for XML XPath Operators //book[last() or contains(@name, "C++")] //book[string-length(@name) > 5] //book[1] | //book[2] Other operators: and, =, >, <=, >=, !=, +, -, *, div, mod Don't forget to escape < >! <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  34. XPath: Addresses for XML XPath Operators //book[last() or contains(@name, "C++")] //book[string-length(@name) > 5] //book[1] | //book[2] Other operators: and, =, >, <=, >=, !=, +, -, *, div, mod Don't forget to escape < >! <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  35. XPath: Addresses for XML XPath Operators //book[last() or contains(@name, "C++")] //book[string-length(@name) > 5] //book[1] | //book[2] Other operators: and, =, >, <=, >=, !=, +, -, *, div, mod Don't forget to escape < >! <library> <book name="C++ How to Program" /> <book name="The Fourth Civilization" /> </library>

  36. XPath: Addresses for XML Namespaces xmlns:y="http://x" /y:library /y:library/y:book /y:library/y:book/@name Namespaces must be declared in the XSLT document – prefixes aren't "imported" from the source document <x:library xmlns:x="http://x"> <x:book name="C++ How to Program" /> <x:book name="The Fourth Civilization" /> </x:library>

  37. Try it out Make an XML file Make an XSLT file Run the transform and view the transformed data XSLT Basics <xml /> input <xsl:… /> transform <xml /> (or text) output

  38. Sample XML <person> <name first="Neil" last="Armstrong" /> <quote>...one giant leap for mankind.</quote> </person> Sample Output Neil Armstrong said "...one giant leap for mankind." Sample XSLT <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" /> <xsl:template match="/person"> <xsl:value-of select="concat(name/@first, ' ', name/@last)" /> said " <xsl:value-of select="quote"/>" </xsl:template> </xsl:stylesheet> XSLT Basics

  39. Sample XML <person> <name first="Neil" last="Armstrong" /> <quote>...one giant leap for mankind.</quote> </person> Sample Output Neil Armstrong said "...one giant leap for mankind." Sample XSLT <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" /> <xsl:template match="/person"> <xsl:value-of select="concat(name/@first, ' ', name/@last)" /> said " <xsl:value-of select="quote" />" </xsl:template> </xsl:stylesheet> XSLT Basics

  40. Sample XML <person> <name first="Neil" last="Armstrong" /> <quote>...one giant leap for mankind.</quote> </person> Sample Output Neil Armstrong said "...one giant leap for mankind." Sample XSLT <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" /> <xsl:template match="/person"> <xsl:value-of select="concat(name/@first, ' ', name/@last)" /> said " <xsl:value-of select="quote"/>" </xsl:template> </xsl:stylesheet> XSLT Basics

  41. Sample XML <person> <name first="Neil" last="Armstrong" /> <quote>...one giant leap for mankind. </quote> </person> Sample Output <html><head><title>Neil Armstrong</title></head> <body><blockquote>…one giant leap for mankind.</blockquote> </body></html> Sample XSLT  HTML <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" /> <xsl:template match="/person"> <html><head><title> <xsl:value-of select="concat(name/@first, ' ', name/@last)" /> </title></head><body> <blockquote><xsl:value-of select="quote"/></blockquote> </body></html> </xsl:template> </xsl:stylesheet> XSLT Basics All well-formed

  42. Sample XML <person> <name first="Neil" last="Armstrong" /> <quote>...one giant leap for mankind. </quote> </person> Sample Output <html><head><title>Neil Armstrong</title></head> <body><blockquote>…one giant leap for mankind.</blockquote> </body></html> Sample XSLT  HTML <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" /> <xsl:template match="/person"> <html><head><title> <xsl:value-of select="concat(name/@first, ' ', name/@last)" /> </title></head><body> <blockquote><xsl:value-of select="quote"/></blockquote> </body></html> </xsl:template> </xsl:stylesheet> XSLT Basics

  43. Sample XML <person> <name first="Neil" last="Armstrong" /> <quote>...one giant leap for mankind. </quote> </person> Sample Output <quote><speaker firstname="Neil" lastname="Armstrong"/> <text>…one giant leap for mankind.</text> </quote> Sample XSLT  XML <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" /> <xsl:template match="/person"> <quote><speaker firstname="{name/@first}" lastname="{name/@last}"/> <text><xsl:value-of select="quote"/></text> </quote> </xsl:template> </xsl:stylesheet> XSLT Basics

  44. Sample XML <person> <name first="Neil" last="Armstrong" /> <quote>...one giant leap for mankind. </quote> </person> Sample Output <quote><speaker firstname="Neil" lastname="Armstrong"/> <text>…one giant leap for mankind.</text> </quote> Sample XSLT  XML <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" /> <xsl:template match="/person"> <quote><speaker firstname="{name/@first}" lastname="{name/@last}"/> <text><xsl:value-of select="quote"/></text> </quote> </xsl:template> </xsl:stylesheet> XSLT Basics

  45. Sample XML <person> <name first="Neil" last="Armstrong" /> <quote>...one giant leap for mankind. </quote> </person> Sample Output <quote><speaker firstname="Neil" lastname="Armstrong"/> <text>…one giant leap for mankind.</text> </quote> Sample XSLT  XML <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" /> <xsl:template match="/person"> <quote><speaker firstname="{name/@first}" lastname="{name/@last}"/> <text><xsl:value-of select="quote"/></text> </quote> </xsl:template> </xsl:stylesheet> XSLT Basics

  46. Sample XML <zoo> <birds> <albatross pop="4" /> <buzzard pop="2" /> <chickadee pop="12" /> </birds> <mammals> <aardvark pop="5" /> <bat pop="200" /> <cheetah pop="2" /> </mammals> </zoo> Sample XSLT  HTML <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" /> <xsl:template match="/zoo"> <html><head><title>Zoo</title></head><body> <xsl:for-each select="*"> <h1> <xsl:value-of select="name(.)" /> </h1> </xsl:for-each> </body></html> </xsl:template> </xsl:stylesheet> XSLT Loops: for-each

  47. Sample XML <zoo> <birds> <albatross pop="4" /> <buzzard pop="2" /> <chickadee pop="12" /> </birds> <mammals> <aardvark pop="5" /> <bat pop="200" /> <cheetah pop="2" /> </mammals> </zoo> Result HTML <html><head><title>Zoo</title></head><body> <h1> birds </h1> <h1> mammals </h1> </body></html> XSLT Loops: for-each

  48. Sample XML <zoo> <birds> <albatross pop="4" /> <buzzard pop="2" /> <chickadee pop="12" /> </birds> <mammals> <aardvark pop="5" /> <bat pop="200" /> <cheetah pop="2" /> </mammals> </zoo> Sample XSLT  HTML <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" /> <xsl:template match="/zoo"> <html><head><title>Zoo</title></head><body> <xsl:for-each select="*"> <h1><xsl:value-of select="name(.)" /></h1> <ul><xsl:for-each select="*"> <li><xsl:value-of select="name(.)" /> (<xsl:value-of select="@pop">)</li> </xsl:for-each></ul> </xsl:for-each> </body></html> </xsl:template> </xsl:stylesheet> XSLT Loops: for-each

  49. Sample XML <zoo> <birds> <albatross pop="4" /> <buzzard pop="2" /> <chickadee pop="12" /> </birds> <mammals> <aardvark pop="5" /> <bat pop="200" /> <cheetah pop="2" /> </mammals> </zoo> Result HTML <html><head><title>Zoo</title></head><body> <h1>birds</h1> <ul> <li>albatross (4)</li> … </ul> <h1>mammals</h1> <ul> <li>aardvark (5)</li> … </ul> </body></html> XSLT Loops: for-each

  50. <xsl:template match="birds | mammals"> <h1><xsl:value-of select="name(.)" /></h1> <ul> <xsl:for-each select="*"> <li><xsl:value-of select="name(.)" /> (<xsl:value-of select="@pop"/>)</li> </xsl:for-each> </ul> </xsl:template> </xsl:stylesheet> XSLT Loops: apply-templates Sample XSLT <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" /> <xsl:template match="/zoo"> <html> <head><title>Zoo</title></head> <body> <xsl:apply-templates select="*" /> </body> </html> </xsl:template>

More Related