Transformation:
<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:template match="/siteMap"> <!-- This value will be used if a node does not have the 'id' attribute or the 'rank' query param --> <xsl:variable name="Today" select="'2007-07-16'"/> <urlset> <xsl:for-each select="//siteMapNode"> <xsl:sort select="@url" data-type="text" order="ascending"/> <xsl:if test="@priority"> <url> <!-- Replace '~/' with 'http://www.wbswiki.com/' --> <loc> <xsl:call-template name="string-replace"> <xsl:with-param name="text" select="@url"/> <xsl:with-param name="from" select="'~/'"/> <xsl:with-param name="to" select="'http://www.wbswiki.com/'" /> </xsl:call-template> </loc> <!-- Obtain the date or the rank --> <lastmod> <xsl:variable name="DateId" select="@id"/> <xsl:choose> <xsl:when test="/siteMap/dates/date[@id=$DateId]/@date and starts-with(@url,'~/Games')"> <xsl:variable name="Date1" select="/siteMap/dates/date[@id=$DateId]/@date"/> <xsl:value-of select="substring($Date1,7,4)"/>-<xsl:value-of select="substring($Date1,4,2)"/>-<xsl:value-of select="substring($Date1,1,2)"/> </xsl:when> <xsl:when test="substring-after(@url,'.aspx?')"> <xsl:variable name="Rank" select="substring-after(@url, 'rank=')"/> <xsl:variable name="Text" select="/siteMap/ranks/rank[@id=$Rank]/@text"/> <xsl:value-of select="$Text" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="$Today"/> </xsl:otherwise> </xsl:choose> </lastmod> <!-- priority --> <priority> <xsl:value-of select="@priority"/> </priority> </url> </xsl:if> </xsl:for-each> </urlset> </xsl:template> <xsl:template match="*" mode="show-ancestry"> <xsl:if test="@title"> <xsl:call-template name="string-replace"> <xsl:with-param name="string" select="@title"/> <xsl:with-param name="from" select="'&nbsp;'"/> <xsl:with-param name="to"> <xsl:text> </xsl:text> </xsl:with-param> </xsl:call-template> </xsl:if> </xsl:template> <!-- replace all occurences of the character(s) 'from' by 'to' in the string 'text' --> <xsl:template name="string-replace" > <xsl:param name="text"/> <xsl:param name="from"/> <xsl:param name="to"/> <xsl:choose> <xsl:when test="contains($text,$from)"> <xsl:value-of select="substring-before($text,$from)"/> <xsl:copy-of select="$to"/> <xsl:call-template name="string-replace"> <xsl:with-param name="string" select="substring-after($text,$from)"/> <xsl:with-param name="from" select="$from"/> <xsl:with-param name="to" select="$to"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$text"/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
A sample XML file:
<?xml version="1.0" encoding="utf-16" ?> <siteMap> <siteMapNode url="~/default.aspx" priority="1.0" /> <siteMapNode url="~/products.aspx" priority="0.9"> <siteMapNode url="~/software.aspx" priority="0.4"> <siteMapNode url="~/dummy.aspx" /> <siteMapNode id="3" url="~/Games/MissileCommand.aspx" priority="0.7" /> <siteMapNode id="6" url="~/StarDemo.aspx" priority="0.3" /> </siteMapNode> <siteMapNode url="~/hardware.aspx" priority="0.4"> <siteMapNode url="~/PocketPC.aspx?rank=1" priority="0.2" /> <siteMapNode url="~/TabletPC.aspx?rank=2" priority="0.8" /> </siteMapNode> </siteMapNode> <dates> <date date="24/04/2012" id="3" /> <date date="09/07/2012" id="6" /> </dates> <ranks> <rank id="1" text="bad" /> <rank id="2" text="good" /> </ranks> </siteMap>
Output:
<?xml version="1.0" encoding="utf-8" ?> <urlset> <url> <loc>http://www.wbswiki.com/default.aspx</loc> <lastmod>2007-07-16</lastmod> <priority>1.0</priority> </url> <url> <loc>http://www.wbswiki.com/Games/MissileCommand.aspx</loc> <lastmod>2012-04-24</lastmod> <priority>0.7</priority> </url> <url> <loc>http://www.wbswiki.com/hardware.aspx</loc> <lastmod>2007-07-16</lastmod> <priority>0.4</priority> </url> <url> <loc>http://www.wbswiki.com/PocketPC.aspx?rank=1</loc> <lastmod>bad</lastmod> <priority>0.2</priority> </url> <url> <loc>http://www.wbswiki.com/products.aspx</loc> <lastmod>2007-07-16</lastmod> <priority>0.9</priority> </url> <url> <loc>http://www.wbswiki.com/software.aspx</loc> <lastmod>2007-07-16</lastmod> <priority>0.4</priority> </url> <url> <loc>http://www.wbswiki.com/StarDemo.aspx</loc> <lastmod>2007-07-16</lastmod> <priority>0.3</priority> </url> <url> <loc>http://www.wbswiki.com/TabletPC.aspx?rank=2</loc> <lastmod>good</lastmod> <priority>0.8</priority> </url> </urlset>
You can download the entire example from here.
The following transformation converts a list of movies and scenes into a web page with thumbnails:
Transformation file catalog.xsl:
<?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:functx="http://www.functx.com"> <xsl:template match="/movies"> <html> <head> <style> * { font-family: verdana; } .title { font-size: 10pt; font-weight: bold; margin-bottom: 5px; } .scene { font-size: 10pt; } </style> </head> <body> <xsl:for-each select="//movie"> <div class="title"> <xsl:value-of select="@title"/> </div> <div> <img border="0"> <xsl:attribute name="src"> <xsl:value-of select="@code" />.jpg </xsl:attribute> </img> <img border="0"> <xsl:attribute name="src"> <xsl:value-of select="../@code" />.jpg </xsl:attribute> </img> </div> <br /> <xsl:for-each select="scene"> <span class="scene"> <xsl:value-of select="@name"/> </span> <br /> <xsl:for-each select="img"> <img border="0"> <xsl:attribute name="src"> <xsl:value-of select="../../@code" />-<xsl:value-of select="../@code" />_<xsl:call-template name="prepend-pad"> <xsl:with-param name="padChar"> <xsl:value-of select="0"/> </xsl:with-param> <xsl:with-param name="padVar"> <xsl:value-of select="."/> </xsl:with-param> <xsl:with-param name="length"> <xsl:value-of select="4"/> </xsl:with-param> </xsl:call-template>.jpg </xsl:attribute> <xsl:attribute name="vspace">5</xsl:attribute> </img>  </xsl:for-each> <br /> <xsl:call-template name="for.loop"> <xsl:with-param name="i">1</xsl:with-param> <xsl:with-param name="count"> <xsl:value-of select="@numimg" /> </xsl:with-param> </xsl:call-template> <br /> </xsl:for-each> <br /> </xsl:for-each> </body> </html> </xsl:template> <xsl:template name="for.loop"> <xsl:param name="i" /> <xsl:param name="count" /> <!--begin_: Line_by_Line_Output --> <xsl:if test="$i <= $count"> <img border="0"> <xsl:attribute name="src"> <xsl:value-of select="../@code" />-<xsl:value-of select="@code" />_<xsl:call-template name="prepend-pad"> <xsl:with-param name="padChar"> <xsl:value-of select="0"/> </xsl:with-param> <xsl:with-param name="padVar"> <xsl:value-of select="$i"/> </xsl:with-param> <xsl:with-param name="length"> <xsl:value-of select="4"/> </xsl:with-param> </xsl:call-template>.jpg </xsl:attribute> <xsl:attribute name="vspace">5</xsl:attribute> </img>  </xsl:if> <!--begin_: RepeatTheLoopUntilFinished--> <xsl:if test="$i <= $count"> <xsl:call-template name="for.loop"> <xsl:with-param name="i"> <xsl:value-of select="$i + 1"/> </xsl:with-param> <xsl:with-param name="count"> <xsl:value-of select="$count"/> </xsl:with-param> </xsl:call-template> </xsl:if> </xsl:template> <xsl:template name="prepend-pad"> <!-- recursive template to right justify and prepend--> <!-- the value with whatever padChar is passed in --> <xsl:param name="padChar"> </xsl:param> <xsl:param name="padVar"/> <xsl:param name="length"/> <xsl:choose> <xsl:when test="string-length($padVar) < $length"> <xsl:call-template name="prepend-pad"> <xsl:with-param name="padChar" select="$padChar"/> <xsl:with-param name="padVar" select="concat($padChar,$padVar)"/> <xsl:with-param name="length" select="$length"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="substring($padVar,string-length($padVar) - $length + 1)"/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
Input XML file catalog.xml:
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="catalog.xsl"?> <!-- for some browsers replace xml-stylesheet with xml:stylesheet --> <movies> <studio name="Pixar" code="logo_pixar"> <movie title="Gamera" code="gamera"> <scene name="scene#1" numimg="14" code="scene1"> <img>4</img> <img>5</img> <img>10</img> <img>12</img> </scene> <scene name="scene#2" numimg="7" code="scene2"> <img>6</img> <img>7</img> </scene> </movie> <movie title="White Dolphin Um" code="dolphin-um"> <scene name="scene#1" numimg="5" code="scene1" /> <scene name="scene#2" numimg="3" code="scene2" /> </movie> </studio> <studio name="Disney" code="logo_disney"> <movie title="Gucio i Cezar" code="gucio_cezar"> <scene name="scene#1: Gucio jest chory" numimg="4" code="scene1" /> </movie> </studio> </movies>
You can download the entire example from here.
The following transformation converts an XML document into another XML document. It uses lookup tables:
books.xsl:
<?xml version='1.0' encoding="utf-16"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output encoding="utf-16"></xsl:output> <xsl:template match="/"> <Books> <xsl:for-each select="//book"> <Book> <Title> <xsl:value-of select="amazonTitle"/> </Title> <PubDate> <xsl:value-of select="pubDate/month"/>/<xsl:value-of select="pubDate/day"/>/<xsl:value-of select="pubDate/year"/> </PubDate> <Publisher> <xsl:variable name="pubCode" select="publisher"/> <xsl:value-of select="document('lookup.xml')//publisher[@code=$pubCode]/@name"/> </Publisher> <Authors> <xsl:value-of select="amazonAuthors"/> </Authors> <ISBN> <xsl:value-of select="@isbn"/> </ISBN> <Categories> <xsl:for-each select="categories/category"> <xsl:variable name="catCode" select="."/> <xsl:value-of select="document('lookup.xml')//category[@code=$catCode]/@name"/> <xsl:if test="position() < last()">, </xsl:if> </xsl:for-each> </Categories> <Price> <xsl:choose> <xsl:when test="price[.!='']"> <xsl:value-of select="price"/> </xsl:when> <xsl:otherwise> 0.00 </xsl:otherwise> </xsl:choose> </Price> <Rating> <xsl:if test="rating[.!='']"> <xsl:value-of select="rating"/> </xsl:if> </Rating> <NumRatings> <xsl:if test="numRatings[.!='']"> <xsl:value-of select="numRatings"/> </xsl:if> </NumRatings> <NumPages> <xsl:choose> <xsl:when test="numPages[.!='']"> <xsl:value-of select="numPages"/> </xsl:when> <xsl:otherwise> 0 </xsl:otherwise> </xsl:choose> </NumPages> </Book> </xsl:for-each> </Books> </xsl:template> </xsl:stylesheet>
lookup.xml:
<?xml version="1.0" encoding="utf-8"?> <lookup> <publishers> <publisher code="aw" amazonName="addison-wesley professional" name="Addison-Wesley" /> <publisher code="ak" amazonName="a k peters/crc press" name="A K Peters/CRC Press" /> <publisher code="crm" amazonName="charles river media" name="Charles River Media" /> <publisher code="mit" amazonName="the mit press" name="The MIT Press" /> <publisher code="ms" amazonName="microsoft press" name="Microsoft Press" /> <publisher code="or" amazonName="oreilly media, inc." name="O'Reilly" /> <publisher code="ph" amazonName="prentice hall ptr" name="Prentice Hall" /> <publisher code="spri" amazonName="springer" name="Springer" /> <publisher code="wrox" amazonName="wrox" name="Wrox" /> </publishers> <categories> <category code="cpp" name="C++" /> <category code="eng" name="Software Engineering" /> <category code="games" name="Game Programming" /> <category code="win8" name="Windows 8 Programming" /> </categories> <series> <series code="aw-obj" name="The Addison-Wesley Object Technology Series" /> </series> </lookup>
books.xml (the source XML file):
<books> <book isbn="978-0735671768"> <amazonTitle>Programming Windows: Writing Windows 8 Apps With C# and XAML</amazonTitle> <pubDate> <year>2013</year> <month>2</month> <day>1</day> </pubDate> <publisher>ms</publisher> <amazonAuthors>Charles Petzold</amazonAuthors> <categories> <category>cpp</category> <category>win8</category> </categories> <price>40.21</price> <rating>4.6</rating> <numRatings>10</numRatings> <numPages>1136</numPages> </book> <book isbn="978-1568814131"> <amazonTitle>Game Engine Architecture</amazonTitle> <pubDate> <year>2009</year> <month>6</month> <day>15</day> </pubDate> <publisher>ak</publisher> <amazonAuthors>Jason Gregory</amazonAuthors> <categories> <category>eng</category> <category>games</category> </categories> <price>43.26</price> <rating>4.6</rating> <numRatings>33</numRatings> <numPages>864</numPages> </book> </books>
output.xml (the resulting XML file):
<?xml version="1.0" encoding="utf-16" ?> <Books> <Book> <Title>Programming Windows: Writing Windows 8 Apps With C# and XAML</Title> <PubDate>2/1/2013</PubDate> <Publisher>Microsoft Press</Publisher> <Authors>Charles Petzold</Authors> <ISBN>978-0735671768</ISBN> <Categories>C++, Windows 8 Programming</Categories> <Price>40.21</Price> <Rating>4.6</Rating> <NumRatings>10</NumRatings> <NumPages>1136</NumPages> </Book> <Book> <Title>Game Engine Architecture</Title> <PubDate>6/15/2009</PubDate> <Publisher>A K Peters/CRC Press</Publisher> <Authors>Jason Gregory</Authors> <ISBN>978-1568814131</ISBN> <Categories>Software Engineering, Game Programming</Categories> <Price>43.26</Price> <Rating>4.6</Rating> <NumRatings>33</NumRatings> <NumPages>864</NumPages> </Book> </Books>
The structure of the output XML file:
<?xml version="1.0" encoding="utf-8" ?> <Books> <Book> <Title>text</Title> <PubDate>00/00/0000</PubDate> <Publisher>text</Publisher> <Authors>auth1,auth2</Authors> <ISBN>text</ISBN> <Categories>cat1,cat2</Categories> <Price>00.00</Price> <Rating>0.00</Rating> <NumPages>000</NumPages> <NumRatings>000</NumRatings> <Image>text</Image> </Book> </Books>
You can download the entire example from here.