1 / 20

XML Language Family Detailed Examples

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 (juergen.mangler@univie.ac.at)

Download Presentation

XML Language Family Detailed Examples

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. XML Language FamilyDetailed Examples • Most information contained in these slide comes from: http://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 (juergen.mangler@univie.ac.at) • This section contains examples on: XSLT (eXtensible Stylesheet Language - Transformations)

  2. 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>

  3. 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

  4. 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>

  5. 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

  6. 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> Which Stylesheet produces which Output? <b>Joe</b><b><i>Smith</i></b> <b>JoeSmith</b>

  7. 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. <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="/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>

  8. 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.

  9. Parses only first level under <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> <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> Output <div style="color:purple">AAA id=a1</div> <div style="color:purple">AAA id=a2</div>

  10. Recurses into sublevels of <AAA> <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> <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> 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>

  11. 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>

  12. 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>

  13. 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>

  14. 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>

  15. 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> Output: <H1>Header1</H1><H3>Header3</H3><b>Bold text</b><sub>Subscript</sub><sup>Superscript</sup> <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>

  16. 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

  17. 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.

  18. 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>

  19. 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.

  20. 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>

More Related