20
XML Language Family Detailed Examples Most information contained in these slide comes from: h ttp://www.w3.org, http://www.zvon.org/ These slides are intended to be used as a tutorial on XML and related technologies Slide author: Jürgen Mangler ([email protected]) This section contains examples on: XSLT (eXtensible Stylesheet Language - Transformations)

XML Language Family Detailed Examples

Embed Size (px)

DESCRIPTION

XML Language Family Detailed Examples. Most information contained in these slide comes from: h ttp://www.w3.org, http://www.zvon.org/ These slides are intended to be used as a tutorial on XML and related technologies Slide author: Jürgen Mangler ([email protected]) - PowerPoint PPT Presentation

Citation preview

Page 1: XML Language Family Detailed Examples

XML Language FamilyDetailed Examples

• Most information contained in these slide comes from: h ttp://www.w3.org, http://www.zvon.org/

• These slides are intended to be used as a tutorial on XML and related technologies

• Slide author:Jürgen Mangler ([email protected])

• This section contains examples on:

XSLT(eXtensible Stylesheet Language -

Transformations)

Page 2: XML Language Family Detailed Examples

With XSL you can freely modify the content and/or layout any source text. You can apply different Stylesheets to the same source to get different results.

<source> <title>XSL</title> <author>John Smith</author> </source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="/">     <h1><xsl:value-of select="//title"/></h1>       <h2><xsl:value-of select="//author"/></h2> </xsl:template></xsl:stylesheet>

Output:<h1>John Smith</h1><h2>XSL</h2>

How can you produce the following output?<h2>John Smith</h2><h1>XSL</h1>

Page 3: XML Language Family Detailed Examples

Every XSL stylesheet must start with an xsl:stylesheet element. The attribute version='1.0' specifies version of XSL(T) specification. This example shows the simplest possible stylesheet. As it does not contain any information, default processing is used.

<source> <em>Hello, world</em></source>

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'></ xsl:stylesheet>

Output:Hello, world

Page 4: XML Language Family Detailed Examples

An XSL processor parses an XML source and tries to find a matching template rule. If it does, instructions inside the matching template are evaluated.

<source> <bold>Hello, world.</bold> <red>I am </red> <italic>fine.</italic> </source>

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="bold"> <p><b><xsl:value-of select="."/></b></p> </xsl:template> <xsl:template match="red"> <p style="color:red"><xsl:value-of select="."/></p> </xsl:template> <xsl:template match="italic"> <p><i><xsl:value-of select="."/></i></p> </xsl:template></xsl:stylesheet>

Output<p><b>Hello, world.</b></p><p style="color:red">I am </p><p><i>fine.</i></p>

Page 5: XML Language Family Detailed Examples

Contents of the original elements can be recovered from the original sources in two basic ways.

Stylesheet 1 uses xsl:value-of construct. In this case the contents of the element is used without any further processing.

The instruction xsl:apply-templates in Stylesheet 2 is different. The parser further processes selected elements, for which a template is defined.

<source> <employee>     <firstName>Joe</firstName>       <surname>Smith</surname> </employee> </source>

For the next page we use this source

Page 6: XML Language Family Detailed Examples

Stylesheet 1:<xsl:stylesheet version='1.0‚ xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="employee">       <b><xsl:value-of select="."/></b> </xsl:template> <xsl:template match="surname">       <i><xsl:value-of select="."/></i> </xsl:template></xsl:stylesheet>

Stylesheet 2:<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="employee">       <b><xsl:apply-templates select="firstName"/></b>       <b><xsl:apply-templates select="surname"/></b> </xsl:template> <xsl:template match="surname">     <i><xsl:value-of select="."/></i> </xsl:template></xsl:stylesheet>

<b>Joe</b><b><i>Smith</i></b>

<b>JoeSmith</b>

Which Stylesheet produces which Output?

Page 7: XML Language Family Detailed Examples

<source> <AAA id="a1" pos="start">       <BBB id="b1"/>       <BBB id="b2"/> </AAA> <AAA id="a2">       <BBB id="b3"/>       <BBB id="b4"/>       <CCC id="c1">          <DDD id="d1"/>       </CCC> </AAA> </source>

Parts of an XML document to which a template should be applied are determined by location paths. The required syntax is specified in the XPath specification. Simple cases looks very similar to filesystem addressing.

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="/source/AAA/CCC/DDD">     <p style="color:red">          <xsl:value-of select="name()"/>          <xsl:text> id=</xsl:text>          <xsl:value-of select="@id"/>       </p> </xsl:template></xsl:stylesheet>

Output<p style="color:red">DDD id=d1</p>

Page 8: XML Language Family Detailed Examples

Processing always starts with the template match="/". This matches the root node (the node whose only child element is the document element, in our case "source"). Many stylesheets do not contain this element explicitly.

