Top Banner
Introduction to XSLT Introduction to XSLT
26

Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

Jan 19, 2016

Download

Documents

Devonte Austell
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

Introduction to XSLTIntroduction to XSLT

Page 2: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

What is XML?What is XML?

Design Goals of XMLDesign Goals of XML

XML FormatXML Format

XML DeclarationXML Declaration

ElementsElements

AttributesAttributes

Page 3: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

Design Goals of XMLDesign Goals of XML

XML shall be straightforwardly usable over the Internet.XML shall be straightforwardly usable over the Internet.XML shall support a wide variety of applications.XML shall support a wide variety of applications.XML shall be compatible with SGML.XML shall be compatible with SGML.It shall be easy to write programs which process XML documents.It shall be easy to write programs which process XML documents.The number of optional features in XML is to be kept to the absolute The number of optional features in XML is to be kept to the absolute minimum, ideally zero.minimum, ideally zero.XML documents should be human-legible and reasonably clear.XML documents should be human-legible and reasonably clear.The XML design should be prepared quickly.The XML design should be prepared quickly.The design of XML shall be formal and concise.The design of XML shall be formal and concise.XML documents shall be easy to create.XML documents shall be easy to create.Terseness in XML markup is of minimal importance. Terseness in XML markup is of minimal importance.

Page 4: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

XML FormatXML Format

XML contains elements, attributes, and XML contains elements, attributes, and data. An example XML entry would look data. An example XML entry would look like:like:<book isbn=” 102-0497449-4818519”><book isbn=” 102-0497449-4818519”>

<title>Tom Sawyer</title><title>Tom Sawyer</title><author>Mark Twain</author><author>Mark Twain</author>

</book></book>

Page 5: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

XML DeclarationXML Declaration

The XML Declaration should appear at the The XML Declaration should appear at the top of each XML file and should specify top of each XML file and should specify the XML version and the encoding used the XML version and the encoding used within the XML. An example XML within the XML. An example XML declaration would look like:declaration would look like:

<?xml version="1.0" encoding="ISO-8859-<?xml version="1.0" encoding="ISO-8859-1"?>1"?>

Page 6: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

ElementsElements

XML Elements are contained within <> XML Elements are contained within <> symbols and must nest properly. Each symbols and must nest properly. Each node that opens must have a node that opens must have a corresponding close node. Nodes can corresponding close node. Nodes can contain attributes, data, and “child” nodes. contain attributes, data, and “child” nodes. An example XML element (also called a An example XML element (also called a node) with a single piece of data would node) with a single piece of data would look like:look like:<book>Tom Sawyer</book><book>Tom Sawyer</book>

Page 7: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

AttributesAttributes

An XML Attribute is a single piece of data An XML Attribute is a single piece of data assigned to an XML Element. If data is assigned to an XML Element. If data is assigned as an attribute, the data can not be assigned as an attribute, the data can not be split further into sub-elements. XML attributes split further into sub-elements. XML attributes should only be used if the data will never need should only be used if the data will never need additional elements created below. An example additional elements created below. An example XML element with an attribute and a single piece XML element with an attribute and a single piece of data would look like:of data would look like:

<book isbn=” 102-0497449-4818519”>Tom <book isbn=” 102-0497449-4818519”>Tom Sawyer</book>Sawyer</book>

Page 8: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

XSLT BasicsXSLT Basics

What is XSLT?What is XSLT?

XML Declaration and XSLT DeclarationXML Declaration and XSLT Declaration

Connecting to an XML Data fileConnecting to an XML Data file

Page 9: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

What is XSLT?What is XSLT?

An XSLT stylesheet utilizes XPath in order An XSLT stylesheet utilizes XPath in order to transform the initial XML document into to transform the initial XML document into another document. The original XML another document. The original XML document must be well formed. The document must be well formed. The resultant document should also be well resultant document should also be well formed, though in certain instances, this formed, though in certain instances, this can be overridden as necessary.can be overridden as necessary.

