[xsl] Finding parent of the middle child

Subject: [xsl] Finding parent of the middle child
From: "Brook Ellingwood" <belling@xxxxxxx>
Date: Mon, 20 Jun 2005 13:21:17 -0700
I'm having a hard time breaking a list of nested elements into a two-column
layout in HTML output.

There are several section elements containing a variable number of child
elements. I want to return a count of all the child elements and identify the
mid point of all the child elements. At the top of my XSL, I do this with:

<xsl:variable name="gearMid">
	<xsl:value-of select="round(count(//rec_gear_sec/*) div 2)"/>
</xsl:variable>

This returns "34."

Next, I want to identify which parent element contains the child at position
34, so I can split the list beginning with the following sibling. However, I
can't even get as far as finding the matching child when I try a similar
method to returning the count:

<xsl:variable name="gearSplit">
	<xsl:value-of select="//rec_gear_sec/*[position() = $gearMid]"/>
</xsl:variable>

That returns nothing. When I substitute numbers for the variable, I discover
that it's matching the first child element at that position *within its parent
element* not within the list of all child elements. Even though this is
behavior that I've used and expected in the past, I'm now confused as to why
the context is interpreted differently for those two matches.

I'm starting to suspect that the real solution to the problem may be some sort
of recursive template with a key, but I'm not really seeing exactly how to do
it. I'd appreciate any help.

Sample XML snippet:

<root>
<rec_gear_sec>
	<rec_gear_title>Official Papers</rec_gear_title>
	<rec_gear>Valid passport and visas</rec_gear>
	<rec_gear>Airline tickets</rec_gear>
	<rec_gear>International health card with immunizations</rec_gear>
</rec_gear_sec>
<rec_gear_sec>
	<rec_gear_title>Clothing Accessories</rec_gear_title>
	<rec_gear>Balaclava or neck gaiter</rec_gear>
	<rec_gear>Wool or fleece mittens/gloves</rec_gear>
	<rec_gear>Thin gloves or mitten liners</rec_gear>
	<rec_gear>Wool or fleece hat</rec_gear>
	<rec_gear>Gaiters, knee-length</rec_gear>
	<rec_gear>Sun hat </rec_gear>
</rec_gear_sec>
<rec_gear_sec>
	<rec_gear_title>Travel Accessories</rec_gear_title>
	<rec_gear>Water bottles (two 1-quart) or a hydration system plus one 1 qt.
water bottle</rec_gear>
	<rec_gear>Headlamp and spare lithium batteries/bulb (no
flashlights!)</rec_gear>
	<rec_gear>Sunglasses with dark lenses</rec_gear>
	<rec_gear>Ski goggles</rec_gear>
	<rec_gear>Toilet paper (1 roll)</rec_gear>
	<rec_gear>Hand sanitizer gel</rec_gear>
	<rec_gear>Collapsible trekking poles</rec_gear>
	<rec_gear>Favorite energy snacks and powdered drinks</rec_gear>
	<rec_gear>Sunblock and lip balm with high SPF</rec_gear>
	<rec_gear>Toiletry kit</rec_gear>
	<rec_gear>Small bath towel</rec_gear>
	<rec_gear>Bio-degradable soap/shampoo</rec_gear>
	<rec_gear>Personal first-aid kit</rec_gear>
	<rec_gear>Rain cover and two large plastic trash bags for your daypack and
duffel bag </rec_gear>
</rec_gear_sec>
<rec_gear_sec>
	<rec_gear_title>City Clothing</rec_gear_title>
	<rec_gear>Bring a few lightweight, easily washable items for travel and when
you are not climbing</rec_gear>
	<rec_gear>Comfortable shoes</rec_gear>
</rec_gear_sec>
<rec_gear_sec>
	<rec_gear_title>Field Gear</rec_gear_title>
	<rec_gear>Sleeping bag rated to 100F or less </rec_gear>
</rec_gear_sec>
<rec_gear_sec>
	<rec_gear_title>Clothing</rec_gear_title>
	<rec_gear>Midweight wool or fleece jacket/sweater</rec_gear>
	<rec_gear>Wool or fleece pants</rec_gear>
	<rec_gear>Midweight thermal underwear top and bottom, synthetic</rec_gear>
	<rec_gear>Expedition weight thermal underwear top and bottom,
synthetic</rec_gear>
	<rec_gear>T-shirts, mix of synthetic and cotton</rec_gear>
	<rec_gear>Long-sleeve synthetic shirts</rec_gear>
	<rec_gear>Synthetic, quick-drying hiking shorts</rec_gear>
	<rec_gear>Synthetic, quick-drying hiking pants</rec_gear>
	<rec_gear>Underwear</rec_gear>
	<rec_gear>Hiking socks - wool and/or synthetic</rec_gear>
	<rec_gear>Liner socks</rec_gear>
</rec_gear_sec>
<rec_gear_sec>
	<rec_gear_title>Optional Field Gear</rec_gear_title>
	<rec_gear>Camera, lenses and lots of film</rec_gear>
	<rec_gear>Small binoculars (highly recommended)</rec_gear>
	<rec_gear>Reading and writing materials</rec_gear>
	<rec_gear>Watch with alarm or travel clock</rec_gear>
	<rec_gear>Folding-style umbrella</rec_gear>
	<rec_gear>Earplugs</rec_gear>
	<rec_gear>Bandanna</rec_gear>
	<rec_gear>Water filter equipment or iodine tablets (bottled water available
for purchase)</rec_gear>
	<rec_gear>Insect repellent with DEET</rec_gear>
	<rec_gear>Spare contact lenses or prescription glasses</rec_gear>
	<rec_gear>Ziploc bags, to protect camera, binoculars, etc. from
dust</rec_gear>
</rec_gear_sec>
<rec_gear_sec>
	<rec_gear_title>Outerwear</rec_gear_title>
	<rec_gear>Waterproof, breathable jacket</rec_gear>
	<rec_gear>Waterproof, breathable pants</rec_gear>
	<rec_gear>Insulated down or synthetic filled jacket</rec_gear>
	<rec_gear>Wind jacket and/or pants (optional) </rec_gear>
</rec_gear_sec>
<rec_gear_sec>
	<rec_gear_title>Footwear</rec_gear_title>
	<rec_gear>Water-resistant leather hiking boots, midweight boots work
great</rec_gear>
	<rec_gear>Camp shoes - sneakers or sport sandals</rec_gear>
</rec_gear_sec>
<rec_gear_sec>
	<rec_gear_title>Luggage</rec_gear_title>
	<rec_gear>2 duffel bags, 1 large enough for all climbing gear and clothing
and a second bag with non climbing items to be left at the hotel</rec_gear>
	<rec_gear>Luggage tags and luggage locks</rec_gear>
	<rec_gear>Passport pouch or money belt</rec_gear>
	<rec_gear>Daypack, 2000-3000 cu. in. to be used during climb</rec_gear>
</rec_gear_sec>
</root>

-- Brook

Current Thread