Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 February 20th, 2008, 12:04 PM
Authorized User
 
Join Date: May 2007
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Looping through XML

Hi. guys
The following xslt stylesheet supposed to loop through the xml doc visiting each node, read and write its name and their attributes (userid, password). Actually it visits the only the first node since it performs this statement <xsl:for-each select="data/node1">
which prevents it from visiting the others. My question is how to amend this code in order to allow it to visit the whole xml doc with any number of nodes with same attributes?
Any help is really much appreciated
Cheers

Here is the xml document

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="xx.xslt"?>
<data>
    <node1 userid="x" password="y" />
         <node2 userid="xx" password="yy" />
    <node3 userid="xxx" password="yyy" />
</data>

Here is the xslt stylesheet
<?xml version="1.0" encoding="ISO-8859-1" ?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" indent="yes"/>

 <xsl:template match="/">

 <html xmlns="http://www.w3.org/1999/xhtml">

 <body>
 <xsl:for-each select="data/node1">
   <h2> <xsl:value-of select="local-name()"/> </h2>
             <h1> <xsl:value-of select="@userid" /> </h1>
             <h1> <xsl:value-of select="@password" /> </h1>
  </xsl:for-each>
   </body>
  </html>
  </xsl:template>
  </xsl:stylesheet>


 
Old February 20th, 2008, 12:11 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You could just do xsl:for-each select="data/*".

But a better way, assuming that the element names node1, node2, and node3 imply some difference of structure or meaning (else why would they have different names?) is to use xsl:apply-templates, and then define a template rule to process each kind of child element.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 20th, 2008, 12:13 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Use
Code:
<xsl:for-each select="data/*">
if you want to process all child elements of the data element.






Similar Threads
Thread Thread Starter Forum Replies Last Post
looping mrjoka Classic ASP Basics 1 September 26th, 2007 12:21 AM
Looping through XML in SQL Server 2005 francislang SQL Server 2005 1 February 23rd, 2006 11:47 AM
xml looping problem harpua Classic ASP XML 0 November 18th, 2005 06:36 PM
Looping..? dedex C# 2 January 6th, 2005 11:24 PM
Looping through attributes in an XML document francislang XML 4 November 17th, 2004 11:02 AM





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