Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Basics Of XSL

 

 

     
    <xsl:template> is a set of rules to be applied when a node is matched
    match="/"          is used along with the template tag to specify on which node to apply the template on , if it is “/” it has to be applied on the entire document.
    <xsl:value-of> used to get the value of a tag.
    <xsl:for-each> used to iterate over a list of nodes
    filter the output  
    <xsl:sort>       used to sort the nodes based on a tag
    <xsl:if> used to test the condition on a node
    <xsl:choose>  
    <xsl:when>  
    <xsl:otherwise>  
    <xsl:apply-templates>   

Sample XML

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
</book>
</catalog>



XSL To Transform

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:template match="/">
<table border="1">
<tr>
<td>Title</td> <td>genre</td> <td>author</td> <td>price</td>
</tr>
<xsl:for-each select="/catalog/book">
<xsl:sort select="title"/>
<tr>
<td>
<xsl:value-of select="./title"/>
</td>
<td>
<xsl:value-of select="./genre"/>
</td>
<td>
<xsl:value-of select="./author"/>
</td>
<td>
<xsl:if test="price &gt; 10">
<font color="Red"></font>
</xsl:if>
<xsl:value-of select="./price"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>



Output


 























Titlegenreauthorprice
Maeve AscendantFantasyCorets, Eva5.95
Midnight RainFantasyRalls, Kim5.95
XML Developer's GuideComputerGambardella, Matthew44.95

No comments:

Post a Comment

Search 24 Bytes