1 / 36

What’s New in XSLT 2.0

What’s New in XSLT 2.0. 임형준 ( hyungjun25@cnu.ac.kr ) 충남대학교 컴퓨터공학과 데이터베이스 시스템 연구실 2014년 9월 2일 화요일. Contents. XSLT Status XSLT Overview New Feature of XSLT 2.0 Schema Support Grouping Functions Types Regular Expressions Temporary Trees Sequences Character Maps Conclusion References.

ozzy
Download Presentation

What’s New in XSLT 2.0

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. What’s New in XSLT 2.0 임형준(hyungjun25@cnu.ac.kr) 충남대학교 컴퓨터공학과 데이터베이스 시스템 연구실 2014년 9월 2일 화요일

  2. Contents • XSLT Status • XSLT Overview • New Feature of XSLT 2.0 • Schema Support • Grouping • Functions • Types • Regular Expressions • Temporary Trees • Sequences • Character Maps • Conclusion • References

  3. XSLT Status • XSLT 1.0 • XSL Transformations (XSLT) Version 1.0 • W3C, http://www.w3.org/TR/1999/REC-xslt-19991116 • Published W3C Recommendation : 1999-11-16 • XSLT 2.0 • XSL Transformations (XSLT) Version 2.0 • W3C, http://www.w3.org/TR/2007/REC-xslt20-20070123/ • Published W3C Recommendation : 2007-01-23

  4. XSLT Overview • A language for transforming XML documents • The results can be: • Same XML document type with filtered data • Another XML document type • Other text formats • HTML • WML • Delimited text, etc. • XSL contains the languages: • XPath: expressions for selecting elements in a document • XSLT (XSL Transformations) • Other languages e.g. XLink, Xpointer

  5. Features of XPath • Identifies particular parts in XML documents • Expressions along the tree structure of XML documents • Examples: • child::chapter • Selects all subordinated elements of chapter • child::*[self::chapter] • Selects all chapter elements of the context element • id(´a23´)/child::chapter[position()=5] • Selects the fifth child-element with the name chapter of the element with the unified ID a23

  6. Features of XSLT • Tree-oriented transformation language based on XML • Uses XPath expressions for: • Conditional parsing: if / while / test / ... • Selecting parts of the document: match / select / value-of / template / ... • Manipulation of strings and numbers • Vocabulary for specifying formating: e.g. sorting • Variable output formats: e.g. xml, html, plain text, ...

  7. XSLTransformation Input Output XML Document XML Document XSLT (eXtensible Stylesheet Language Transformations) HTML Converting Rules: Stylesheet(XSL) WML Plain Text XPath …

  8. Example of a XSLT <?xml version="1.0" ?> <!-- hello.xml --> <?xml:stylesheet type="text/xsl" href="hello.xsl"?> <myMessage> <message>Hello XSLT!</message> </myMessage> <html> <body> <h1>Hello XSLT!</h1> </body> </html> <?xml version="1.0" ?> <!-- hello.xsl --> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="myMessage"> <html><body> <h1><xsl:value-of select="message"/></h1> </body></html> </xsl:template> </xsl:stylesheet>

  9. New Feature of XSTL 2.0 Schema Support Grouping Functions Types Regular Expressions Temporary Trees Sequences Character Maps

  10. Schema Support • Need to import schemas to use: • user-defined types • substitution groups • Import with <xsl:import-schema> • namespace identifies target namespace • schema-location locates schema • In order to refer to additional types, you must import the schema that defines them: • Must specify at least the namespace or the schema location, if not both <xsl:import-schema namespace="http://nwalsh.com/xmlns/extreme2004/recipes/" schema-location="recipes.xsd"/>

  11. Implications for XSLT 2.0 Use • Well-designed schemas become a useful tool • Substitution groups and appropriate types help reduce number of templates • Check whether the result conforms to the schema while generating it

  12. Grouping • XSLT 1.0 allowed sorting, but not SQL-style grouping • XSLT 2.0 provides a new, flexible grouping instruction for grouping • <xsl:for-each-group /> iterates through a set of groups. Attributes: • select: XPath expression for which elements to select • group-by: Node by which to group the results • group-adjacent: Leaves elements in their original order and groups elements with the same key value together • group-starting-with: Specifies a node that will be treated as the beginning of a new group • group-ending-with: Specifies a node that will be treated as the end of the group

  13. Grouping • Functions to know: • current-grouping-key() returns the current value of the grouping key shared by members of the group • current-group() returns a reference to the current group • Grouping by key(Example) <xsl:for-each-group select="cities/city" group-by="@country"> <tr> <td><xsl:value-of select="position()"/></td> <td><xsl:value-of select="@country"/></td> <td> <xsl:value-of select="current-group()/@name" separator=", "/> </td> </tr> </xsl:for-each-group> <cities> <city name="Milano" country="Italia"/> <city name="Paris" country="France"/> <city name="Munchen" country="Deutschland"/> <city name="Lyon" country="France"/> <city name="Venezia" country="Italia"/> </cities> <tr> <td>1</td> <td>Italia</td> <td>Milano, Venezia</td> </tr> <tr> <td>2</td> <td>France</td> <td>Paris, Lyon</td> </tr>

  14. Implications for XSLT 2.0 Use • Much easier to convert from flat to hierarchical structures • Processing XHTML to DocBook (or XHTML2.0) • Easier to create indexes • Easier to create summaries/roll-ups • Easier to create paginated documents

  15. Functions • XSLT is really a programming language, but in version 1.0 lacked the ability to define new functions • XSLT 2.0 rectifies this: • You can define new functions • These functions can take 0 or more parameters • These functions can return a value

  16. Functions • <xsl:function>…</xsl:function> wraps the function. Attributes: • name: The name of the function • as: Data type returned by the function (as an XML Schema data type) • override: Whether this function should override previous definitions with the same name and arity (number of xsl:param elements in the function definition) • Content of the function: • 0 or more <xsl:param /> elements that define incoming parameters • Code to execute – final value can be returned via <xsl:value-of />, <xsl:sequence />, or another expression

  17. Function Example <xsl:function name="str:reverse" as="xs:string"> <xsl:param name="sentence" as="xs:string"/> <xsl:sequence select=“ if (contains($sentence, ' ')) then concat(str:reverse( substring-after($sentence, ' ')), ' ', substring-before($sentence, ' ')) else $sentence"/> </xsl:function> <xsl:function name="str:align"> <xsl:param name="string" /> <xsl:sequence select="str:reverse(‘Database System')" /> </xsl:function>

  18. Implications for XSLT 2.0 Use • General replacement for named templates • Particular use where XSLT code can't be used: • creating a value to sort by using <xsl:sort> • creating a value to index by using <xsl:key> • creating a value to group by using <xsl:for­each-group> • carrying out complex tests on nodes, for use in match patterns in templates or keys <xsl:template match=“\S.*"> ... </xsl:template>

  19. Types • Strong Typing in XSLT 2.0 • The type of a sort key determines how values are sorted • If the wrong type of value is passed as a parameter to a template, you will get an error • XSLT 2.0 allows you to declare: • The type of variables • The return type of templates • The type of sequences (constructed with xsl:sequence) • The return type of (user-declared) functions • Both the type and required type of parameters

  20. Declaring Types of Variables • Declare the type of variables and parameters with as attribute <xsl:variable name="i" select="1"/> is the integer value 1 <xsl:variable name="fp" select="1“ as="xs:double"/> is the double value 1.0 <xsl:variable name="date” select="'2003-11-20'"> is the string “2003-11-20” <xsl:variable name="date” select="xs:date('2003-11-20')"/> is the date November 20, 2003 <xsl:variable name="date" as="xs:date” select="'2003-11-20'"/> is an error

  21. Implications for XSLT 2.0 Use • Easy to get errors from a stylesheet unless you're rigorous in keeping track of types • Declare types of variables and parameters • Cast elements/attributes to particular types

  22. Regular Expressions • Within XSLT 2.0, there are three key functions that use regular expressions: • Tokenize(string, regex, flags?):Lets you split a string into pieces at each point where a delimiter or pattern occurs • matches(string, regex, flags?): Returns a boolean with whether the string in the first argument contains the pattern in the second argument • Replace(string, regex, replacement, flags?): Replaces the string or pattern in the first argument with the string in the second argument

  23. Regular Expression Instructions • XSLT 2.0 has <xsl:analyze-string> instruction • Uses regular expressions to apply markup to a string • select attribute selects string • regex The pattern to which the string is compared • string split into a sequence of matching and non-matching substrings • processed in turn by: • <xsl:matching-substring> for matching • <xsl:non-matching-substring> for non-matching

  24. Example String Analysis <poem> Mary had a little lamb, Its fleece was white as snow; And everywhere that Mary went The lamb was sure to go. </poem> <xsl:template match="poem"> <poem> <xsl:analyze-string select="." regex="\S.*" flags="m"> <xsl:matching-substring> <line><xsl:value-of select="." /></line> </xsl:matching-substring> </xsl:analyze-string> </poem> </xsl:template> <poem> <line>Mary had a little lamb,</line> <line>Its fleece was white as snow;</line> <line>And everywhere that Mary went</line> <line>The lamb was sure to go.</line> </poem>

  25. Implications for XSLT 2.0 Use • Potential to process any text format • comma-delimited and fixed-format files • CSS files • C#, Java code? • These are hard because of matching tags/braces • Addition of regular expressions and XSD type support, it now becomes possible for an XSLT 2.0 transformer to read something like a Java or C# header • Possible to parse non-comformant HTML files and convert them into valid XHTML.

  26. Transclusion • Transclusion is the act of dynamically including data from another resource into the current resource • Sample code using the document() function: <chapter xsl:version="2.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <title>doc1.xml</title> <p>First paragraph</p> <xsl:copy-of select="document('doc2.xml')/doc/p[@id='p2']"/> <p>Last paragraph</p> </chapter>

  27. Temporary Trees • XSLT 1.0 had Result Tree Fragments • Result trees are read-only. • Created when use content of <xsl:variable> • Tree that couldn't be accessed with location path • Most processors have xxx:node-set() extension function • Convert result tree fragment to node tree • In XSLT 2.0, have temporary trees • Can copy in same way as RTFs • Can access without using extension function • Provides a <xsl:result-document> instruction to create multiple result documents • Provides support for validation

  28. Example Temporary Tress <xsl:variable name="menus"> <menu name="File"> <menuItem name="New..." shortcut="Ctrl-N" /> <menuItem name="Open..." shortcut="Ctrl-O" /> <menuItem name="Save..." shortcut="Ctrl-S" /> ... </menu> ... </xsl:variable> XSLT 1.0 <xsl:value-of select="exsl:node-set($menus)/menu /menuItem[@shortcut = $shortcut]/@name" /> XSLT 2.0 <xsl:value-of select="$menus/menu /menuItem[@shortcut = $shortcut]/@name" />

  29. Implications for XSLT 2.0 Use • Break up complex transformations into several steps • Filter, sort, annotate nodes in early steps • Later steps are easier to write • Create lookup tables • Translate from codes or numbers to labels • Similar to arrays or matrices • Iteratively process a document until it fulfils some test • Add content until a document is valid

  30. Sequences • Every sequence of instructions creates a sequence of items • When a sequence is added to a node: • Atomic values are converted to text nodes • Spaces added between atomic values • Nodes are copied to create children sequence of any items sequence of new nodes children of node

  31. Sequences • The <xsl:sequence> instruction is used to construct sequences of nodes or atomic values (or both) • Adds existing nodes or new atomic values to a sequence • <xsl:sequence select='(1,2,3,4)'/> • returns a sequence of integers. • <xsl:sequence select='(1,2,3,4)‘ as="xs:double"/> • returns a sequence of doubles.

  32. Using Sequences in XSLT 2.0 • Iterate over a sequence • Create a text node from a sequence <xsl:for-each select="1 to 3"> <tr><td colspan="4" /></tr> </xsl:for-each> <tr><td colspan="4" /></tr> <tr><td colspan="4" /></tr> <tr><td colspan="4" /></tr> <xsl:value-of select="author/surname" separator=", " /> Thompson, Tobin

  33. Implications for XSLT 2.0 Use • Less need for recursive templates • Use integer sequences to iterate a number of times • Use <xsl:sequence> to build node sequences by iteration rather than recursion • Less need for temporary elements • Atomic values don't need to be wrapped in an element in order to be passed around in a list

  34. Character Maps • Character maps give you greater control over serialization. They map a Unicode character to any string in the serialized document • For XML and HTML output methods, the resulting character stream does not have to be well-formed • The mapping occurs only at serialization: it is not present in result tree fragments • This facility can be used instead of “disabled output escaping” in most cases

  35. Conclusion • XSLT 2.0 introduces a lot of new features • Many stylesheets can be simpler: • multi-step processing with temporary trees • Grouping using <xsl:for-each-group> • user-defined functions for repetitive code • Stylesheet applications can be simpler: • Multiple result documents should reduce need for client-side scripting • XSLT 2.0 expands into text parsing • Using schemas/types well will make things easier; not using them will make things harder

  36. References • W3C, “XSL Transformations (XSLT)Version 1.0” http://www.w3.org/TR/1999/REC-xslt-19991116, 16, November. 1999 • W3C, “XSL Transformations (XSLT)Version 2.0” http://www.w3.org/TR/2007/REC-xslt20-20070123/, 12, January. 2007 • Steve Heckler, XSLT20.ppt, Accelebrate • Kurt Cagle, “Reevaluating XSLT 2.0”, 9, March. 2007

More Related