[tds_menu_login inline="yes" guest_tdicon="td-icon-profile" logout_tdicon="td-icon-log-out" show_menu="yes" toggle_txt="Your Profile" menu_uh_padd="12px 20px" menu_ul_padd="12px 20px" menu_ulo_padd="12px 20px" menu_gc_padd="12px 20px" menu_gh_padd="15px 20px" menu_uh_color="var(--lc-primary)" menu_uh_border_color="var(--lc-lgrey-c1)" menu_bg="var(--lc-greyish)" menu_ul_link_color="var(--lc-black)" menu_ul_link_color_h="var(--lc-primary)" menu_ul_sep_color="var(--lc-lgrey-c1)" menu_uf_txt_color="var(--lc-black)" menu_uf_txt_color_h="var(--lc-primary)" menu_uf_icon_color="var(--lc-black)" menu_uf_icon_color_h="var(--lc-primary)" menu_uf_border_color="var(--lc-lgrey-c1)" f_uh_font_family="sans-serif_global" f_links_font_family="sans-serif_global" f_uf_font_family="sans-serif_global" f_uh_font_size="14" f_links_font_size="14" f_uf_font_size="14" f_uf_font_line_height="1.2" f_links_font_line_height="1.2" f_uh_font_line_height="1.2" f_uh_font_weight="500" f_links_font_weight="400" f_uf_font_weight="500" menu_ul_sep_space="0" menu_ul_space="10" f_gh_font_family="sans-serif_global" f_btn1_font_family="sans-serif_global" f_btn2_font_family="sans-serif_global" show_version="" toggle_txt_color="var(--lc-black)" toggle_txt_color_h="var(--lc-primary)" icon_color="var(--lc-black)" icon_color_h="var(--lc-primary)" f_toggle_font_family="sans-serif_global" f_toggle_font_transform="" f_toggle_font_size="eyJhbGwiOiIxNiIsInBvcnRyYWl0IjoiMTQifQ==" f_toggle_font_line_height="1.2" f_toggle_font_weight="600" menu_arrow_color="var(--lc-greyish)" menu_border_color="var(--lc-greyish)" menu_shadow_shadow_size="0" menu_shadow_shadow_offset_vertical="0" f_gh_font_size="14" f_btn1_font_size="14" f_btn2_font_size="14" f_btn2_font_line_height="1.2" f_btn1_font_line_height="1.2" f_gh_font_line_height="1.2" menu_gh_color="var(--lc-primary)" menu_gh_border_color="var(--lc-lgrey-c1)" menu_gc_btn1_bg_color="var(--lc-lgrey-c1)" menu_gc_btn1_bg_color_h="var(--lc-primary)" menu_gc_btn1_color="var(--lc-black)" menu_gc_btn1_color_h="var(--lc-white)" menu_gc_btn1_border_color="var(--lc-lgrey-c1)" menu_gc_btn1_border_color_h="var(--lc-primary)" menu_gc_btn2_color="var(--lc-black)" menu_gc_btn2_color_h="var(--lc-primary)" f_gh_font_weight="500" f_btn2_font_weight="500" f_btn1_font_weight="500" menu_gh_txt="Explore account options." icon_size="20" avatar_size="20" disable_dropdown="yes" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjAiLCJwYWRkaW5nLXJpZ2h0IjoiMjAiLCJib3JkZXItY29sb3IiOiJ2YXIoLS1sYy1kYXJrLWJsdWUpIiwiZGlzcGxheSI6IiJ9fQ=="]

Become a member

Get the latest updates and tutorials relating to Artificial Intelligence (AI).

― Advertisement ―

How to Write Engaging Blogs People Want to Read

Tips and tricks to write engaging blogs. From understanding your audience to SEO optimization, this guide covers it all.
HomeArchiveUse loops to display XML in an HTML browser

Use loops to display XML in an HTML browser

In the first article in this series I presented a brief introduction to the use of XSL as a means to display XML in HTML browsers. Before continuing, I’d like to clear up a few points which I believe may have been confusing, largely due to a lack of rigorous clarity on my part.

In the preceding article I mentioned that HTML browsers will ignore unrecognized tags and merely display the text content. What I failed to mention is that this is a function of HTML, not of the browsers themselves. In order to see what I was talking about, save the XML file as an HTML file and view it. (Additional weirdness: The most recent versions of IE, Navigator, and Opera will all display such a file as mentioned above, and their displays will be virtually identical; an older version of Navigator will probably display nothing at all.)

I also pointed out that the code examples I provide will not be displayed properly in older browsers. What I should have said is that this code (saved as XML documents) will only be displayed properly in an XML enabled browser, such as IE 5+ or Navigator 6+. As of the publication date of this article, Opera still ignores tags and displays the remainder as a few lines of plain text, and my Netscape Navigator 4.77 insists on using something called “iexplore.exe” to open an XML file.

Continuing where I left off last time, I was pointing out that the code I’d provided only produced a table with two rows. We can build upon that code by using some of the tools that XSLT provides. First of all, take a look at the source file of the complete XML document which contains the data we want to display, part of which is reproduced here:

<?xml version=”1.0″?>
<?xml-stylesheet type=”text/xsl”
<location>Way Big Stadium</location>
<title>Sweet Willow</title>
<coolness>Excellent harmonies</coolness>

Now take a look at its XSL companion:

<?xml version=’1.0′?>
<xsl:stylesheet xmlns:xsl=” “>
<xsl:template match=”/”>
<table border=”1″>
<xsl:for-each select=”band/show”>
<table border=”1″>
<th><xsl:value-of select=”location”/></th>
<td><xsl:value-of select=”date/year”/> –
<xsl:value-of select=”date/month”/>
<xsl:value-of select=”date/day”/></td>
<table border=”1″>
<th>Really Cool Stuff About This Performance</th>
<xsl:for-each select=”song”>
<td><xsl:value-of select=”title”/></td>
<td><xsl:value-of select=”key”/></td>
<td><xsl:value-of select=”time”/></td>
<td><xsl:value-of select=”coolness”/></td>

Comparing this document to the example provided in the previous article, you’ll notice an additional XSLT element being used, <xsl:for-each>. As you might have deduced, this is how XSL implements loops. In this case, due to the structure of the XML document, nested loops are required.


In order for this XSL to work in your browser, it needs to refer to the correct XSLT version, which is done in the <xsl:stylesheet> element.

For IE 5+
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/TR/WD-xsl”>

For Navigator 6+
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>


If you save your appropriately-modified file as deadandgladaboutit.xsl in the same directory as the source of the XML document, and view the XML through the proper browser, you should see one large table consisting of four cells, each of which contains another table. These are created by the XSL as it runs through its nested loops, extracting the values of each successive node in the XML. Time now, I think, for a blow-by-blow description of what’s really happening here.

The XSL document must follow the rules for all XML documents, so it begins with the standard XML declaration followed by the root element, in this case <xsl:stylesheet>. The <xsl:template> element uses the attribute-value pair <match=”/”> to define the root element of the XML document as the base from which all content presentation will be outlined within the XSL. I’ll just skip right over the basic HTML tags and move along to the next XSLT element.

<xsl:for-each> is a loop-controller, <select=”band/show”> defines the nodeset of each loop. In this case, <band> is the root element of the XML document and <show> is its only child element, but there are
two instances of <show>; this loop will process each of them successively.

<xsl:value-of> gets the value of a particular node and outputs it as directed. In this case the value of the <location> element is displayed as the header of a table, and the values of the three child elements of <date>
are concatenated and displayed in a cell of the table.

Following the code which displays the header row of the next table is another loop. Remember that we’re still within the first loop, which is using <select=”band/show”> to define its nodeset; so this new loop need only define its nodeset with <select=”song”>, rather than <select=”band/show/song”>. This loop produces a row in the table for each <song> element in the XML.

After running through all <song>s in the first <show>, this loop is terminated and the final touches are put on the first row of the main table. The next row is created by the next iteration of the first loop, which terminates when it has processed all of the <show>s.

So there you have it. As long as the XML document’s structure is preserved, you can add as much information to it (as many <show>s, as many <song>s) as you wish, and no changes to the XSL should be required.

Murdok provides free highly informative newsletters for web developers, IT professionals and small business owners. We deliver 50 million email newsletters per month and have over 4,000,000 unique opt-in subscribers. From our extensive range of email newsletters we can provide you with a selection of newsletters that best meet your interests.


Please enter your comment!
Please enter your name here