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 July 31st, 2010, 12:47 PM
Authorized User
 
Join Date: Jul 2010
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default Don't understand how XSLT works

I have the source document:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>

<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>

<cd>
<title>One night only</title>
<artist>Bee Gees</artist>
<country>UK</country>
<company>Polydor</company>
<price>10.90</price>
<year>1998</year>
</cd>
<cd>
<title>Sylvias Mother</title>
<artist>Dr.Hook</artist>
<country>UK</country>
<company>CBS</company>
<price>8.10</price>
<year>1973</year>
</cd>
<cd>
<title>Maggie May</title>
<artist>Rod Stewart</artist>
<country>UK</country>
<company>Pickwick</company>
<price>8.50</price>
<year>1993</year>
</cd>

<cd>
<title>For the good times</title>
<artist>Kenny Rogers</artist>
<country>UK</country>
<company>Mucik Master</company>
<price>8.70</price>
<year>1995</year>
</cd>

<cd>
<title>Soulsville</title>
<artist>Jorn Hoel</artist>
<country>Norway</country>
<company>WEA</company>
<price>7.90</price>
<year>1996</year>
</cd>
<cd>
<title>Bridge of Spies</title>
<artist>T`Pau</artist>
<country>UK</country>
<company>Siren</company>
<price>7.90</price>
<year>1987</year>
</cd>

<cd>
<title>Midt om natten</title>
<artist>Kim Larsen</artist>
<country>EU</country>
<company>Medley</company>
<price>7.80</price>
<year>1983</year>
</cd>
<cd>
<title>Pavarotti Gala Concert</title>
<artist>Luciano Pavarotti</artist>
<country>UK</country>
<company>DECCA</company>
<price>9.90</price>
<year>1991</year>
</cd>
<cd>
<title>The dock of the bay</title>
<artist>Otis Redding</artist>
<country>USA</country>
<company>Atlantic</company>
<price>7.90</price>
<year>1987</year>
</cd>

</catalog>
---
And my transformation file like this:
----
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<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:call-template name="artist"/>
<xsl:call-template name="below18"/>

</body>
</html>
</xsl:template>
<!--list all UK artist-->
<xsl:template name="artist" >
<ArtistUK>
<xsl:for-each select="catalog/cd/artist">
<xsl:if test="catalog/cd/country='UK'">
<list_artist><xsl:value-of select ="artist"/></list_artist>
</xsl:if>
</xsl:for-each>
</ArtistUK>
</xsl:template>

<!--list all UK artist below 18-->

<xsl:template name="below18" >
<Artistbelow18>
<xsl:for-each select="catalog/cd/artist">
<xsl:if test="catalog/cd/year &gt; 1992 and catalog/cd/country='UK'">
<list_artist><xsl:value-of select ="artist"/></list_artist>
</xsl:if>
</xsl:for-each>
</Artistbelow18>
</xsl:template>
</xsl:stylesheet>
----
The expectation document is: receive the information about 2 list of artists (that is in upper each template)
 
Old July 31st, 2010, 12:54 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I think instead of
Code:
<xsl:for-each select="catalog/cd/artist">
<xsl:if test="catalog/cd/country='UK'">
<list_artist><xsl:value-of select ="artist"/></list_artist>
</xsl:if>
</xsl:for-each>
you rather want
Code:
<xsl:for-each select="catalog/cd[country='UK']/artist">
   <list_artist><xsl:value-of select ="."/></list_artist>
</xsl:for-each>
although using an element like 'list_artist' in HTML (which your first template outputs) does not make much sense. But at least the code above shows you how to put a predicate in square brackets and it should correctly output artist names.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old July 31st, 2010, 02:51 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You need to read about the concept of the "context node" or "context item" which represent the current position in the document. An instruction like xsl:for-each changes the context node. Relative path expressions navigate starting at the context node. So you don't want to say

Code:
<xsl:for-each select="catalog/cd/artist">
<xsl:if test="catalog/cd/country='UK'">
you should say instead

Code:
<xsl:for-each select="catalog/cd/artist">
<xsl:if test="../country='UK'">
or in this case, since the condition applies to the whole body of the for loop, you can simply select the cd's you want with a predicate select="catalog/cd[country='uk']/artist".
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
xslt works in IE 8 but not in FF 3.5, can anyone help? pagis XSLT 4 September 22nd, 2009 07:59 AM
incorporate javascript, works in html but not xslt ismailc XSLT 2 December 12th, 2007 03:32 AM
I don't understand... jmsherry ASP.NET 2.0 Basics 17 July 23rd, 2007 12:28 PM
I still do not understand aude_poullain ASP.NET 1.0 and 1.1 Basics 2 February 7th, 2007 09:11 PM
Does Print Template <HTML XMLNS:IE> works w/ xslt? gracehanh XSLT 0 July 20th, 2005 08:22 AM





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