Parameters and variables

XSLT / XQuery
xsl:param name="cd">Cd collection</xsl:param
xsl:variable name="cds">My Cd collection</xsl:variable / declare variable $c as xs:string := "Cd collection";
Invoked by: / calling a variable:
xsl:value-of select="$cd"/>
xsl:value-of select="$cds"/> / return h2{$c}</h2
Example: / Example:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
xsl:stylesheet version="1.0":xsl="http://www.w3.org/1999/XSL/Transform">
xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>
xsl:param name="cd">Cd collection</xsl:param
xsl:variable name="cds">My Cd collection</xsl:variable
xsl:template match="/">
html
body
h2Parameter: xsl:value-of select="$cd"/</h2
h3Variable: xsl:value-of select="$cds"/</h3
</body
</html
</xsl:template
</xsl:stylesheet / declare variable $c as xs:string := "Cd collection";
html
body
{
for $x in doc("cdcatalog.xml")/catalog
return h2{$c}</h2
}
</body
</html
Output: / Output:
html
body
h2Parameter: Cd collection</h2
h3Variable: My Cd collection</h3
</body
</html / html
body
h2Cd collection</h2
</body
</html

xsl:value-of à in XQuery the first position in a collection [1]

XSLT / XQuery /
xsl:value-of select="catalog/cd/title" /> / for $x in …(collection of data[1]) /
Invoked by: / usage: /
xsl:value-of select="catalog/cd/title" /> / for $x in doc("cdcatalog.xml")/catalog/cd[1] /
Example: / Example: /
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
xsl:stylesheet version="1.0":xsl="http://www.w3.org/1999/XSL/Transform">
xsl:template match="/">
html
body
h2My CD Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
tr
tdxsl:value-of select="catalog/cd/title" /</td
tdxsl:value-of select="catalog/cd/artist" /</td
</tr
</table
</body
</html
</xsl:template
</xsl:stylesheet / html
body
h2Cd Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
{
for $x in doc("cdcatalog.xml")/catalog/cd[1]
return tr
td{data($x/title)}</td
td{data($x/artist)}</td
</tr
}
</table
</body
</html /
Output:
/ Output:
/
html
body
h2My CD Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
tr
tdEmpire Burlesque</td
tdBob Dylan</td
</tr
</table
</body
</html / html
body
h2Cd Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
tr
tdEmpire Burlesque</td
tdBob Dylan</td
</tr
</table
</body
</html /

xsl:for-each> à for $x in…

XSLT / XQuery /
xsl:for-each select="node">
<!—function Body -->
</xsl:for-each / for $x in …(collection of data) /
usage: / usage: /
/ for $x in doc("cdcatalog.xml")/catalog/cd /
Example: / Example: /
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
xsl:stylesheet version="1.0":xsl="http://www.w3.org/1999/XSL/Transform">
xsl:template match="/">
html
body
h2My CD Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
xsl:for-each select="catalog/cd">
tr
tdxsl:value-of select="title"/</td
tdxsl:value-of select="artist"/</td
</tr
</xsl:for-each
</table
</body
</html
</xsl:template
</xsl:stylesheet / html
body
h2Cd Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
{
for $x in doc("cdcatalog.xml")/catalog/cd
return tr
td{data($x/title)}</td
td{data($x/artist)}</td
</tr
}
</table
</body
</html /
Output:
/ Output
: /
html
body
h2My CD Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
tr
tdEmpire Burlesque</td
tdBob Dylan</td
</tr
tr
tdHide your heart</td
tdBonnie Tyler</td
</tr



</table
</body
</html / html
body
h2Cd Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
tr
tdEmpire Burlesque</td
tdBob Dylan</td
</tr
tr
tdHide your heart</td
tdBonnie Tyler</td
</tr



</table
</body
</html /

Filtration is a very straight forward in both languages process;

