Presentation is loading. Please wait.

Presentation is loading. Please wait.

XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert.

Similar presentations


Presentation on theme: "XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert."— Presentation transcript:

1 XSLT Aug ’10 – Dec ‘10

2 Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert XML to some other output  Often output is XML or HTML  XSLT uses XPath to select parts of the source XML document that are used in the result document  An XSLT file consists of a number of templates that match specific nodes in the XML being processed.

3 Need for XSLT   XSLT is a very important XML application in many XML workflows   XSLT is important because the way in which XML is stored needs to be changed before it is used   XML might need to be presented to end users in a convenient format   XSLT plays a key role in converting XML to its presentation formats and restructuring XML Aug’10 – Dec ’10 2

4 Uses of XSLT Aug’10 – Dec ’10 3  Restructuring XML  Restructuring is useful when two companies need to exchange XML documents electronically but have differences in the structure of basic documents  XSLT can copy selected parts of the source XML unchanged into the result document or can create new elements or attributes in the result document.  The names of elements and attributes can be changed.  Elements or attributes present in the source document can be selectively omitted from the result document.  Presenting XML content  XML is often presented as HTML or XHTML  XSLT is used to transform select parts of XML document for display

5 Aug’10 – Dec ’10 4  Accepts an XML document, applies XSLT stylesheet to it and produces another document which can be XML, HTML or plain text  Creates an in-memory tree representation of the source document, according to the XPath data model called the source tree  XSLT processor processes the source tree according to the templates contained in the stylesheet and result tree is created  The creation of result tree from source tree is called transformation  After creating result tree, a process called serialization takes place  Serialization creates a familiar serialized XML document from the result tree XSLT Processor

6 Aug’10 – Dec ’10 5  Procedural Programming  Tell the computer what to do step by step  Define function, assign variable, iterate through loop etc.  Declarative Programming  Tell the computer what to achieve  Resembles SQL  Specify what the XSLT processor is to create when it comes across a pattern in the source tree  XSLT is also a functional language – one that relies on functions to accept and return data Procedural versus Declarative Programming

7 Aug’10 – Dec ’10 6  People.xml Winston Churchill Winston Churchill was a mid 20th century British politician who became famous as Prime Minister during the Second World War. Indira Gandhi Indira Gandhi was India’s first female prime minister and was assassinated in 1984. John F. Kennedy JFK, as he was affectionately known, was a United States president who was assassinated in Dallas, Texas. Foundational XSLT Elements

8 Aug’10 – Dec ’10 7  People.xslt <xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0” > Information about people. Information about people. Foundational XSLT Elements

9 Aug’10 – Dec ’10 8  People.html Information about 3 people. Information about 3 people. Winston Churchill Winston Churchill was a mid 20th Century British politician who became famous as Prime Minister during the Second World War. Indira Gandhi Indira Gandhi was India’s first female prime minister and was assassinated in 1984. John F. Kennedy JFK, as he was affectionately known, was a United States President who was assassinated in Dallas, Texas. Foundational XSLT Elements

10 Aug’10 – Dec ’10 9  Stylesheet, People.xslt, creates a simple HTML web page, People.html, which contains the name and description information about the politicians Foundational XSLT Elements

11 Aug’10 – Dec ’10 10  Every XSLT element has as its document element, either an xsl:stylesheet element or an xsl:transform element  The xsl:stylesheet element is semantically similar to xsl:transform element  The start tag of the xsl:stylesheet element has a mandatory version attribute  xsl:stylesheet element must also have a namespace declaration for the XSLT namespace  The XSLT namespace has the URI http://www.w3.org/1999/XSL/Transform. http://www.w3.org/1999/XSL/Transform The Element

12 Aug’10 – Dec ’10 11  An XSLT processor looks in a stylesheet for an xsl:template element that has a match attribute with value of / Information about people. Information about people. The Element

13 Aug’10 – Dec ’10 12  Every time XSLT processor finds a node in the source tree that is a root node, the structure corresponding to the content of this template is added to the result tree.  There is only one root node in an XPath model, so the nodes are added only once to the result tree.  Many of the elements in the template that match the root node are likely to be HTML/XHTML elements. These elements are added to the result tree literally, and so are called literal result elements.  The template also contains several elements from the XSLT namespace. Those elements are called instructions.  A frequentlyused instruction is the xsl:apply-templates element.  A frequently used instruction is the xsl:apply-templates element. The Element

14 Aug’10 – Dec ’10 13  The xsl:apply-templates element causes the XSLT processor to look for matching nodes in the source tree.  The nodes to be looked for are specified by the XPath location path /People/Person, which specifies Person element nodes that are child nodes of a People element node, which is, in turn, a child node of the root node.  The XSLT processor then looks for a template that matches such a Person element node  Each time the XSLT processor finds a Person element node that corresponds to the location path /People/Person, the content of this template is processed and content is added to the result tree.  Because three such nodes exist, the content specified by the template is added to the result tree three times. The Element

