168493: XML and Web Services, Semester I/2546Page 1/16 Page 1/16

168493: XML and Web Services, Semester I/2546Page 1/16 Page 1/16

168493: XML and Web Services, Semester I/2546Page 1/16 Page 1/16

Exercise 2

Please specify the output of these following pairs of the input XML source and the XSLT style sheet.

  1. XSLT stylesheet1

XML source

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

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<xsl:template match="/">
<h1>
<xsl:value-of select="//title"/>
</h1>
<h2>
<xsl:value-of select="//author"/>
</h2>
</xsl:template>
</xsl:stylesheet>

Output

2. XSLT stylesheet1

XML source

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

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<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

3. XSLT stylesheet3

XML source

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

XSLT Stylesheet

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<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>

Output

  1. XSLT Stylesheet4

XML Source

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

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<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>

Output

  1. XSLT Stylesheet5

XML Source

<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>
<BBB id="b5">
<CCC id="c2"/>
</BBB>
</AAA>
</source>

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<xsl:template match="BBB">
<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="/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

  1. XSLT Stylesheet6

XML Source

<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>
<BBB id="b5">
<CCC id="c2"/>
</BBB>
</AAA>
</source>

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<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:stylesheet>

Output

  1. XSLT Stylesheet7

XML Source

<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">
<CCC id="c2"/>
</CCC>
<BBB id="b5">
<CCC id="c3"/>
</BBB>
</AAA>
</source>

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<xsl:template match="/">
<xsl:apply-templates select="//CCC" mode="red"/>
<xsl:apply-templates select="//CCC" mode="yellow"/>
</xsl:template>
<xsl:template match="CCC" mode="red">
<div style="color:red">
<xsl:value-of select="name()"/>
<xsl:text> id=</xsl:text>
<xsl:value-of select="@id"/>
</div>
</xsl:template>
<xsl:template match="CCC">
<div style="color:purple">
<xsl:value-of select="name()"/>
<xsl:text> id=</xsl:text>
<xsl:value-of select="@id"/>
</div>
</xsl:template>
</xsl:stylesheet>

Output

  1. XSLT Stylesheet8

XML Source

<source>
<dog name="Joe">
<data weight="18 kg" color="black"/>
</dog>
</source>

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<xsl:template match="dog">
<p>
<b>
<xsl:text>Dog: </xsl:text>
</b>
<xsl:value-of select="@name"/>
</p>
<p>
<b>
<xsl:text>Color: </xsl:text>
</b>
<xsl:value-of select="data/@color"/>
</p>
</xsl:template>

Output

  1. XSLT Stylesheet9

XML Source

<source>
<employee id="js0034"> Joe Smith </employee>
</source>

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<xsl:template match="employee">
<xsl:value-of select="."/>
<xsl:text>[</xsl:text>
<xsl:apply-templates select="@id"/>
<xsl:text>]</xsl:text>
</xsl:template>
<xsl:template match="@id">
<b>
<i>
<xsl:value-of select="."/>
</i>
</b>
</xsl:template>
</xsl:stylesheet>

Output

  1. XSLT Stylesheet10

XML Source

<source>
<car id="a234" checked="yes"/>
<car id="a111" checked="yes"/>
<car id="a005"/>
</source>

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<xsl:template match="car[@checked]">
<p>
<xsl:text>Car: </xsl:text>
<xsl:value-of select="@id"/>
</p>
</xsl:template>
</xsl:stylesheet>

Output

  1. XSLT Stylesheet11

XML Source

<source>
<car id="11"/>
<car id="6"/>
<car id="105"/>
</source>

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<xsl:template match="/">
<TABLE>
<xsl:for-each select="//car">
<xsl:sort data-type="text" select="@id"/>
<TR>
<TH>
<xsl:text>Car-</xsl:text>
<xsl:value-of select="@id"/>
</TH>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>

Output

  1. XSLT Stylesheet12

