XSLT xsl:for-each Element
xsl:for-each The element allows you to loop in XSLT.
xsl:for-each Element
XSL xsl:for-each The element can be used to select each XML element in a specified node-set:
Example
<
xsl:stylesheet
version
=
"
1.0
"
xmlns:xsl
=
"
http://www.w3.org/1999/XSL/Transform
"
<
xsl:template
match
=
"
/
"
<
html
<
body
<
h2My CD Collection
h2
<
table
border
=
"
1
"
<
tr
bgcolor
=
"
#9acd32
"
<
th
Title
th
<
th
Artist
th
tr
<
xsl:for-each
select
=
"
catalog/cd
"
<
tr
<
td
<
xsl:value-of
select
=
"
title
"
/>
td
<
td
<
xsl:value-of
select
=
"
artist
"
/>
td
tr
xsl:for-each
table
body
html
xsl:template
xsl:stylesheetTry it »
**Note:**select The value of the attribute is an XPath expression. This XPath expression works like locating a file system, in which a forward slash (/) selects a subdirectory.
Filter the Output Result
By adding the .img-responsive class to the xsl:for-each By adding a predicate with a selection attribute to the element, we can also filter the results output from the XML file.
Valid filter operators:
- = (equal to)
- != (not equal to)
- < (less-than)
- > (greater than)
Look at the adjusted XSL stylesheet:
Example
<
xsl:stylesheet
version
=
"
1.0
"
xmlns:xsl
=
"
http://www.w3.org/1999/XSL/Transform
"
<
xsl:template
match
=
"
/
"
<
html
<
body
<
h2My CD Collection
h2
<
table
border
=
"
1
"
<
tr
bgcolor
=
"
#9acd32
"
<
th
Title
th
<
th
Artist
th
tr
<
xsl:for-each
select
=
"catalog/cd[artist='Bob Dylan']
"
<
tr
<
td
<
xsl:value-of
select
=
"
title
"
/>
td
<
td
<
xsl:value-of
select
=
"
artist
"
/>
td
tr
xsl:for-each
table
body
html
xsl:template
xsl:stylesheetTry it »