Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 12th, 2008, 01:15 PM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default repetitive xml - sorry, I'm new to this...

I have the following xml there are thousands of items.
Is there a way with xslt to "loop" through them displaying each on the page?
Any help would be great.

<?xml version="1.0" encoding="UTF-8"?>
<?xml:stylesheet type="text/xsl" href="myxsl.xsl" ?>
<ControlledList>
<Item Id="1" ConceptId="1">
    <Name>name1</Name>
    <ScopeNotes>notes1</ScopeNotes>
    <HistoryNotes>history1</HistoryNotes>
</Item>
<Item Id="2" ConceptId="2">
    <Name>name2</Name>
    <ScopeNotes>notes2</ScopeNotes>
    <HistoryNotes>history2</HistoryNotes>
</Item>
</ControlledList>
 
Old March 12th, 2008, 01:36 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Actually you don't need to write a loop, because that's what the XSLT processor does by default. Just write a template rule to process Item elements:

<xsl:template match="Item">
  <h2>Item <xsl:value-of select="@Id"/> (Name <xsl:value-of select="Name"/></h2>
  <p><xsl:value-of select="ScopeNotes"/></p>
</xsl:template>

In practice you might want to add some wrapper stuff:

<xsl:template match="ControlledList">
  <html>
    <xsl:apply-templates/>
  </html>
</xsl:template>

It's the apply-templates that does the "looping".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 12th, 2008, 01:51 PM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's great.
Thanks a lot, I'd have been sat here all night working that out.

Cheers for the help

 
Old March 12th, 2008, 02:05 PM
Authorized User
 
Join Date: Mar 2008
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is it considered better practice to define templates for sub-items instead of looping through?

 
Old March 12th, 2008, 04:24 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Many people recommend use of template rules (called "push mode") as better programming practice than using for-each and call-template ("pull mode"). But both have their place. The big advantage of template rules is that your code is more resilient to change in the structure of the input document.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference




Similar Threads
Thread Thread Starter Forum Replies Last Post
Repetitive regex mark.gargan BOOK: Beginning Regular Expressions 2 August 6th, 2008 03:31 AM
SQL Server 2005 XML: FOR XML PATH -> cdata? stoves SQL Server 2005 1 July 8th, 2008 02:40 AM
VB.net, adding XML data to an existing XML file saikoboarder XML 11 April 17th, 2008 04:19 PM
repetitive instruction, more efficient solution? stolte XSLT 4 November 27th, 2007 02:41 PM
XSLT Table Row with repetitive first cell the cause XSLT 5 July 3rd, 2007 01:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.