XSLTExamples

advertisement
1
DATA FILE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bookshop [
<!ELEMENT bookshop (book+)>
<!ELEMENT book (title, author, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT author (initials, surname)>
<!ELEMENT initials (#PCDATA)>
<!ELEMENT surname (#PCDATA)>
<!ATTLIST book
type (comic | novel) #REQUIRED
>
]>
<bookshop>
<book type="novel">
<title>Harry Potter and the Sorcerer Stone</title>
<author>
<initials>JK</initials>
<surname>Rowling</surname>
</author>
<price>$16.95</price>
</book>
<book type="comic">
<title>Spiderman</title>
<author>
<initials>S</initials>
<surname>Lee</surname>
</author>
<price>$10.00</price>
</book>
<book type="comic">
<title>X-Men</title>
<author>
<initials>S</initials>
<surname>Lee</surname>
</author>
<price>$10.00</price>
</book>
</bookshop>
2
FILE: templateAll.xsl
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h1>Monash Bookshop</h1>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="bookshop/book">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="title">
<h2>Book Title</h2>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="author">
<h2>Author</h2>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="price">
<h4>Price</h4>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
3
FILE: NoAuthorWrong.xsl
<?xml version="1.0"?>
<!-- Query: list all the titles and prices-->
<!--Incorrect used of apply templates -->
<!-- Apply-templates without attribute "select" will
processes all the child-elements-->
<!-- Notice in the output that the Author contents are
displayed although there is no Author template-->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h1>Monash Bookshop</h1>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="bookshop/book">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="title">
<h2>Book Title</h2>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="price">
<h4>Price</h4>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
4
FILE: NoAuthorCorrectHTML.xsl
<?xml version="1.0"?>
<!-- Query: List all the title and price -->
<!-- The correct version of the noAuthorWrong.xsl-->
<!-- The apply-templates uses the attribute "select"
specify required child-elements, title and price-->
<!-- The output is HTML document-->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h1>Monash Bookshop</h1>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="bookshop/book">
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="price"/>
</xsl:template>
<xsl:template match="title">
<h2>Book Title</h2>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="price">
<h2>Price</h2>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
to
5
FILE: NoAuthorCorrectXML.xsl
<?xml version="1.0"?>
<!-- The query is the same as in templateNoAuthor.xsl,
however, the output in this example is XML document-->
<!-- Take notice on the element names created in this
script-->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml"/>
<xsl:template match="/">
<!-- Create an element called 'bookshop',
the xsl:element is used to creat element -->
<xsl:element name="bookshop">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="bookshop/book">
<xsl:element name="book_title">
<xsl:value-of select="title"/>
</xsl:element>
<xsl:element name="current_price">
<xsl:value-of select="price"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
6
FILE: callTemplateWrong.xsl
<?xml version="1.0"?>
<!-- Query: List the surname of all author-->
<!-- This is an incorrect example of "call-template"-->
<!-- call-tempate does not change the context node, the
"match" attribute is ignored-->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h1>Monash Bookshop</h1>
<h2>Authors Surname</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="bookshop/book">
<xsl:apply-templates select="author"/>
</xsl:template>
<xsl:template match="author">
<xsl:call-template name="authorSurname"/>
</xsl:template>
<xsl:template name="authorSurname" match="surname">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
7
FILE: callTemplate.xsl
<?xml version="1.0"?>
<!-- Query: List the surname of all author-->
<!-- This is a correct example of "call-template"-->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h1>Monash Bookshop</h1>
<h2>Authors Surname</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="bookshop/book">
<xsl:apply-templates select="author"/>
</xsl:template>
<xsl:template match="author">
<xsl:call-template name="authorSurname"/>
</xsl:template>
<xsl:template name="authorSurname">
<p>
<xsl:value-of select="surname"/>
</p>
</xsl:template>
</xsl:stylesheet>
8
FILE: sortTitle.xsl
<?xml version="1.0"?>
<!-- Query: list all author's surname and book title,
sorted in ascending order of author's surname-->
<!-- The default ordering in sorting is "descending"-->
<!-- sort is always called within an apply-templates-->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h1>Monash Bookshop</h1>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="bookshop">
<xsl:apply-templates select="book">
<xsl:sort select="author/surname" order="ascending"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="book">
<p>
<xsl:value-of select="author/surname"/>,
<I><xsl:value-of select="title"/></I>
</p>
</xsl:template>
</xsl:stylesheet>
9
FILE: numberSortWrong.xsl
<?xml version="1.0"?>
<!-- Query: list all the title and price for all books in
the bookshop, sorted in asceding order of price-->
<!-- This is incorrect example of sorting number, the
attribute "data-type" is not declared, hence the price is
compared as text value instead of number-->
<!-- Attribute "data-type" needs to be declared as "number"
when sorting number. The default value is "text"-->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h1>Monash Bookshop</h1>
<h2>Price List</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="bookshop">
<xsl:apply-templates select="book">
<xsl:sort select="price" order="ascending"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="book">
<p>
<b><xsl:value-of select="title"/></b>,
<xsl:value-of select="price"/>
</p>
</xsl:template>
</xsl:stylesheet>
10
FILE: numberSort.xsl
<?xml version="1.0"?>
<!-- Query: list all the title and price for all books in
the bookshop, sorted in asceding order of price-->
<!-- This is incorrect example of sorting number, the
attribute "data-type" is not declared, hence the price is
compared as text value instead of number-->
<!-- Attribute "data-type" needs to be declared as "number"
when sorting number. The default value is "text"-->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h1>Monash Bookshop</h1>
<h2>Price List</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="bookshop">
<xsl:apply-templates select="book">
<xsl:sort select="price" order="ascending"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="book">
<p>
<b><xsl:value-of select="title"/></b>,
<xsl:value-of select="price"/>
</p>
</xsl:template>
</xsl:stylesheet>
11
FILE: usingIf.xsl
<?xml version="1.0"?>
<!-- Query: Display all the book title authored by Rowling->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h1>Monash Bookshop</h1>
<h2> Book written by Rowling </h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="bookshop">
<xsl:apply-templates select="book"/>
</xsl:template>
<xsl:template match="book">
<!-- Notice the use of single quote, quotes need to be
alternated between double and single quote if two
or more quotes are required -->
<xsl:if test="author/surname='Rowling'">
<xsl:value-of select="title"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
12
FILE:usingFunction.xsl
<?xml version="1.0"?>
<!-- Query: Display all the book title authored by author
with surname start with 'L'-->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h1>Monash Bookshop</h1>
<h2> Books Written by author with a surname starts with
L</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="bookshop">
<xsl:apply-templates select="book"/>
</xsl:template>
<xsl:template match="book">
<!-- Notice the use of single quote, quotes need to be
alternated between double and single quote if two
or more quotes are required -->
<!-- Function substring is used to get the first
character of the surname -->
<xsl:if test="substring(author/surname,1,1)='L'">
<p>
<xsl:value-of select="title"/>
</p>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
13
<?xml version="1.0"?>
<!-- Query: Create a price list with all the comic books
listed first -->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:element name="priceList">
<xsl:for-each select="bookshop/book">
<xsl:if test="@type='comic'">
<xsl:element name="comicBook">
<xsl:value-of select="title"/>,
<xsl:value-of select="price"/>
</xsl:element>
</xsl:if>
</xsl:for-each>
<xsl:for-each select="bookshop/book">
<xsl:if test="@type='novel'">
<xsl:element name="Novel">
<xsl:value-of select="title"/>,
<xsl:value-of select="price"/>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Download