 |
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
|
|
|

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

June 20th, 2007, 12:15 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|

June 20th, 2007, 01:41 PM
|
Registered User
|
|
Join Date: Jun 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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,
|

June 20th, 2007, 01:56 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|

June 20th, 2007, 02:34 PM
|
Registered User
|
|
Join Date: Jun 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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,
|

June 20th, 2007, 03:16 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|

June 20th, 2007, 03:17 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>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
|

June 20th, 2007, 04:03 PM
|
Registered User
|
|
Join Date: Jun 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |