[xsl] finding lowest level in ancestor:: axis

Subject: [xsl] finding lowest level in ancestor:: axis
From: Dan Vint <dvint@xxxxxxxxx>
Date: Thu, 26 Jan 2006 13:56:10 -0800
I'm trying to build some links between a tag (table, graphic, illustration) and the referencing tag. So I have a <ref> tag that has an idref attribute that can reference these objects anywhere and these objects can appear directly under one of my division tags or they might be inside a table or code or some other lower level structure, ultimately in the link I want to build I need to know the containing division tag which is not always the immediate parent.

So I have a document that looks like:

<div1>
     <p>Some text here</p>
     <div2>
           <p>More text <ref idref="t1'/></p>
             <example>
                     <table id='t1'/>
             </example>
     </div2>
</div1>

I thought the following would work:

<template match="ref">
<xsl:variable name="parent"
   select="id(@idref)/ancestor::div10 |
	    id(@idref)/ancestor::div9 |
	    id(@idref)/ancestor::div8 |
	    id(@idref)/ancestor::div7 |
	    id(@idref)/ancestor::div6 |
	    id(@idref)/ancestor::div5 |
	    id(@idref)/ancestor::div4 |
	    id(@idref)/ancestor::div3 |
	    id(@idref)/ancestor::div2 |
	    id(@idref)/ancestor::div1                    	
   "/>
     <a href="{concat(generate-id($parent), '.html#', @idref)}">
          Table <xsl:number level="any" from="div1" count="table" format="1"/>
          <xsl:text> </xsl:text>		
      </a>
</xsl:template>

Instead of finding the first <div2> tag this seems to always locate the highest ancestor in the tree and comes back with the <div1>. I even changed the order of the select for the variable to be high to low and low to high and nothing changed. I though maybe there might be an evaluation order going on.

I thought about adding a [1] to each item in the select for the variable but I don't think that changes anything, both the div2 and div1 are the first of that type. I think my only option is to split this select out into a chose statement which would force the order, is there another way to do this instead of:

<xsl:variable name="parent">
    <xsl:choose>
        <xsl:when test="id(@idref)/ancestor::div10">
	  <xsl:value-of select="id(@idref)/ancestor::div10"/>
        </xsl:when>
....
</xsl:choose>
</xsl:variable>

thanks
..dan

PS still using XSLT v1
---------------------------------------------------------------------------
Danny Vint

Specializing in Panoramic Images of California and the West
http://www.dvint.com

voice: 510-522-4703

When H.H. Bennett was asked why he preferred to be out
shooting landscapes rather than spending time in his portrait studio:

"It is easier to pose nature and less trouble to please."

http://www.portalwisconsin.org/bennett_feature.cfm

Current Thread