XML Source

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

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<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

  1. XSLT Stylesheet13

XML Source

<source>
<color>blue</color>
<color>navy</color>
<color>green</color>
</source>

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<xsl:template match="color">
<TABLE>
<TR>
<TD>
<xsl:attribute name="style">
<xsl:text>color:</xsl:text>
<xsl:value-of select="."/>
</xsl:attribute>
<xsl:value-of select="."/>
</TD>
</TR>
</TABLE>
</xsl:template>
</xsl:stylesheet>

Output

  1. XSLT Stylesheet14

XML Source

<source>
<h1>GREETING</h1>
<p>Hello, world!</p>
</source>

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<xsl:template match="/">
<xsl:apply-templates select="/source/*"/>
</xsl:template>
<xsl:template match="h1">
<xsl:copy use-attribute-sets="H1">
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<xsl:copy use-attribute-sets="P ">
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
<xsl:attribute-set name="H1">
<xsl:attribute name="align">center</xsl:attribute>
<xsl:attribute name="style">color:red</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="P">
<xsl:attribute name="align">left</xsl:attribute>
<xsl:attribute name="style">color:blue</xsl:attribute>
</xsl:attribute-set>
</xsl:stylesheet>

Output

  1. XSLT Stylesheet15

XML Source

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

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<xsl:template match="list">
<xsl:for-each select="entry">
<xsl:value-of select="@name"/>
<xsl:text>, </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Output

  1. XSLT Stylesheet16

XML Source

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

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<xsl:template match="list">
<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>

Output

  1. XSLT Stylesheet17

XML Source

<source>
<n>one</n>
<n>two</n>
<n>three</n>
<n>four</n>
</source>

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<xsl:template match="/">
<TABLE>
<xsl:for-each select="//n">
<TR>
<TD>
<xsl:number value="position()" format="1. "/>
<xsl:value-of select="."/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>

Output

  1. XSLT Stylesheet18

XML Source

<source>
<chapter>Chapter A</chapter>
<chapter>Chapter B</chapter>
</source>

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<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:text> (</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="$totalChapters"/>
<xsl:text>)</xsl:text>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>

Output

  1. XSLT Stylesheet19

XML Source

<source>
<TABLE border="1">
<TR>
<TD>AAA</TD>
<TD>BBB</TD>
</TR>
</TABLE>
<TABLE border="1">
<TR>
<TD>1111111</TD>
<TD>22222222</TD>
</TR>
</TABLE>
</source>

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<xsl:variable name="A1">
<xsl:copy-of select="//TABLE[1]"/>
</xsl:variable>
<xsl:variable name="A2">
<xsl:copy-of select="//TABLE[2]"/>
</xsl:variable>
<xsl:template match="/">
<xsl:copy-of select="$A2"/>
<xsl:copy-of select="$A1"/>
<xsl:copy-of select="$A2"/>
</xsl:template>
</xsl:stylesheet>

Output

  1. XSLT Stylesheet20

XML Source

<source>
<number>1</number>
<number>3</number>
<number>4</number>
<number>17</number>
<number>8</number>
<number>11</number>
</source>

XSLT stylesheet

<xsl:stylesheet version = '1.0'
xmlns:xsl='
<xsl:template match="/">
<P>
<xsl:value-of select="//number[1]"/>
<xsl:text> + </xsl:text>
<xsl:value-of select="//number[2]"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="//number[1] + //number[2]"/>
</P>
<P>
<xsl:value-of select="//number[3]"/>
<xsl:text> - </xsl:text>
<xsl:value-of select="//number[4]"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="//number[3] - //number[4]"/>
</P>
<P>
<xsl:value-of select="//number[5]"/>
<xsl:text> * </xsl:text>
<xsl:value-of select="//number[6]"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="//number[5] * //number[6]"/>
</P>
</xsl:template>
</xsl:stylesheet>

Output