XSLT / XQuery /
XPath/expression/[where condition]/node / XPath/expression/[where condition]/node
Or
where variable = “something” /
usage: / usage: /
xsl:for-each select="catalog/cd[artist=’Bonnie Tyler’]">
tr
tdxsl:value-of select="title"/</td
tdxsl:value-of select="artist"/</td
</tr
</xsl:for-each / for $x in doc("cdcatalog.xml")/catalog/cd[artist=”Bonnie Tyler”] /
Example: / Example: /
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
xsl:stylesheet version="1.0":xsl="http://www.w3.org/1999/XSL/Transform">
xsl:template match="/">
html
body
h2My CD Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
xsl:for-each select="catalog/cd [artist=’Bonnie Tyler’]">
tr
tdxsl:value-of select="title"/</td
tdxsl:value-of select="artist"/</td
</tr
</xsl:for-each
</table
</body
</html
</xsl:template
</xsl:stylesheet / html
body
h2Cd Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
{
doc("cdcatalog.xml")/catalog/cd[artist=”Bonnie Tyler”]
return tr
td{data($x/title)}</td
td{data($x/artist)}</td
</tr
}
</table
</body
</html /
Output:
/ Output:
/
html
body
h2My CD Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
tr
tdHide your heart</td
tdBonnie Tyler</td
</tr
</table
</body
</html / html
body
h2Cd Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
tr
tdHide your heart</td
tdBonnie Tyler</td
</tr
</table
</body
</html /

Sort xsl:sort/> à order by

XSLT / XQuery /
xsl:sort select="element"/ / Order by variable/XPath/expression /
usage: / usage: /
xsl:sort select="artist"/ / order by $x/artist /
Example: / Example: /
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
xsl:stylesheet version="1.0":xsl="http://www.w3.org/1999/XSL/Transform">
xsl:template match="/">
html
body
h2My CD Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
xsl:for-each select="catalog/cd">
xsl:sort select=”artist”/
tr
tdxsl:value-of select="title"/</td
tdxsl:value-of select="artist"/</td
</tr
</xsl:for-each
</table
</body
</html
</xsl:template
</xsl:stylesheet / html
body
h2Cd Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
{
doc("cdcatalog.xml")/catalog/cd
order by $x/artist
return tr
td{data($x/title)}</td
td{data($x/artist)}</td
</tr
}
</table
</body
</html /
Output:
/ Output:
/

sxl:if à if () then else() but with empty else() expression

XSLT / XQuery /
xsl:if test="conditional expression">
<!—function Body -->
</xsl:if / If( conditional expression ) then
<!—function Body -->
else() /
usage: / usage: /
xsl:if test="price &gt; 10">
tr
td
xsl:value-of select="title"/>
</td
td
xsl:value-of select="artist"/>
</td
</tr
</xsl:if / return if($x/price > 10) then
tr
td{data($x/title)}</td td{data($x/artist)}</td
</tr
else() /
Example: / Example: /
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
xsl:template match="/">
html
body
h2My CD Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
xsl:for-each select="catalog/cd">
xsl:sort select="artist"/>
xsl:if test="price &gt; 10">
tr
td
xsl:value-of select="title"/>
</td
td
xsl:value-of select="artist"/>
</td
</tr
</xsl:if
</xsl:for-each
</table
</body
</html
</xsl:template
</xsl:stylesheet / (: demonstrate <xsl:if test=""> of XSLT:)
html
body
h2Cd Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
{
for $x in doc("cdcatalog.xml")/catalog/cd
order by $x/artist
return if($x/price > 10) then
tr
td{data($x/title)}</td td{data($x/artist)}</td
</tr
else()
}
</table
</body
</html /
Output:
/ Output:
/
html
body
h2My CD Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
tr
tdRomanza</td
tdAndrea Bocelli</td
</tr
tr
tdOne night only</td
tdBee Gees</td
</tr
tr
tdEmpire Burlesque</td
tdBob Dylan</td
</tr



</table
</body
</html / html
body
h2Cd Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
tr
tdRomanza</td
tdAndrea Bocelli</td
</tr
tr
tdOne night only</td
tdBee Gees</td
</tr
tr
tdEmpire Burlesque</td
tdBob Dylan</td
</tr



</table
</body
</html /

sxl:choose à if() then else if()then else()