15 Aug’10 – Dec ’10 14 Winston Churchill Winston Churchill was a mid 20th century British politician who became famous as Prime Minister during the Second World War. Indira Gandhi Indira Gandhi was India’s first female prime minister and was assassinated in 1984. John F. Kennedy JFK, as he was affectionately known, was a United States president who was assassinated in Dallas, Texas. The Element

16 Aug’10 – Dec ’10 15  XSLT provides a number of ways to use information from the source tree.  A frequently used XSLT instruction to achieve this is the xsl:value-of element The xsl:value-of element  Provides the value of a part of the source tree that represents the source XML document.  The xsl:value-of element has a mandatory select attribute whose path is an XPath location path Getting information from the source tree

17 Aug’10 – Dec ’10 16 Information about people. in the stylesheet is replaced by Information about 3 people. in the result document The xsl:value-of elements are replaced in the result document by text corresponding, respectively, to the Name element node and the Description element node that are child nodes of the Person element node that matches the value of the match attribute of the xsl:template element. The element

18 Aug’10 – Dec ’10 17  The value of the select attribute is the relative location path Name, which matches a Name element node that is a child node of the context node  The value of the select attribute is the relative location path Name, which matches a Name element node that is a child node of the context node.  When the template that matches the pattern Person is instantiated, the context node is define by the select attribute of the xsl:apply-templates element  The relative location path Name in Could bewritten as the following absolute location path /People/Person/Name The Element

19 Aug’10 – Dec ’10 18  Simplest XSLT element that extracts information from the source tree  It selects the value of a node-set, which might be only a single node specified by the location path that is the value of the select attribute of the xsl:value-of element  If there is more than one node in the node set, then the xsl:value-of element uses the value of the first node in document order only, not the value of all nodes  particularly useful when producing output for presentation  can also be used when XML is being restructured The element

20 Aug’10 – Dec ’10 19  Copies a node to the result tree  It does not copy any descendant nodes  If the context node is element node, it does cause any attribute nodes to be copied  This can be useful when you want to use an element but change the structure of its content or add or remove attributes from it. The element

21 Aug’10 – Dec ’10 20 Persons.xml Jill Harper Claire Vogue Paul Cathedral The element

22 Aug’10 – Dec ’10 21 Persons.xslt <xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0” > The element

23 Aug’10 – Dec ’10 22 The element

24 Aug’10 – Dec ’10 23  xsl:copy element is used when the context node is the Person element node  A node that is same as the context node is added to the result tree  In this example, a Person node is added to the result tree, but its child nodes FirstName and LastName are not copied  If the serialization is done at this point without adding attribute nodes, then the output would look like this: The element

25 Aug’10 – Dec ’10 24  Reversing the process using xsl:copy element  Removes attributes and adds elements The element

26 Aug’10 – Dec ’10 25  Deep copy takes place  A node together with all its attribute nodes and descendant nodes, is copied to the result tree The element PurchaseOrder.xml Example.org XMML.com 234 Any Street Any Town MO 98765

27 Aug’10 – Dec ’10 26 The rest of the Invoice would go here. The element

28 Aug’10 – Dec ’10 27 Output <Invoice><From>XMML.com</From><To>Example.org</To><Address> 234 Any Street 234 Any Street Any Town Any Town <State>MO</State><ZipCode>98765</ZipCode></Address> </Invoice> The element

29 Aug’10 – Dec ’10 28 The rest of the Invoice would go here. The element

30 Aug’10 – Dec ’10 29  XSLT can be used to produce XML, HTML, or text output.  The developer makes a choice among these options by using the method attribute of the xsl:output element.  XML output is the default, and it is not necessary to specify XML as an output method.  To explicitly specify output,    In XSLT 1.0, there is no way to specify XHTML output although this has been added to version 2.0 The element

31 Aug’10 – Dec ’10 30  Tests whether a boolean condition is true or false  If the condition is true, then the content of is instantiated  If it is false, then nothing inside the element is added to the result tree The element Conditional Processing 

32 Aug’10 – Dec ’10 31 Characters.xml Julius Caesar Anne Boleyn George Washington Martin Luther Methuselah Moses Asterix the Gaul  To test whether the age of some characters exceeds the upper age limit, 110 The example

33 Aug’10 – Dec ’10 32 Characters.xslt Age check on Characters. The recorded age is unusually high. Please check original data. is older than expected. Please check if this character’s age,, is correct. The example

34 Aug’10 – Dec ’10 33 Output The example

35 Aug’10 – Dec ’10 34  Similar to, but with a few key differences  One xsl:choose element can test for more than one condition and add different nodes to the result tree based on which condition is true.  An xsl:choose element can have a default template to add to the result tree if none of the conditions are true  The xsl:choose element has specific subelements that are necessary for it to work, while you can put any well-formed elements you want inside of an xsl:if element The element Conditional Processing

36 Aug’10 – Dec ’10 35........................  The xsl:choose element must contain one or more xsl:when elements and can contain only one optional xsl:otherwise element (which must occur after all of the xsl:when elements).  If the xsl:choose element only contains one xsl:when element, then it behaves just like the xsl:if element.  When faced with three or more choices, this element behaves like an if-then-else statement (or a Select Case)  Each xsl:when element is examined in the order of occurrence The element Conditional Processing

