RE: [xsl] Creating Footnote Ids

Subject: RE: [xsl] Creating Footnote Ids
From: "Trevor Nicholls" <trevor@xxxxxxxxxxxxxxxxxx>
Date: Thu, 5 Jul 2007 18:52:25 +1200
Can you not achieve this by passing a parameter to all your templates? Each
applied template then receives this parameter, with a value which identifies
the lineage of its parent, and passes on a parameter of the same name whose
value consists of the supplied parameter value plus the string which
identifies the location of the current node within its parent.

Cheers
Trevor

-----Original Message-----
From: Jeff Sese [mailto:jsese@xxxxxxxxxxxx] 
Sent: Thursday, 5 July 2007 6:16 p.m.
To: Xsl-List
Subject: [xsl] Creating Footnote Ids

Hi,

I have an XML document that has footnote mark-up, this can appear in any 
level within the document. I want to create an id attribute that will 
have a value depending on the ancestors of the footnote element.

for example:

<book>
    <preface>
       <footnote/>
       <footnote/>
       <footnote/>
       <footnote/>
    </preface>
    <part>
        <chapter>
           <footnote/>
           <footnote/>
            <footnote/>
        </chapter>
        <chapter>
           <footnote/>
           <footnote/>
            <footnote/>
        </chapter>
       <footnote/>
       <footnote/>
       <footnote/>
    </part>
    <chapter>
       <footnote/>
       <footnote/>
       <footnote/>
    </chapter>
</book>

would turn to:

<book>
    <preface>
       <footnote id="PRE1FN1"/>
       <footnote id="PRE1FN2"/>
       <footnote id="PRE1FN3"/>
       <footnote id="PRE1FN4"/>
    </preface>
    <part>
        <chapter>
           <footnote id="P1C1FN1"/>
           <footnote id="P1C1FN2"/>
           <footnote id="P1C1FN3"/>
        </chapter>
        <chapter>
           <footnote id="P1C2FN1"/>
           <footnote id="P1C2FN2"/>
           <footnote id="P1C2FN3"/>
        </chapter>
       <footnote id="P1FN1"/>
       <footnote id="P1FN2"/>
       <footnote id="P1FN3"/>
    </part>
    <chapter>
       <footnote id="C1FN1"/>
       <footnote id="C1FN2"/>
       <footnote id="C1FN3"/>
    </chapter>
</book>

How can i do this in XSLT 2.0? I tried doing it by creating a variable 
for the count of each ancestor but I can get it correct.

    <xsl:template match="footnote">
        <xsl:variable name="part">
            <xsl:number count="part" level="single" format="1"/>
        </xsl:variable>
        <xsl:variable name="chapter">
            <xsl:number count="chapter" level="single" format="1"/>
        </xsl:variable>
        <xsl:variable name="footnote">
            <xsl:number count="footnote" from="chapter | part | preface" 
level="any" format="1"/>
        </xsl:variable>
        <xsl:variable name="id" select="if (ancestor::book and 
ancestor::part and ancestor::chapter) then concat('P', $part, 'C', 
$chapter, 'N', $footnote)
            else if (ancestor::book and ancestor::chapter) then 
concat('C', $chapter, 'N', $footnote)
            else concat('N', $footnote)"/>
        <xsl:message select="$id"/>
        <footnote id="{$id}"/>
    </xsl:template>

Thanks,
Jeff

Current Thread