Creating Links and Cross-References(Chapter 6 of XSLT 2nd Edition)
The key() function returns a node-set from the document, using the index specified by an <xsl:key> element.
cdcatalog.xml
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!-- Edited by XMLSpy® -->
3 <catalog>
4 <cd>
5 <title>Empire Burlesque</title>
6 <artist>Bob Dylan</artist>
7 <country>USA</country>
8 <company>Columbia</company>
9 <price>10.90</price>
10 <year>1985</year>
11 </cd>
12 <cd>
13 <title>Hide your heart</title>
14 <artist>Bonnie Tyler</artist>
15 <country>UK</country>
16 <company>CBS Records</company>
17 <price>9.90</price>
18 <year>1988</year>
19 </cd>
20 <cd>
21 <title>Greatest Hits</title>
22 <artist>Dolly Parton</artist>
23 <country>USA</country>
24 <company>RCA</company>
25 <price>9.90</price>
26 <year>1982</year>
27 </cd>
28 <cd>
29 <title>Still got the blues</title>
30 <artist>Gary Moore</artist>
31 <country>UK</country>
32 <company>Virgin records</company>
33 <price>10.20</price>
34 <year>1990</year>
35 </cd>
36 <cd>
37 <title>Eros</title>
38 <artist>Eros Ramazzotti</artist>
39 <country>EU</country>
40 <company>BMG</company>
41 <price>9.90</price>
42 <year>1997</year>
43 </cd>
44 <cd>
45 <title>One night only</title>
46 <artist>Bee Gees</artist>
47 <country>UK</country>
48 <company>Polydor</company>
49 <price>10.90</price>
50 <year>1998</year>
51 </cd>
52 <cd>
53 <title>Sylvias Mother</title>
54 <artist>Dr.Hook</artist>
55 <country>UK</country>
56 <company>CBS</company>
57 <price>8.10</price>
58 <year>1973</year>
59 </cd>
60 <cd>
61 <title>Maggie May</title>
62 <artist>Rod Stewart</artist>
63 <country>UK</country>
64 <company>Pickwick</company>
65 <price>8.50</price>
66 <year>1990</year>
67 </cd>
68 <cd>
69 <title>Romanza</title>
70 <artist>Andrea Bocelli</artist>
71 <country>EU</country>
72 <company>Polydor</company>
73 <price>10.80</price>
74 <year>1996</year>
75 </cd>
76 <cd>
77 <title>When a man loves a woman</title>
78 <artist>Percy Sledge</artist>
79 <country>USA</country>
80 <company>Atlantic</company>
81 <price>8.70</price>
82 <year>1987</year>
83 </cd>
84 <cd>
85 <title>Black angel</title>
86 <artist>Savage Rose</artist>
87 <country>EU</country>
88 <company>Mega</company>
89 <price>10.90</price>
90 <year>1995</year>
91 </cd>
92 <cd>
93 <title>1999 Grammy Nominees</title>
94 <artist>Many</artist>
95 <country>USA</country>
96 <company>Grammy</company>
97 <price>10.20</price>
98 <year>1999</year>
99 </cd>
100 <cd>
101 <title>For the good times</title>
102 <artist>Kenny Rogers</artist>
103 <country>UK</country>
104 <company>Mucik Master</company>
105 <price>8.70</price>
106 <year>1995</year>
107 </cd>
108 <cd>
109 <title>Big Willie style</title>
110 <artist>Will Smith</artist>
111 <country>USA</country>
112 <company>Columbia</company>
113 <price>9.90</price>
114 <year>1997</year>
115 </cd>
116 <cd>
117 <title>Tupelo Honey</title>
118 <artist>Van Morrison</artist>
119 <country>UK</country>
120 <company>Polydor</company>
121 <price>8.20</price>
122 <year>1971</year>
123 </cd>
124 <cd>
125 <title>Soulsville</title>
126 <artist>Jorn Hoel</artist>
127 <country>Norway</country>
128 <company>WEA</company>
129 <price>7.90</price>
130 <year>1996</year>
131 </cd>
132 <cd>
133 <title>The very best of</title>
134 <artist>Cat Stevens</artist>
135 <country>UK</country>
136 <company>Island</company>
137 <price>8.90</price>
138 <year>1990</year>
139 </cd>
140 <cd>
141 <title>Stop</title>
142 <artist>Sam Brown</artist>
143 <country>UK</country>
144 <company>A and M</company>
145 <price>8.90</price>
146 <year>1988</year>
147 </cd>
148 <cd>
149 <title>Bridge of Spies</title>
150 <artist>T`Pau</artist>
151 <country>UK</country>
152 <company>Siren</company>
153 <price>7.90</price>
154 <year>1987</year>
155 </cd>
156 <cd>
157 <title>Private Dancer</title>
158 <artist>Tina Turner</artist>
159 <country>UK</country>
160 <company>Capitol</company>
161 <price>8.90</price>
162 <year>1983</year>
163 </cd>
164 <cd>
165 <title>Midt om natten</title>
166 <artist>Kim Larsen</artist>
167 <country>EU</country>
168 <company>Medley</company>
169 <price>7.80</price>
170 <year>1983</year>
171 </cd>
172 <cd>
173 <title>Pavarotti Gala Concert</title>
174 <artist>Luciano Pavarotti</artist>
175 <country>UK</country>
176 <company>DECCA</company>
177 <price>9.90</price>
178 <year>1991</year>
179 </cd>
180 <cd>
181 <title>The dock of the bay</title>
182 <artist>Otis Redding</artist>
183 <country>USA</country>
184 <company>Atlantic</company>
185 <price>7.90</price>
186 <year>1987</year>
187 </cd>
188 <cd>
189 <title>Picture book</title>
190 <artist>Simply Red</artist>
191 <country>EU</country>
192 <company>Elektra</company>
193 <price>7.20</price>
194 <year>1985</year>
195 </cd>
196 <cd>
197 <title>Red</title>
198 <artist>The Communards</artist>
199 <country>UK</country>
200 <company>London</company>
201 <price>7.80</price>
202 <year>1987</year>
203 </cd>
204 <cd>
205 <title>Unchain my heart</title>
206 <artist>Joe Cocker</artist>
207 <country>USA</country>
208 <company>EMI</company>
209 <price>8.20</price>
210 <year>1987</year>
211 </cd>
212 </catalog>
2 <!-- Edited by XMLSpy® -->
3 <catalog>
4 <cd>
5 <title>Empire Burlesque</title>
6 <artist>Bob Dylan</artist>
7 <country>USA</country>
8 <company>Columbia</company>
9 <price>10.90</price>
10 <year>1985</year>
11 </cd>
12 <cd>
13 <title>Hide your heart</title>
14 <artist>Bonnie Tyler</artist>
15 <country>UK</country>
16 <company>CBS Records</company>
17 <price>9.90</price>
18 <year>1988</year>
19 </cd>
20 <cd>
21 <title>Greatest Hits</title>
22 <artist>Dolly Parton</artist>
23 <country>USA</country>
24 <company>RCA</company>
25 <price>9.90</price>
26 <year>1982</year>
27 </cd>
28 <cd>
29 <title>Still got the blues</title>
30 <artist>Gary Moore</artist>
31 <country>UK</country>
32 <company>Virgin records</company>
33 <price>10.20</price>
34 <year>1990</year>
35 </cd>
36 <cd>
37 <title>Eros</title>
38 <artist>Eros Ramazzotti</artist>
39 <country>EU</country>
40 <company>BMG</company>
41 <price>9.90</price>
42 <year>1997</year>
43 </cd>
44 <cd>
45 <title>One night only</title>
46 <artist>Bee Gees</artist>
47 <country>UK</country>
48 <company>Polydor</company>
49 <price>10.90</price>
50 <year>1998</year>
51 </cd>
52 <cd>
53 <title>Sylvias Mother</title>
54 <artist>Dr.Hook</artist>
55 <country>UK</country>
56 <company>CBS</company>
57 <price>8.10</price>
58 <year>1973</year>
59 </cd>
60 <cd>
61 <title>Maggie May</title>
62 <artist>Rod Stewart</artist>
63 <country>UK</country>
64 <company>Pickwick</company>
65 <price>8.50</price>
66 <year>1990</year>
67 </cd>
68 <cd>
69 <title>Romanza</title>
70 <artist>Andrea Bocelli</artist>
71 <country>EU</country>
72 <company>Polydor</company>
73 <price>10.80</price>
74 <year>1996</year>
75 </cd>
76 <cd>
77 <title>When a man loves a woman</title>
78 <artist>Percy Sledge</artist>
79 <country>USA</country>
80 <company>Atlantic</company>
81 <price>8.70</price>
82 <year>1987</year>
83 </cd>
84 <cd>
85 <title>Black angel</title>
86 <artist>Savage Rose</artist>
87 <country>EU</country>
88 <company>Mega</company>
89 <price>10.90</price>
90 <year>1995</year>
91 </cd>
92 <cd>
93 <title>1999 Grammy Nominees</title>
94 <artist>Many</artist>
95 <country>USA</country>
96 <company>Grammy</company>
97 <price>10.20</price>
98 <year>1999</year>
99 </cd>
100 <cd>
101 <title>For the good times</title>
102 <artist>Kenny Rogers</artist>
103 <country>UK</country>
104 <company>Mucik Master</company>
105 <price>8.70</price>
106 <year>1995</year>
107 </cd>
108 <cd>
109 <title>Big Willie style</title>
110 <artist>Will Smith</artist>
111 <country>USA</country>
112 <company>Columbia</company>
113 <price>9.90</price>
114 <year>1997</year>
115 </cd>
116 <cd>
117 <title>Tupelo Honey</title>
118 <artist>Van Morrison</artist>
119 <country>UK</country>
120 <company>Polydor</company>
121 <price>8.20</price>
122 <year>1971</year>
123 </cd>
124 <cd>
125 <title>Soulsville</title>
126 <artist>Jorn Hoel</artist>
127 <country>Norway</country>
128 <company>WEA</company>
129 <price>7.90</price>
130 <year>1996</year>
131 </cd>
132 <cd>
133 <title>The very best of</title>
134 <artist>Cat Stevens</artist>
135 <country>UK</country>
136 <company>Island</company>
137 <price>8.90</price>
138 <year>1990</year>
139 </cd>
140 <cd>
141 <title>Stop</title>
142 <artist>Sam Brown</artist>
143 <country>UK</country>
144 <company>A and M</company>
145 <price>8.90</price>
146 <year>1988</year>
147 </cd>
148 <cd>
149 <title>Bridge of Spies</title>
150 <artist>T`Pau</artist>
151 <country>UK</country>
152 <company>Siren</company>
153 <price>7.90</price>
154 <year>1987</year>
155 </cd>
156 <cd>
157 <title>Private Dancer</title>
158 <artist>Tina Turner</artist>
159 <country>UK</country>
160 <company>Capitol</company>
161 <price>8.90</price>
162 <year>1983</year>
163 </cd>
164 <cd>
165 <title>Midt om natten</title>
166 <artist>Kim Larsen</artist>
167 <country>EU</country>
168 <company>Medley</company>
169 <price>7.80</price>
170 <year>1983</year>
171 </cd>
172 <cd>
173 <title>Pavarotti Gala Concert</title>
174 <artist>Luciano Pavarotti</artist>
175 <country>UK</country>
176 <company>DECCA</company>
177 <price>9.90</price>
178 <year>1991</year>
179 </cd>
180 <cd>
181 <title>The dock of the bay</title>
182 <artist>Otis Redding</artist>
183 <country>USA</country>
184 <company>Atlantic</company>
185 <price>7.90</price>
186 <year>1987</year>
187 </cd>
188 <cd>
189 <title>Picture book</title>
190 <artist>Simply Red</artist>
191 <country>EU</country>
192 <company>Elektra</company>
193 <price>7.20</price>
194 <year>1985</year>
195 </cd>
196 <cd>
197 <title>Red</title>
198 <artist>The Communards</artist>
199 <country>UK</country>
200 <company>London</company>
201 <price>7.80</price>
202 <year>1987</year>
203 </cd>
204 <cd>
205 <title>Unchain my heart</title>
206 <artist>Joe Cocker</artist>
207 <country>USA</country>
208 <company>EMI</company>
209 <price>8.20</price>
210 <year>1987</year>
211 </cd>
212 </catalog>
xslt 1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!-- Edited by XMLSpy® -->
3 <xsl:stylesheet version="1.0"
4 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
5
6 <xsl:key name="cdlist" match="cd" use="title" />
7
8 <xsl:template match="/">
9 <html>
10 <body>
11 <xsl:for-each select="key('cdlist', 'Empire Burlesque')">
12 <p>
13 Title: <xsl:value-of select="title" />
14 <br />
15 Artist: <xsl:value-of select="artist" />
16 <br />
17 Price: <xsl:value-of select="price" />
18 </p>
19 </xsl:for-each>
20 </body>
21 </html>
22 </xsl:template>
23
24 </xsl:stylesheet>
2 <!-- Edited by XMLSpy® -->
3 <xsl:stylesheet version="1.0"
4 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
5
6 <xsl:key name="cdlist" match="cd" use="title" />
7
8 <xsl:template match="/">
9 <html>
10 <body>
11 <xsl:for-each select="key('cdlist', 'Empire Burlesque')">
12 <p>
13 Title: <xsl:value-of select="title" />
14 <br />
15 Artist: <xsl:value-of select="artist" />
16 <br />
17 Price: <xsl:value-of select="price" />
18 </p>
19 </xsl:for-each>
20 </body>
21 </html>
22 </xsl:template>
23
24 </xsl:stylesheet>
http://www.w3schools.com/xsl/cdcatalog_key.xml
The generate-id() function returns a string value that uniquely identifies a specified node.
If the node-set specified is empty, an empty string is returned. If you omit the node-set parameter, it defaults to the current node.
xslt 1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!-- Edited by XMLSpy® -->
3 <xsl:stylesheet version="1.0"
4 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
5
6 <xsl:template match="/">
7 <html>
8 <body>
9 <h3>Artists:</h3>
10 <ul>
11 <xsl:for-each select="catalog/cd">
12 <li>
13 <a href="#{generate-id(artist)}">
14 <xsl:value-of select="artist" /></a>
15 </li>
16 </xsl:for-each>
17 </ul>
18 <hr />
19 <xsl:for-each select="catalog/cd">
20 Artist: <a name="{generate-id(artist)}">
21 <xsl:value-of select="artist" /></a>
22 <br />
23 Title: <xsl:value-of select="title" />
24 <br />
25 Price: <xsl:value-of select="price" />
26 <hr />
27 </xsl:for-each>
28 </body>
29 </html>
30 </xsl:template>
2 <!-- Edited by XMLSpy® -->
3 <xsl:stylesheet version="1.0"
4 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
5
6 <xsl:template match="/">
7 <html>
8 <body>
9 <h3>Artists:</h3>
10 <ul>
11 <xsl:for-each select="catalog/cd">
12 <li>
13 <a href="#{generate-id(artist)}">
14 <xsl:value-of select="artist" /></a>
15 </li>
16 </xsl:for-each>
17 </ul>
18 <hr />
19 <xsl:for-each select="catalog/cd">
20 Artist: <a name="{generate-id(artist)}">
21 <xsl:value-of select="artist" /></a>
22 <br />
23 Title: <xsl:value-of select="title" />
24 <br />
25 Price: <xsl:value-of select="price" />
26 <hr />
27 </xsl:for-each>
28 </body>
29 </html>
30 </xsl:template>
http://www.w3schools.com/xsl/cdcatalog_generateid.xml

浙公网安备 33010602011771号