37 Aug’10 – Dec ’10 36  If a test expression returns true, the code contained in the element is executed and the element is exited  If the xsl:choose element only contains one xsl:when element, then it behaves just like the xsl:if element  example Seest thou yon dreary Plain, forlorn and wild, The seat of desolation, void of light, Save what the glimmering of these livid flames Casts pale and dreadful? Seest thou yon dreary Plain, forlorn and wild, The seat of desolation, void of light, Save what the glimmering of these livid flames Casts pale and dreadful? </poem> The element Conditional Processing

38 Aug’10 – Dec ’10 37 The element The poem is one of Milton's earlier works. The poem is from Milton's middle period. The poem is one of Milton's later works. The poem is one of Milton's last works. The poem was written after Milton's death.

39 Aug’10 – Dec ’10 38 The element Output The poem is one of Milton's later works.  Although the poem element's year value of "1667" also makes the last xsl:when element's test expression ("@year < 1675") true, the XSLT processor will not continue checking for more true test expressions after it finds one.

40 Aug’10 – Dec ’10 39  Allows all nodes in a node-set to be processed according to the XSLT instructions nested inside the xsl:for-each element  The element establishes the context for iteration.  The XSLT transformation instructions within this loop are to be applied to the selected nodes.  Each source element selected by becomes a new context against which any pattern matching within the occurs. <xsl:for-each select = Expression > </xsl:for-each> The element

41 Aug’10 – Dec ’10 40 Objects.xml <Objects> <Characteristic>Hard</Characteristic><Characteristic>Shiny</Characteristic> Has 4 wheels Has 4 wheels Internal Combustion Engine Internal Combustion Engine </Object></Objects> Example

42 Aug’10 – Dec ’10 41 Object.xslt <html><head> Object Characteristics Object Characteristics </head><body> Characteristics of Characteristics of </body></html></xsl:template> <ul> </xsl:for-each></ul></xsl:template></xsl:stylesheet> Example

43 Aug’10 – Dec ’10 42 Output Example Characteristics of Car Hard Shiny Has 4 wheels Internal Combustion Engine

44 Aug’10 – Dec ’10 43 <customers> John Smith John Smith 123 Oak St. 123 Oak St. WA WA (206) 123-4567 (206) 123-4567 Zack Zwyker Zack Zwyker 368 Elm St. 368 Elm St. WA WA (206) 423-4537 (206) 423-4537 Albert Aikens Albert Aikens 368 Elm St. 368 Elm St. WA WA (206) 423-4537 (206) 423-4537 </customers> Example

45 Aug’10 – Dec ’10 44 <HTML><BODY><TABLE> </xsl:for-each></TABLE></BODY> </xsl:template></xsl:stylesheet> Example

46 Aug’10 – Dec ’10 45 Output Albert Aikens 368 Elm St. (206) 423-4537 John Smith 123 Oak St. (206) 123-4567 Zack Zwyker 368 Elm St. (206) 423-4537 Example

47 Aug’10 – Dec ’10 46  Used to specify sort order for node-sets  Defines sort key  Sort key determines the order in which selected nodes are processed  A sort can be based on more than one element  The xsl:sort element can be used together with the xsl:apply-templates element and the xsl:for-each element </xsl:sort The element

48 Aug’10 – Dec ’10 47 Hard Shiny Has 4 wheels Internal Combustion Engine Fruit Juicy Dimpled skin Citrus Tall Four legs Big spots Mammal Crisp Savoury Off white Edible The element

49 Aug’10 – Dec ’10 48 Object Characteristics Characteristics of The element

50 Aug’10 – Dec ’10 49 Characteristics of Car Shiny Internal Combustion Engine Has 4 wheels Hard Characteristics of Giraffe Tall Mammal Four legs Big spots Characteristics of Orange Juicy Fruit Dimpled skin Citrus Characteristics of Prawn Cracker Savoury Off white Edible Crisp The element

51 Aug’10 – Dec ’10 50  Allows variables and parameters to be specified by the xsl:variable and xsl:param elements, respectively.  Both variables and parameters are referenced using $VariableName or $ParameterName syntax. <Ages> </Ages> XSLT Variables and Parameters

52 Aug’10 – Dec ’10 51 Finding an age using an XSLT parameter The age of is <xsl:value-of select=”@age”/> XSLT Variables and Parameters

53 Aug’10 – Dec ’10 52  Passing parameter from command line java -jar saxon8.jar -o Ages.html Ages.xml Ages.xslt person=”Hannah” Finding an age using an XSLT parameter The age of Hannah is 30 XSLT Variables and Parameters

54 Aug’10 – Dec ’10 53 XSLT Variables  Variables behave the same way as parameters but one difference  Parameters can be passed into a transformation from outside  Variables are defined inside an XSLT stylesheet  Variables can be global or local XSLT Variables and Parameters

55 Aug’10 – Dec ’10 54  call-template is used to call a template by name  Named templates are identified by a name attribute elements can go here. --> Named Templates and the Element

56 Aug’10 – Dec ’10 55  document()  key()  format-number()  generate-id() XSLT Funtions


Download ppt "XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert."

Similar presentations


Ads by Google