Creating Loops With The For-each Element In XSLT
Extensible Stylesheet Language (XSL) is a technology closely related to XML which is used to transform XML data. XML file are used to set out the ground-rules for a given body of data and to describe the data itself. XSL allows us to take the original XML data and convert it into information which can be displayed in a web browser window or opened with a text editor. XSL can also be used to create an XML file which is a modified version of an original XML file.
When displaying XML elements in a browser window, the XSL elements which enable looping through an XML tree and decision-making really come into their own. One of the key methods used in XSL for looping through elements in an XML file is the the "For-each" element. This requires the "select" attribute to specify which element is to be used in the loop. The "For-each" element normally requires both an opening and a closing tag and any lines of markup placed between the opening and closing tags will be repeated each time the element specified in the "select" attribute is encountered in the XML file.
For example, let's say we have an XML file containing a list of companies and the addresses of their websites. Let's also say that the element which we will be targeting in the XSL "For-each" statement is called "company" and that, within each company element, we have a "name", "telephone" and "website" element. Before beginning our "For-each" loop, we could place the opening tag of an HTML "ul" element (an unordered or bulleted list).
Inside the "For-each" loop, we could output an HTML "li" element and, between its opening and closing tags, output the contents of the "name" and "telephone" elements from the XML file. The data in the "website" element in the input XML could be used to convert the "name" into a clickable link.
As regards the appearance of the data in the resulting HTML output, this would be controlled by a linked CSS file. CSS can be used to format the output in any way we desire, so our bulleted list can take on pretty much any appearance we care to give it.
The writer of this article is a developer and trainer with Macresource Computer Training, a UK IT training company offering XML, XSLT, XPATH, XQuery and XSL-FO training courses in London and throughout the UK.
![]() |
Employee Training & Development Sale Price: $85.00 Used From: $94.92 |
|
Raymond Noe’s Employee Training and Development sets the standard in this course area. First introduced in 1998, ETD became the market-defining text within 6 months of publication. Its popularity is due to its lively writing style and relevant examples of the most up-to-date developments in training, research and practice, including the strategic role of training and the use of new technologies in training... |
|
![]() |
Dynamic Web Application Development using XML and Java List Price: $110.95 Sale Price: $42.87 Used From: $42.00 |
|
Providing an end-to-end view of how modern web applications are built, Dynamic Web Applications takes a cohesive approach to building a software architecture from core components. It tells a development story through a running case study taking you through each phase - analysis, design and implementation - without straying into detail or trying to cover too many alternatives... |
|
![]() |
The Semantic Web: A Guide to the Future of XML, Web Services, and Knowledge Management List Price: $35.00 Sale Price: $2.52 Used From: $0.47 |
|
* "The Semantic Web is an extension of the current Web in which information is given * well-defined meaning, better enabling computers and people to work in cooperation."-Tim Berners-Lee, Scientific American, May 2001 * This authoritative guide shows how the Semantic Web works technically and how businesses can utilize it to gain a competitive advantage * Explains what taxonomies and ontologies are as well as their importance in constructing the Semantic Web * Companion Web site includes further updates as the framework develops and links to related sites |
|
![]() |
Training and Development: Enhancing Communication and Leadership Skills List Price: $64.40 Sale Price: $54.19 Used From: $50.00 |
|
Training and Development provides readers and future trainers with practical “how-to” strategies for training and for designing training programs. Rooted in contemporary instructional communication research, the book suggests that every aspect of developing a training program should be based upon trainee needs. |
|












Leave a Reply
You must be logged in to post a comment.