RE: [xsl] Finding and comparing a parent attribute for the next result in a for-each loop

Subject: RE: [xsl] Finding and comparing a parent attribute for the next result in a for-each loop
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 7 Aug 2008 17:41:28 +0100
It looks to me as if this is more of a grouping problem than a sorting
problem.

If you are really stuck with XSLT 1.0 then you need to learn about Muenchian
grouping (http://www.jenitennison.com/xslt/grouping).

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: Kate Busch Petersen [mailto:kate.busch-petersen@xxxxxxxxxxxxx] 
> Sent: 07 August 2008 17:28
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Finding and comparing a parent attribute for 
> the next result in a for-each loop
> 
> Hi all,
> 
> I'm fairly new to XSLT and am having no end of difficulty 
> sorting the data from the following XML:
> 
> 	<resource day="04" live="200707040000" month="07" 
> monthname="July" 
> year="2007">
> 		<title>Title 1</title>
> 		<section name="Articles">
> 			<subsection>section 1</subsection>
> 		</section>
> 	</resource>
> 	<resource day="10" live="200701100000" month="01" 
> monthname="January" year="2007">
> 		<title>Competition: Commission energy sector 
> inquiry confirms serious competition problems </title>
> 		<section name="News releases">
> 			<subsection>section 1</subsection>
> 		</section>
> 		<section name="Articles">
> 			<subsection>section 1</subsection>
> 		</section>
> 	</resource>
> 
> and so on, for as many resources as there are.
> 
> I have managed to sort the data by section and 
> correspondingly by subsection, but now I am having problems 
> drilling down through the navigation, to display all the 
> years for each section (and subsection).
> 
> The XSL I have done so far is rubbish, but here goes:
> 
> <xsl:choose>
> 	<xsl:when test="$subsection = ''">
> 		<xsl:for-each select="//section[@name=$section]">
> 		<xsl:sort select="../@live" order="descending"/>
> 			<xsl:if
> test="not(parent::resource/following-sibling::resource/@year=p
> arent::node()/@year)">
> 				<li><!-- list item lives here 
> --><xsl:value-of select="../@year"/></li>
> 			</xsl:if>
> 		</xsl:for-each>
> 	</xsl:when>
> 	<xsl:otherwise>
> 		<xsl:for-each 
> select="//section[@name=$section][node()=$subsection]">
> 		<xsl:sort select="../@live" order="descending"/>
> 			<xsl:if
> test="not(parent::resource/following-sibling::resource/@year=p
> arent::node()/@year)">
> 				<li><!-- list item lives here 
> --><xsl:value-of select="../@year"/></li>
> 			</xsl:if>
> 		</xsl:for-each>
> 	</xsl:otherwise>
> </xsl:choose>
> 
> Ideally it should produce a list of all years, so the user 
> can browse the resources by year. All | 2008 | 2007 | 2006 etc
> 
> Yup - it's gorgeous old XSLT 1.0 - I don't doubt there is a 
> brilliant tag in XSLT 2.0 but hey ho.
> 
> The problem seems to be that the following-sibling of the 
> parent node doesn't return the next node in the for-each, but 
> the next node in the main xml. So sometimes it returns no 
> results at all because there's a following sibling in a node 
> which it shouldn't be looking at.
> 
> I'm sure I'm making a simple mistake, but for the life of me 
> I can't figure out what it is.
> 
> All help gratefully appreciated!
> 
> Kate

Current Thread