1 / 19

XSLT

XSLT. part of XSL (Extensible Stylesheet Language) includes also XPath and XSL Formatting Objects used to transform an XML document into: another XML document (or a part of it) HTML document or any other text document (although not designed to do so)

Télécharger la présentation

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. XSLT part of XSL (Extensible Stylesheet Language) includes also XPath and XSL Formatting Objects used to transform an XML document into: another XML document (or a part of it) HTML document or any other text document (although not designed to do so) An XSLT processor takes an XSLT stylesheet to convert aninput document into the output document

  2. XSLT stylesheet consists of template rules (xsl:template elements) each rule describes: a pattern to match (e.g. element name) set of parameters (optionally) a sequence constructor: data (template) to output, typically some markup some data copied from source document some new data

  3. <?xml version="1.0"?> <Students> <Personid="12998800"> <FirstName>Peter</FirstName> <LastName>Kováč</LastName> <BirthDate>1980-10-25</BirthDate> <Study> <Programid="1.MAT"></Program> <Facultyid="1070">FMFI UK</Faculty> </Study> <Study> <Program id="1.AE"></Program> <Faculty id="1050">FiF UK</Faculty> </Study> </Person> <Personid="88332212"> <FirstName>Karol</FirstName> <LastName>Nový</LastName> <BirthDate>1982-11-16</BirthDate> <Study> <Program id="1.INF"></Program> <Faculty id="1070">FMFI UK</Faculty> </Study> </Person> </Students> <?xml version="1.0"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <ListOfStudents> <xsl:apply-templates/> </ListOfStudents> </xsl:template> <xsl:template match="Person"> <Student> <xsl:value-of select="FirstName"/>, <xsl:value-of select="LastName"/>, <xsl:value-of select="Study[1]/Faculty"/> (<xsl:value-of select="Study[1]/Faculty/@id"/>) </Student> </xsl:template> </xsl:stylesheet> <?xml version="1.0"?> <ListOfStudents> <?xml version="1.0"?> <ListOfStudents> <Student>Peter, Kováč, FMFI UK (1070)</Student> <Student>Karol, Nový, FMFI UK (1070)</Student> </ListOfStudents>

  4. Documents modeled as trees with following kinds of nodes: the root (/) element attribute text namespace processing instruction comment

  5. Basic algorithm XSLT processor accepts an input tree and produces an output tree Transformation is done by evaluating the sequence constructor of the initial template. initial template: if not set explicitly, it is template rule selected for processing initial context node (default: root node) other templates can be “called” from here Default templates: for elements: call apply-templates on all child elements for text and attributes: copy string value of node

  6. Patterns – examples root node: / (relative) element name: e.g. Student, Person, FirstName, Study (relative) path: e.g. Person/Study, Person/Study/Faculty element at any level: e.g. Person//Faculty, //Study attribute: e.g. @id, Study/Faculty/@id wildcard: *, cdo:*, Person/*/Faculty, //*/@* “or” operator: FirstName|Lastname, *|@* test with [XPath expression]: Person[FirstName=‘Peter’], Faculty[@id=1070], Study[position()=last()], Person[Study/Faculty/@id=1050], Person[Study]

  7. Sequence constructors Sequence constructor can consist of text nodes literal result elements XSLT instructions extension instructions

  8. XSLT instructions creating new nodes xsl:document, xsl:element, xsl:attribute, xsl:text, xsl:value-of, ... conditional or repeated execution xsl:if, xsl:choose, xsl:for-each, ... invoking other templates xsl:apply-templates, xsl:next-match, xsl:call-template, ... declaring variables xsl:variable, xsl:param other

  9. Creating new nodes <an-elementan-attr=“a-value”> a-text </an-element> <an-element an-attr=“{expression}”> a-text </an-element> <xsl:elementname=“an-element”> ... </xsl:element> <xsl:attributename=“an-attr”>a-value</xsl:attribute> <xsl:attributename=“an-attr” select=“expression”/> a-text <xsl:text>a-text</xsl:text>

  10. Conditional and repeated execution <xsl:iftest=“...”> ... </xsl:if> <xsl:choose> <xsl:whentest=“...”> ... </xsl:when> <xsl:when test=“...”> ... </xsl:when> <xsl:otherwise> ... </xsl:otherwise> </xsl:choose> <xsl:for-eachselect=“...”> ... </xsl:for-each>

  11. Invoking other templates <xsl:apply-templates select=“...”/> finds a template for each node in the input sequence and invokes it default: all child nodes (elements, PI’s, comments, text – not attributes!) can be nodes that are not descendants

  12. Variables definition (global/local): <xsl:variablename=“...” select=“...”/> <xsl:variablename=“...”>...</xsl:variable> use: $variable-name e.g. <xsl:value-of select=“$var”/>

  13. Parameters declaration (at the level of stylesheet, template, or function): <xsl:paramname=“...” required=“yes|no” select=“...”/> use like variables ($xxx) When invoking templates: <xsl:apply-templates ...> <xsl:with-paramname=“...” select=“...”/> </xsl:apply-templates>

  14. XPath 2.0 • all values = sequences of • nodes, or • simple values, e.g. int, string, boolean, date • single value = sequence of length 1 • empty sequence: () • single value: „a“ or („a“) • general sequence: („a“, „b“, „c“) from www.saxonica.com

  15. Constants • string literals • "This is a test.“ • "This is a ‘test'." • numeric constants • 14 (integer) • 385.032 (fixed point decimal) • 3.2e-7 (double precision floating point number) • no boolean constants • use true(), false() functions • other • xs:dateTime("1966-07-31T15:00:00Z") • xs:float("10.7") • ...

  16. Various • variables: $name • function calls: fnc(arg1, arg2, ...) • comparisons / tests / conditionals: • for atomic values: eq, ne, lt, le, gt, ge • for sequences: =, !=, <, <=, >, >=: true if any pair has that relationship! • is (testing node identity) • deep-equal (node-sequence-1, node-sequence-2) • <<, >> (testing node precedentness in document order) • instance of, castable as • if (E1) then E2 else E3 • some/every $x in E1 satisfies E2

  17. Accessing documents • axis :: node-test • axes available: • child(default), attribute (abbrev. @), ancestor, ancestor-or-self, descendant, descendant-or-self, preceding/following, preceding/following-sibling, parent, self • node test - examples: • node name • prefix:*, *:localname • text(), node(), comment(), element(), element(Name), attribute(), attribute(Name)

  18. Accessing documents (2) • E1 / E2 / E3 means: • first we have an input sequence (of nodes), S1 • then we apply expression E1 on each of them, resulting in S2 • then we apply E2 on each of them, resulting in S3 • then we apply E3 on each of them, resulting in S4 • e.g. • /Students/Person[@id=‘12998800’]/Study = /child::Students/child::Person[attribute::id=‘12998800’]/child::Study • results in two elements “Study” • /Students/Person/Study (three elements “Study”) • /Students/Person[@id=‘12998800’]/FirstName/text() • results in text node “Peter” • /Students/Person[@id=‘12998800’]/FirstName/string() • results in string “Peter”

  19. <?xml version="1.0"?> <Students> <Personid="12998800"> <FirstName>Peter</FirstName> <LastName>Kováč</LastName> <BirthDate>1980-10-25</BirthDate> <Study> <Programid="1.MAT"></Program> <Facultyid="1070">FMFI UK</Faculty> </Study> <Study> <Program id="1.AE"></Program> <Faculty id="1050">FiF UK</Faculty> </Study> </Person> <Personid="88332212"> <FirstName>Karol</FirstName> <LastName>Nový</LastName> <BirthDate>1982-11-16</BirthDate> <Study> <Program id="1.INF"></Program> <Faculty id="1070">FMFI UK</Faculty> </Study> </Person> </Students>

More Related