XSLT / XQuery /
xsl:choose
xsl:when test=" conditional expression ">
<!—function Body -->
</xsl:when
xsl:when test=" conditional expression ">
<!—function Body -->
</xsl:when
xsl:otherwise
<!—function Body -->
</xsl:otherwise
</xsl:choose / if($x/price > 10) then
<!—function Body -->
if ($x/price > 9) then
<!—function Body -->
else()
<!—function Body --> /
usage: / usage: /
xsl:choose
xsl:when test="price &gt; 10">
td bgcolor="#ff00ff">
xsl:value-of select="artist"/>
</td
</xsl:when
xsl:when test="price &gt; 9">
td bgcolor="#cccccc">
xsl:value-of select="artist"/</td
</xsl:when
xsl:otherwise
tdxsl:value-of select="artist"/</td
</xsl:otherwise
</xsl:choose / return if($x/price > 10) then
tr
td{data($x/title)}</td
td bgcolor="#ff00ff">{data($x/artist)}</td
</tr
if ($x/price > 9) then
tr
td{data($x/title)}</td
td bgcolor="#cccccc">{data($x/artist)}</td
</tr
else() /
Example: / Example: /
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
xsl:template match="/">
html
body
h2My CD Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
xsl:for-each select="catalog/cd">
xsl:sort select="artist"/>
tr
td
xsl:value-of select="title"/>
</td
xsl:choose
xsl:when test="price &gt; 10">
td bgcolor="#ff00ff">
xsl:value-of select="artist"/>
</td
</xsl:when
xsl:when test="price &gt; 9">
td bgcolor="#cccccc">
xsl:value-of select="artist"/>
</td>
</xsl:when>
<xsl:otherwise>
<td>
<xsl:value-of select="artist"/>
</td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
</xsl:stylesheet / (: demonstrate <xsl:chooser> of XSLT:)
html
body
h2Cd Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
{
for $x in doc("cdcatalog.xml")/catalog/cd
order by $x/artist
return if($x/price > 10) then
tr
td{data($x/title)}</td
td bgcolor="#ff00ff">{data($x/artist)}</td
</tr
if ($x/price > 9) then
tr
td{data($x/title)}</td
td bgcolor="#cccccc">{data($x/artist)}</td
</tr
else()
tr
td{data($x/title)}</td
td{data($x/artist)}</td
</tr
}
</table
</body
</html /
Output:
/ Output:
/
html
body
h2My CD Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
tr
tdRomanza</td
td bgcolor="#ff00ff">Andrea Bocelli</td
</tr
tr
tdOne night only</td
td bgcolor="#ff00ff">Bee Gees</td
</tr
tr
tdEmpire Burlesque</td
td bgcolor="#ff00ff">Bob Dylan</td
</tr



</table>
</body>
</html> / html
body
h2Cd Collection</h2
table border="1">
tr bgcolor="#9acd32">
thTitle</th
thArtist</th
</tr
tr
tdRomanza</td
td bgcolor="#ff00ff">Andrea Bocelli</td
</tr
tr
tdOne night only</td
td bgcolor="#ff00ff">Bee Gees</td
</tr
tr
tdEmpire Burlesque</td
td bgcolor="#ff00ff">Bob Dylan</td
</tr



</table
</body
</html /

Templates à Functions

XSLT / XQuery /
xsl:template match="node">
<!—Template Body -->
</xsl:template / declare function local:cd-info($cd as element()? ) as element()?
{ <!—Function Body --> } /
Invoked by: / calling a variable: /
xsl:apply-templates/>
xsl:apply-templates select="XPath/to/node "/> / local:cd-info($x) /
Example: / Example: /
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
xsl:stylesheet version="1.0":xsl="http://www.w3.org/1999/XSL/Transform">
xsl:template match="/">
html
body
h2My CD Collection</h2
xsl:apply-templates/>
</body
</html
</xsl:template
xsl:template match="cd">
p
xsl:apply-templates select="title"/>
xsl:apply-templates select="artist"/>
</p
</xsl:template
xsl:template match="title">
Title: span style="color:#ff0000">
xsl:value-of select="."/</span
br />
</xsl:template
xsl:template match="artist">
Artist: span style="color:#00ff00">
xsl:value-of select="."/</span
br />
</xsl:template
</xsl:stylesheet / : declare functions :)
declare function local:cd-info($cd as element()? ) as element()?
{
let $t := $cd/title
let $a := $cd/artist
return
pTitle: span style="color:#ff0000">
{data($t)}</span
br/>
Artist: span style="color:#00ff00">
{data($a)}</span
</p
};
html
body
h2Cd Collection</h2
{
for $x in doc("cdcatalog.xml")/catalog/cd
return li{local:cd-info($x)}</li
}
</body
</html /
Output:
/ Output:
/
html
body
h2My CD Collection</h2
p
Title: span style="color:#ff0000">Empire Burlesque</spanbr
Artist: span style="color:#00ff00">Bob Dylan</spanbr</p
p
Title: span style="color:#ff0000">Hide your heart</spanbr
Artist: span style="color:#00ff00">Bonnie Tyler</spanbr</p
p



