[xsl] Select the nearest of two preceding siblings

Subject: [xsl] Select the nearest of two preceding siblings
From: "ccbranham" <ccbranham@xxxxxxxxxxx>
Date: Thu, 19 Jul 2007 17:07:26 -0700
Hello:

Sorry if this is too simple, but I'm still a bit new at this. I need to compare the relative positions of two preceding siblings and grab the value of the nearest one.

I'm transforming a flat document into a shallow hierachical document using xsl:for-each-group and group-starting-with. In the for-each-group, an element is created with attributes whose values are based on text from elements that occur before the group in document order. I've gotten this far (for Case 1 below), but there is variation in the input I need to account for (Case 2).

So these are the two cases for the input:

Case 1: With a h3
..
<p style="h1">Heading 1</p>
<p style="h2">Heading 2</p>
<p style="h3">Heading 3</p>
<p style="h4">Heading 4</p>
<p style="Standard">...</p>
<p style="Standard">...</p>
..


Case 2: No h3 and h2 has the text "Odd" .. <p style="h1">Heading 1</p> <p style="h2">Odd</p> <p style="h4">Heading 4</p> <p style="Standard">...</p> <p style="Standard">...</p> ..


I need to create groups starting with h4, and use h1-h3 for attribute values or for choice in processing. So here is the desired output:


Case 1: Select the value of p[@style='h3'] for @mythiratt

..
  <myelem myfiratt="Heading 4" mysecatt="Heading 1" mythiatt="Heading
3">
     <p style="Standard">...</p>
     <p style="Standard">...</p>
     ...
  </myelem>
..


Case 2: Since the preceding p[@style='h2'] has the text "Odd" and it occurs ahead of the next preceding p[@style='h3'], supply a value for @mythiatt


..
  <myelem myfiratt="Heading 4" mysecatt="Heading 1" mythiatt="Oddball">
     <p style="Standard">...</p>
     <p style="Standard">...</p>
     ...
  </myelem>
..


I've set up the for-each-group, and can get the values for myfiratt and mysecatt easily. I'm just not sure how to do the comparison to find out which heading is closest in the document. Is this a simple thing to do, or do I need to take a different approach?


Thanks,

Craig B.

Current Thread