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 January 27th, 2011, 01:01 PM
Registered User
 
Join Date: Jan 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default If preceding-sibling question

My XML basically look something like this:


Code:
<Staff>
     <Row alphakey="doejohn" building="abc" class="Algebra" />
     <Row alphakey="doejohn" building="abc" class="Geometry" />
     <Row alphakey="personbob" building="abc" class="Calculus" />
     <Row alphakey="personbob" building="abc" class="Precalc" />
...
</Staff>
And my code looks a little like this:


Code:
<xsl:for-each select="$Row">
     <xsl:if test="not(@alphakey = preceding-sibling::Row[1]/@alphakey)">
         ...
For some reason, the test always comes back as true, no matter what. What I need it to do in this situation is only process the first instance of each teacher, and skip the rest. I have also tried this meathod...


Code:
<xsl:for-each select="$Row">
     <xsl:variable name="akey" select="@alphakey"/>
     <xsl:if test="not(preceding-sibling::Row[@alphakey=$akey])">
     ...
And the same thing happens. What am I doing wrong? Also, I am using xslt 1.0

EDIT: Fixed the code a little.

Last edited by XadRav; January 27th, 2011 at 08:01 PM..
 
Old January 27th, 2011, 01:10 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Try changing Rows[1] to Row[1] as you have no element names of <Rows>.

Also you could look at the previously mentioned Muenchian grouping, as using similar techniques (i.e. using the key() function with generate-id() function) you can accomplish the same thing, which depending on your dataset size could be more performant.
http://www.jenitennison.com/xslt/gro...muenchian.html
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old January 27th, 2011, 09:06 PM
Registered User
 
Join Date: Jan 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am using Muenchian grouping to get that particular data set. What we're doing is we have a sql database filled with teachers and the classes they are teaching, a data view to group the teachers with their classes to our class catalog, and SharePoint calling that data view and importing it as XML every time the page is called, so all of the info on the page is to the second accurate and extremely dynamic. The way we have it set up (and it kind of has to be this way) is that the resulting data view shows each teacher-class combo (as shown in the code above) and I have a muenchian group set to group each teacher with their courses. However, the way it is all set up with SharePoint, it needs to go through each row to process the grouping, and when the loop reaches the teachers 2nd, 3rd, etc. row, is displays the group all over again. What I though would be the easiest way to fix this is to have the loop only process the first instance of the teacher, so the group would only be displayed once.

tl;rd; I am using Muenchian grouping, but I need the if statement to work for what I need to work properly.
 
Old January 27th, 2011, 09:08 PM
Registered User
 
Join Date: Jan 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also, I meant to say Rows not row, it's just the weird way SharePoint is making the XML, so I changed the XML example to row instead of rows to avoid confusion...
 
Old January 28th, 2011, 04:10 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

It seems to work fine for me.

The following slightly modified template outputs only the first class for each teacher:

Code:
<xsl:template match="Staff">
	<xsl:for-each select="Row">
		<xsl:if test="not(@alphakey = preceding-sibling::Row[1]/@alphakey)">
				<xsl:copy-of select="."/>
			</xsl:if>
	</xsl:for-each>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Want to Pull multiple following-sibling data to preceding-sibling element sameer_kadam XSLT 4 May 9th, 2009 08:07 AM
Preceding Sibling mphare XSLT 3 March 4th, 2009 11:04 AM
Using preceding-sibling mcanne98 Infopath 0 September 11th, 2008 11:09 PM
Preceding-sibling question. lafilip XSLT 7 March 5th, 2007 12:35 PM
preceding-sibling jonesyp XSLT 2 November 22nd, 2005 12:29 PM





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