When this template is not explicitly given, the implicit template is used (it contains as the sole instruction).

<xsl:template match="*|/"><xsl:apply-templates/>

</xsl:template>

This instruction means: process all children of the current node, including text nodes.

Page 9: XML Language Family Detailed Examples

<source> <AAA id="a1" pos="start">       <BBB id="b1"/>       <BBB id="b2"/> </AAA> <AAA id="a2">       <BBB id="b3"/>       <BBB id="b4"/>       <CCC id="c1">          <DDD id="d1"/>       </CCC> </AAA> </source>

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="AAA">     <div style="color:purple">          <xsl:value-of select="name()"/>          <xsl:text> id=</xsl:text>          <xsl:value-of select="@id"/>       </div> </xsl:template> <xsl:template match="BBB">       <div style="color:blue">          <xsl:value-of select="name()"/>          <xsl:text> id=</xsl:text>          <xsl:value-of select="@id"/>       </div> </xsl:template> </xsl:stylesheet>

Output<div style="color:purple">AAA id=a1</div> <div style="color:purple">AAA id=a2</div>

Parses only first level under <source>

Page 10: XML Language Family Detailed Examples

<source> <AAA id="a1" pos="start">       <BBB id="b1"/>       <BBB id="b2"/> </AAA> <AAA id="a2">       <BBB id="b3"/>       <BBB id="b4"/>       <CCC id="c1">          <DDD id="d1"/>       </CCC> </AAA> </source>

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="AAA">     <div style="color:purple">          <xsl:value-of select="name()"/>          <xsl:text> id=</xsl:text>          <xsl:value-of select="@id"/>       </div> <xsl:apply-templates/> </xsl:template> <xsl:template match="BBB">       <div style="color:blue">          <xsl:value-of select="name()"/>          <xsl:text> id=</xsl:text>          <xsl:value-of select="@id"/>       </div> </xsl:template> </xsl:stylesheet>

Output<div style="color:purple">AAA id=a1</div> <div style="color:blue">BBB id=b1</div> <div style="color:blue">BBB id=b2</div> <div style="color:purple">AAA id=a2</div> <div style="color:blue">BBB id=b3</div> <div style="color:blue">BBB id=b4</div>

Recurses into sublevels of <AAA>

Page 11: XML Language Family Detailed Examples

A template can match individual paths being separated with "|" ( Stylesheet 1) from a selection of location paths. Wildcard "*" selects all possibilities. Compare Stylesheet 1 with Stylesheet 2.

Stylesheet 1:<xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="firstName|surname"> <div>       <xsl:text>[template: </xsl:text>            <xsl:value-of select="name()"/>            <xsl:text> outputs </xsl:text>            <xsl:apply-templates/>            <xsl:text> ]</xsl:text>      </div> </xsl:template></xsl:stylesheet>

<source> <employee>       <firstName>Joe</firstName>       <surname>Smith</surname> </employee> </source>

Output:<div>[template: firstName outputs Joe ]</div><div>[template: surname outputs Smith ]</div>

Page 12: XML Language Family Detailed Examples

A template can match individual paths being separated with "|" ( Stylesheet 1) from a selection of location paths. Wildcard "*" selects all possibilities. Compare Stylesheet 1 with Stylesheet 2.Stylesheet 2:<xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="*"> <div>       <xsl:text>[template: </xsl:text>            <xsl:value-of select="name()"/>            <xsl:text> outputs </xsl:text>            <xsl:apply-templates/>            <xsl:text> ]</xsl:text>      </div> </xsl:template></xsl:stylesheet>

<source> <employee>       <firstName>Joe</firstName>       <surname>Smith</surname> </employee> </source>

Output:<div>[template: source outputs <div>[template: employee outputs <div>[template: firstName outputs Joe ]</div>     <div>[template: surname outputs Smith ]</div>]</div>]</div>

Page 13: XML Language Family Detailed Examples

With modes an element can be processed multiple times, each time producing a different result.

Stylesheet 2:<xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="/">      <xsl:apply-templates select="//CCC" mode="red"/>      <xsl:apply-templates select="//CCC"/> </xsl:template><xsl:template match="CCC" mode="red">      <div style="color:red">           <xsl:value-of select="name()"/>      </div> </xsl:template><xsl:template match="CCC">      <div style="color:purple">           <xsl:value-of select="name()"/>      </div> </xsl:template></xsl:stylesheet>

<source><AAA id="a2">      <CCC id="c1">           <CCC id="c2"/>

     </CCC>      <BBB id="b5">           <CCC id="c3"/>

     </BBB> </AAA></source>

Output:<div style="color:red">CCC</div><div style="color:red">CCC</div><div style="color:red">CCC</div><div style="color:purple">CCC</div><div style="color:purple">CCC</div><div style="color:purple">CCC</div>

