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 June 20th, 2007, 10:58 AM
Registered User
 
Join Date: Jun 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default please help deep nested looping

Hi i have the following xml

<rootmenu>
  <name>
    <title>title1</title>
      <element>el1</element>
      <element>el2
    <subelement>sub1</subelement>
    <subelement>sub2</subelement>
      </element>
      <element>el3</element>
      <element>el4</element>
  </name>
  <name>
    <title>title2</title>
  </name>
</rootmenu>

i need a nested loop in my xsl to read the data out so it looks like the following
title1
 el1
 el2
  sub1
  sub2
 el3
 el4
title2


i have the following xsl but it doesn't produce what i require.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">


 <xsl:for-each select="rootmenu/name">
 <xsl:value-of select="title" />

 <xsl:value-of select="element" />

  <xsl:for-each select="rootmenu/name/element">
  <xsl:value-of select="subelement" />
  </xsl:for-each>

 </xsl:for-each>


</xsl:template>
</xsl:stylesheet>

please can someone help me, i'm going mad trying to understand why it wont work and also only read the first element and replaceing the name with a "." goes to deep into the node, i dont understand how to work this, many thanks in advance

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

The reason your code doesn't work is that you haven't understood context. When you do

<xsl:for-each select="rootmenu/name">

the context node becomes a "name" element, and to select the "element" children your selection needs to start from there:

<xsl:for-each select="element">

But it's really much nicer to tackle this kind of problem with template rules rather than nested xsl:for-each iterations.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 20th, 2007, 01:41 PM
Registered User
 
Join Date: Jun 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
cheers for that, i tried it but it only reads the first child node and not the others after it, please could you give me some assistance as to how i can get it to work?

also i have googled but do you have any good examples on understanding the template rules you touched on?


Many Thanks,

 
Old June 20th, 2007, 01:56 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Amazing how many people expect me to find bugs in their code but don't show me the code...

To learn about the concepts of template rules, read a good XSLT book. If you like to understand concepts in great depth and detail, you'll enjoy my book; if you want something more introductory, try Jeni Tennison's.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 20th, 2007, 02:34 PM
Registered User
 
Join Date: Jun 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
apologies about the code, but i have shown the whole of my xml file how i need the layout to be read and the code i have in the xsl file in my origonal post, i don know what other code i can show this is all the code i am using as i am new to this and need the xml file i dispaled to be read correctly by the code in the xsl file i displayed
is there anything else you may require to help me?

Many Thanks,

 
Old June 20th, 2007, 03:16 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You said you had tried my suggestion and it didn't work. So you did something wrong. Your original code before I made the suggestion isn't going to help me see what you did wrong.

The important thing is, have you now understood why your original code didn't work, because you hadn't grasped the essential concept that relative path expressions work relative to a context node which changes when you do an xsl:for-each?

>is there anything else you may require to help me?

Since you ask, yes. Take the trouble to write in sentences with full stops and capital letters. It makes you look more professional and less like a teenager, and it saves me a few seconds in reading and understanding your question.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 20th, 2007, 03:17 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>is there anything else you may require to help me?

Oh, and giving your real name is a nice courtesy too.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 20th, 2007, 04:03 PM
Registered User
 
Join Date: Jun 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
i am sorry if i offended you in any way i just need some help with this xsl style sheet

i have the following xml file now:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="menu.xsl"?>
<rootmenu>
<name>
 <title>title1</title>
   <element>el1</element>
   <element>el2
     <subelement>sub1</subelement>
     <subelement>sub2</subelement>
   </element>
   <element>el3</element>
   <element>el4</element>
</name>
<name>
  <title>title2</title>
</name>
</rootmenu>

and am applying the following xsl file to it:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template><xsl:template match="rootmenu/name">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="element"/>
</p>
</xsl:template><xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template><xsl:template match="element">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>

the outcome is:
Title: title1
Artist: el1
Artist: el2 sub1 sub2
Artist: el3
Artist: el4

Title: title2

But i require it to be
Title: title1
Artist: el1
Artist: el2
logo: sub1
logo: sub2
Artist: el3
Artist: el4

Title: title2

i dont know how to achieve this can you possible help me please


Many Thanks
Rikki






Similar Threads
Thread Thread Starter Forum Replies Last Post
looping mrjoka Classic ASP Basics 1 September 26th, 2007 12:21 AM
Looping deepsea007 XSLT 1 June 14th, 2007 12:13 PM
Deep element access azrail1222 XSLT 1 August 22nd, 2006 02:24 AM
Nested Repeaters three deep? Silfverduk ASP.NET 2.0 Basics 0 June 21st, 2006 01:14 PM
AS/400 and VB6.0 - Deep in the “ol” Kim chi!!!! kf4rrm Pro VB Databases 0 March 14th, 2005 11:37 AM





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