</body
</html / html
body
h2Cd Collection</h2
li
p
Title:
span style="color:#ff0000">Empire Burlesque
</span
br />
Artist:
span style="color:#00ff00">Bob Dylan
</span
</p
</li
li
p
Title:
span style="color:#ff0000">Hide your heart
</span
br />
Artist:
span style="color:#00ff00">Bonnie Tyler
</span
</p
</li


/body
/html /

Group By

XSLT / XQuery /
xsl:key name="by-country" match="cd" use="country"/>
generate-id(.) = generate-id(key('by-country', country)[1]) / distinct-values(doc("cdcatalog.xml")/catalog/cd/country)
for $x in doc("cdcatalog.xml")/catalog/cd
where $x/country = $c /
Invoked by: / calling a variable: /
xsl:for-each select="key('by-country', country)">
p
xsl:apply-templates select="title"/>
xsl:apply-templates select="artist"/>
</p
</xsl:for-each / for $c in distinct-values(doc("cdcatalog.xml")/catalog/cd/country)
return
p
{$c}
{
for $x in doc("cdcatalog.xml")/catalog/cd
where $x/country = $c
return li{local:cd-info($x)}</li
}
{count(doc("cdcatalog.xml")/catalog/cd[country = $c])}
p/>
</p /
Example: / Example: /
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
xsl:key name="by-country" match="cd" use="country"/>
xsl:key name="countCd" match="*[country]" use="country"/>
xsl:template match="catalog">
html
body
h2My CD Collection</h2
xsl:apply-templates select="cd[generate-id(.) = generate-id(key('by-country', country)[1])]"/>
</body
</html
</xsl:template
xsl:template match="cd">
b
xsl:value-of select="country"/>
</b
ul
xsl:for-each select="key('by-country', country)">
p
xsl:apply-templates select="title"/>
xsl:apply-templates select="artist"/>
</p
</xsl:for-each
CD# = xsl:value-of select="count(key('countCd', country))"/>
</ul
</xsl:template
xsl:template match="title">
Title: span style="color:#ff0000">
xsl:value-of select="."/>
</span
br/>
</xsl:template
xsl:template match="artist">
Artist: span style="color:#00ff00">
xsl:value-of select="."/>
</span
br/>
</xsl:template
</xsl:stylesheet / (: declare functions :)
declare function local:cd-info($cd as element()? ) as element()?
{
let $t := $cd/title
let $a := $cd/artist
return
pTitle: span style="color:#ff0000">{data($t)}</span
br/>
Artist: span style="color:#00ff00">{data($a)}</span
</p
};
html
body
h2Cd Collection</h2
{
for $c in distinct-values(doc("cdcatalog.xml")/catalog/cd/country)
return
p
{$c}
{
for $x in doc("cdcatalog.xml")/catalog/cd
where $x/country = $c
return li{local:cd-info($x)}</li
}
CD# = {count(doc("cdcatalog.xml")/catalog/cd[country = $c])}
p/>
</p
}
</body
</html /
Output:
/ Output:
/
html
body
h2My CD Collection</h2
p
Title: span style="color:#ff0000">Empire Burlesque</spanbr
Artist: span style="color:#00ff00">Bob Dylan</spanbr</p
p
Title: span style="color:#ff0000">Hide your heart</spanbr
Artist: span style="color:#00ff00">Bonnie Tyler</spanbr</p
p



</body
</html / html
body
h2Cd Collection</h2
p
USA
li
p
Title:
span style="color:#ff0000">Empire Burlesque</span
br />
Artist:
span style="color:#00ff00">Bob Dylan</span
</p
</li
li
p
Title:
span style="color:#ff0000">Greatest Hits</span
br />
Artist:
span style="color:#00ff00">Dolly Parton</span
</p
</li


CD# = 7
p />
</p


</body
</html /

References

XQuery 1.0 and XPath 2.0 Functions and Operators

http://www.w3.org/TR/xquery-operators/

XQuery 1.0: An XML Query Language

http://www.w3.org/TR/2005/WD-xquery-20050915/

XSL Transformations (XSLT) Version 2.0

http://www.w3.org/TR/2005/WD-xslt20-20050915/

XSLT Tutorial

http://www.w3schools.com/xsl/default.asp

XQuery Tutorial

http://www.w3schools.com/xquery/default.asp

Jeni's XSLT Pages.

Tutorials

http://www.jenitennison.com/xslt/index.html