Page 10: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

DeclarationsDeclarations

<?xml version="1.0" encoding="ISO-8859-1"?><?xml version="1.0" encoding="ISO-8859-1"?>In addition to the XML declaration, an XSLT In addition to the XML declaration, an XSLT stylesheet must specify a style sheet stylesheet must specify a style sheet declaration. The style sheet declaration is what declaration. The style sheet declaration is what enables the XSLT to utilize the binding features. enables the XSLT to utilize the binding features. An example XML Style Sheet Declaration would An example XML Style Sheet Declaration would look like:look like:<xsl:stylesheet version="1.0" <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transfoxmlns:xsl="http://www.w3.org/1999/XSL/Transform">rm">

Page 11: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

Connecting to XML DataConnecting to XML Data

In order to client-side bind the XML Data In order to client-side bind the XML Data with the XSLT Stylesheet, a single line with the XSLT Stylesheet, a single line must be added within the XML Data. An must be added within the XML Data. An example binding line would look like:example binding line would look like:

<?xml-stylesheet type="text/xsl" <?xml-stylesheet type="text/xsl" href="PageDesign.xsl"?>href="PageDesign.xsl"?>

This line should appear directly beneath This line should appear directly beneath the XML Declaration in the XML Data file.the XML Declaration in the XML Data file.

Page 12: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

XSLT TagsXSLT Tags

<xsl:template><xsl:template><xsl:template match=””><xsl:template match=””><xsl:apply-templates><xsl:apply-templates><xsl:value-of><xsl:value-of><xsl:for-each ><xsl:for-each ><xsl:sort><xsl:sort><xsl:if><xsl:if><xsl:choose><xsl:choose>position functionposition functionmodmod<xsl:call-template><xsl:call-template><xsl:copy><xsl:copy><xsl:copy-of><xsl:copy-of>

Page 13: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

<xsl:template><xsl:template>

The <xsl:template> element is used to The <xsl:template> element is used to create individual templates within a single create individual templates within a single stylesheet. These templates can be stylesheet. These templates can be accessed by simple matching or called accessed by simple matching or called directly via the name attribute.directly via the name attribute.

Page 14: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

<xsl:template match=””><xsl:template match=””>

When an XSLT style sheet comes across an When an XSLT style sheet comes across an <xsl:template match=”/”> tag, the style sheet will <xsl:template match=”/”> tag, the style sheet will look for matching nodes. The matching node is look for matching nodes. The matching node is determined by the value located within the determined by the value located within the match attribute. In this case a forward slash (/). match attribute. In this case a forward slash (/). In XSLT, a forward slash represents the root In XSLT, a forward slash represents the root node of the XML data. In this case, the template node of the XML data. In this case, the template should be matched only a single time as only a should be matched only a single time as only a single root node can exist within an XML single root node can exist within an XML document.document.

Page 15: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

<xsl:apply-templates><xsl:apply-templates>

When an XSLT style sheet comes across When an XSLT style sheet comes across an <xsl:apply templates> tag, the style an <xsl:apply templates> tag, the style sheet will compare current nodes with sheet will compare current nodes with available <xsl:template match=””> tags. available <xsl:template match=””> tags. For each match it comes across (in the For each match it comes across (in the current node set), the template will be current node set), the template will be applied. The select statement can be applied. The select statement can be added to the tag in order to specify the added to the tag in order to specify the order in which the nodes are processed. order in which the nodes are processed.

Page 16: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

<xsl:value-of><xsl:value-of>

The <xsl:value-of> tag is used to take the The <xsl:value-of> tag is used to take the value (or attribute value) from an XML value (or attribute value) from an XML element within the XML Data file and use it element within the XML Data file and use it within the output stream of the stylesheet. within the output stream of the stylesheet. The standard use is to write output to the The standard use is to write output to the final output.final output.

Page 17: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

<xsl:for-each ><xsl:for-each >