Page 14: XML Language Family Detailed Examples

Axes play a very important role in XSLT – e.g. child axis, for-each. Stylesheet 2:

<xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="/">      <table border="1" cellpadding="6">           <xsl:for-each select="/source/*">                <xsl:call-template name="print"/>           </xsl:for-each>      </table> </xsl:template><xsl:template name="print">      <tr>           <td> <xsl:value-of select="./@id"/>                 <xsl:text>: </xsl:text>

<xsl:for-each select="child::*">                      <xsl:value-of select="./@id"/>                      <xsl:text> </xsl:text>                </xsl:for-each>           </td>      </tr> </xsl:template> </xsl:stylesheet>

Document:<source><AAA id="a2">      <BBB id="b4"/>      <CCC id="c1"/>

</AAA></source>

Output:<table border="1" cellpadding="6"> <tr>      <td>a2: b4 c1</td> </tr></table>

Page 15: XML Language Family Detailed Examples

xsl:element generates elements in time of processing. In this example it transforms the sizes to formating tags. <xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="/">      <xsl:for-each select="//text">           <xsl:element name="{@size}">               <xsl:value-of select="."/>          </xsl:element>      </xsl:for-each> </xsl:template></xsl:stylesheet>

<source> <text size="H1">Header1</text> <text size="H3">Header3</text> <text size="b">Bold text</text> <text size="sub">Subscript</text> <text size="sup">Superscript</text> </source>

Output:<H1>Header1</H1><H3>Header3</H3><b>Bold text</b><sub>Subscript</sub><sup>Superscript</sup>

Page 16: XML Language Family Detailed Examples

xsl:if instruction enables conditional processing. A typical case of xsl:for-each usage is to add a text between individual entries. Very often you do not want to add text after the last element:

<xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="source">      <xsl:for-each select="entry">           <xsl:value-of select="@name"/>           <xsl:if test="not (position()=last())">                <xsl:text>, </xsl:text>           </xsl:if>      </xsl:for-each> </xsl:template> </xsl:stylesheet>

<source> <entry name="A"/>    <entry name="B"/>    <entry name="C"/> <entry name="D"/></source>

Output:A, B, C, D

Page 17: XML Language Family Detailed Examples

Numbering of individual chapter elements depends on the position of the chapter element. Each level of chapters is numbered independently. Setting the attribute level to multiple enables natural numbering.

<xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="/">      <TABLE BORDER="1">           <xsl:for-each select="//chapter">                <TR>                    <TD>                          <xsl:number level="multiple"/>                          <xsl:text> - </xsl:text> <xsl:value-of select="./text()"/>                     </TD>               </TR>           </xsl:for-each>      </TABLE> </xsl:template> </xsl:stylesheet>

<source><chapter>First Chap.</chapter> <chapter>Sec. Chap.      <chapter>Sub 1</chapter>      <chapter>Sub 2</chapter> </chapter> </source>

What could be the possible output?Continued on next page.

Page 18: XML Language Family Detailed Examples

Numbering of individual chapter elements depends on the position of the chapter element. Each level of chapters is numbered independently. Setting the attribute level to multiple enables natural numbering.

Output:<TABLE BORDER="1">  <TR>     <TD>1 - First Chap.</TD>  </TR>  <TR>     <TD>2 - Sec. Chap.</TD>  </TR>  <TR>     <TD>2.1 - Sub 1</TD>  </TR>  <TR>     <TD>2.2 - Sub 2</TD>  </TR>  </TABLE>

Page 19: XML Language Family Detailed Examples

You can set variables in a Stylesheet and use them later in the processing. The following example demonstrates a way of setting xsl:variable.<xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:variable name="totalChapters">      <xsl:value-of select="count(//chapter)"/> </xsl:variable> <xsl:template match="/">      <TABLE>           <xsl:for-each select="//chapter">                <TR><TD>                    <xsl:value-of select="."/>                    <xsl:value-of select="position()"/>                    <xsl:text>/</xsl:text>                    <xsl:value-of select="$totalChapters"/>                </TD></TR>           </xsl:for-each>      </TABLE> </xsl:template> </xsl:stylesheet>

<source> <chapter>Chapter</chapter> <chapter>Chapter</chapter> <chapter>Chapter</chapter> <chapter>Chapter</chapter> </source>

What could be the possible output?Continued on next page.

Page 20: XML Language Family Detailed Examples

You can set variables in a Stylesheet an use them later in the processing. The following example demonstrates a way of setting xsl:variable.

Output:<TABLE>  <TR>     <TD>Chapter 1/4</TD>  </TR>  <TR>     <TD>Chapter 2/4</TD>  </TR>  <TR>     <TD>Chapter 3/4</TD>  </TR>  <TR>     <TD>Chapter 4/4</TD>  </TR></TABLE>