Project

Profile

Help

How to connect?
Download (4.39 KB) Statistics
| Branch: | Revision:

he / src / main / c / samples / data / books.xsl @ 55b80284

1
<xsl:transform
2
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3
  version="2.0">
4
  
5
  <!-- This style sheet displays the books.xml file.  -->
6
  
7
  <xsl:key name="authkey" match="ITEM" use="AUTHOR"/>
8
  <xsl:key name="codekey" match="CATEGORY" use="@CODE"/>
9
  
10
  <xsl:variable name="categories" select="//CATEGORY"/>
11
  
12
  <xsl:variable name="now" select="current-dateTime()"/>
13
  
14
  <xsl:param name="top-author">Jasper Fforde</xsl:param>
15
  
16
  <xsl:param name="title">Book List</xsl:param>
17
  
18
  <xsl:template match="/">
19
    <html>
20
      <xsl:comment>Generated at <xsl:value-of select="$now"/></xsl:comment>
21
      <xsl:comment><xsl:copy-of select="unparsed-text('books.txt')"/></xsl:comment>
22
      
23
      <xsl:call-template name="header">
24
        <xsl:with-param name="title" select="$title"/>
25
      </xsl:call-template>
26
      
27
      <body leftmargin="100" >
28
        <xsl:apply-templates/>
29
      </body>
30
    </html>
31
  </xsl:template>
32
  
33
  <xsl:template name="header">
34
    <xsl:param name="title" select="'Default Title'"/>
35
    <head>
36
      <xsl:choose>
37
        <xsl:when test="exists($title)">
38
          <title><xsl:value-of select="$title"/></title>
39
        </xsl:when>
40
        <xsl:otherwise>
41
          <title>Untitled</title>
42
        </xsl:otherwise>
43
      </xsl:choose>
44
    </head>
45
  </xsl:template>
46
  
47
  <xsl:template match="BOOKLIST">
48
    
49
    <h2>This week's top author is <xsl:value-of select="$top-author"/></h2>
50
    <xsl:variable name="top-authors-books" select="key('authkey', $top-author)"/>
51
    
52
    <xsl:if test="$top-authors-books">
53
      <p>We stock the following <xsl:value-of select="count($top-authors-books)"/> books by this author:</p>
54
      <ul>
55
        <xsl:for-each select="$top-authors-books">
56
          <li><xsl:value-of select="TITLE"/></li>
57
        </xsl:for-each>
58
      </ul>
59
      
60
      <p>This author has written books in the following categories:</p>
61
      <ul>
62
        <xsl:for-each select="key('codekey', $top-authors-books/@CAT)/@DESC">
63
          <li><xsl:value-of select="."/></li>
64
        </xsl:for-each>
65
      </ul>
66
      
67
      <p>The average price of these books is: 
68
        <xsl:value-of select="format-number(avg($top-authors-books/PRICE),'$####.00')"/>
69
      </p>
70
    </xsl:if>
71
    
72
    <h2>A complete list of books, grouped by author</h2>
73
    <xsl:apply-templates select="BOOKS" mode="by-author"/>
74
    
75
    <h2>A complete list of books, grouped by category</h2>
76
    <xsl:apply-templates select="BOOKS" mode="by-category"/>
77
    
78
    <h2>List of categories</h2>
79
    <xsl:apply-templates select="$categories">
80
      <xsl:sort select="@DESC" order="descending"/>
81
      <xsl:sort select="@CODE" order="descending"/>
82
    </xsl:apply-templates>
83
    
84
  </xsl:template>   
85
  
86
  <xsl:template match="BOOKS" mode="by-author">
87
    <div>
88
      <xsl:for-each-group select="ITEM" group-by="AUTHOR">
89
        <xsl:sort select="AUTHOR" order="ascending"/>
90
        <xsl:sort select="TITLE" order="ascending"/>
91
        <h3>AUTHOR: <xsl:value-of select="AUTHOR"/></h3>
92
        <table>
93
          <xsl:for-each select="current-group()">
94
            <tr>
95
              <td width="100" valign="top"><xsl:number value="position()" format="i"/></td>
96
              <td>
97
                TITLE: <xsl:value-of select="TITLE"/><br/>
98
                CATEGORY: <xsl:value-of select="id(@CAT)/@DESC" />
99
                (<xsl:value-of select="@CAT" />)
100
              </td>
101
            </tr>
102
          </xsl:for-each>
103
        </table>
104
        <hr/>
105
      </xsl:for-each-group>
106
    </div>
107
  </xsl:template>
108
  
109
  <xsl:template match="BOOKS" mode="by-category">
110
    <div>
111
      <xsl:for-each-group select="ITEM" group-by="@CAT">
112
        <xsl:sort select="id(@CAT)/@DESC" order="ascending"/>
113
        <xsl:sort select="TITLE" order="ascending"/>
114
        <h3>CATEGORY: <xsl:value-of select="id(@CAT)/@DESC" /></h3>
115
        <ol>
116
          <xsl:for-each select="current-group()">
117
            <li>AUTHOR: <xsl:value-of select="AUTHOR"/><br/>
118
              TITLE: <xsl:value-of select="TITLE"/>
119
            </li>                
120
          </xsl:for-each>
121
        </ol>
122
        <hr/>
123
      </xsl:for-each-group>
124
    </div>
125
  </xsl:template>
126
  
127
  <xsl:template match="CATEGORY" >
128
    <h4>CATEGORY <xsl:number value="position()" format="I"/></h4>
129
    <table>
130
      <xsl:for-each select="@*">
131
        <tr>
132
          <td><xsl:value-of select="name(.)"/></td>
133
          <td><xsl:value-of select="."/></td>
134
        </tr>
135
      </xsl:for-each>
136
    </table>
137
    <hr/>
138
  </xsl:template>
139
  
140
</xsl:transform>	
(6-6/19)