The <xsl:for-each> tag allows the XSLT to The <xsl:for-each> tag allows the XSLT to loop through a set of nodes at which point loop through a set of nodes at which point it can use data within each node for it can use data within each node for output. The <xsl:for-each> is often used output. The <xsl:for-each> is often used to create lists or tables of repeating data. to create lists or tables of repeating data. XPath is used to determine which XPath is used to determine which selection will be looped and can include selection will be looped and can include select statements to filter the looped items. select statements to filter the looped items.

Page 18: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

<xsl:sort><xsl:sort>

The <xsl:sort> tag can be used within The <xsl:sort> tag can be used within <xsl:for-each> lops in order to sort the <xsl:for-each> lops in order to sort the nodes before the final output has been nodes before the final output has been created. This is often useful for created. This is often useful for alphabetizing items within a repeating list. alphabetizing items within a repeating list.

Page 19: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

<xsl:if><xsl:if>

The <xsl:if > statement allows the XSLT to The <xsl:if > statement allows the XSLT to perform a test on the current node set to perform a test on the current node set to see if it meets conditions for continued see if it meets conditions for continued processing. processing.

The <xsl:if> statement must contain the The <xsl:if> statement must contain the entire section it will affect. <xsl:if> entire section it will affect. <xsl:if> statements are most often used within statements are most often used within <xsl:for-each> loops. <xsl:for-each> loops.

Page 20: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

<xsl:choose><xsl:choose>

The <xsl:choose> statement is used to The <xsl:choose> statement is used to select a single instance of multiple select a single instance of multiple possibilities. The first condition that is met possibilities. The first condition that is met will be used, if no condition are met, the will be used, if no condition are met, the <xsl:choose> statement allows the use of <xsl:choose> statement allows the use of <xsl:otherwise> which will be the catch-all <xsl:otherwise> which will be the catch-all condition. condition.

Page 21: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

position functionposition function

The position function is used to determine The position function is used to determine the current position in the node set. This the current position in the node set. This tag will often be used within <xsl:for-each> tag will often be used within <xsl:for-each> loops in order to number the output. loops in order to number the output.

Page 22: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

modmod

Mod has many applications, but the most Mod has many applications, but the most common is alternating row colors within a common is alternating row colors within a table. Because of the nature of mod, the table. Because of the nature of mod, the developer can determine how often a developer can determine how often a pattern repeats (every other row, every pattern repeats (every other row, every third row, etc.). third row, etc.).

Page 23: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

<xsl:call-template><xsl:call-template>

The <xsl:call-template> tag works very The <xsl:call-template> tag works very much like <xsl:apply-templates> with the much like <xsl:apply-templates> with the exception that the developer is able to call exception that the developer is able to call a chosen template regardless of the a chosen template regardless of the current node in use. This adds the ability current node in use. This adds the ability to call template that have nested calls to call template that have nested calls whether or not the original template should whether or not the original template should have been called in the first place. have been called in the first place.

Page 24: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

<xsl:copy> and <xsl:copy-of><xsl:copy> and <xsl:copy-of>

The <xsl:copy> tag allows the XSLT to The <xsl:copy> tag allows the XSLT to create a copy of the currently selected create a copy of the currently selected node. node.

The <xsl:copy-of> tag is very similar to the The <xsl:copy-of> tag is very similar to the <xsl:copy” tag but copies the children <xsl:copy” tag but copies the children nodes and data as well. nodes and data as well.

Page 25: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

Old vs NewOld vs New

<img><xsl:attribute <img><xsl:attribute name=“src”><xsl:value-of name=“src”><xsl:value-of select=“./source” select=“./source” /></xsl:attribute><xsl:attribute /></xsl:attribute><xsl:attribute name=“alt”><xsl:value-of name=“alt”><xsl:value-of select=“./altTag” /></xsl:attribute></img>select=“./altTag” /></xsl:attribute></img>

<img src=“{./source}” alt=“{./altTag}” /><img src=“{./source}” alt=“{./altTag}” />

Page 26: Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.

Questions?Questions?