[xsl] Creating Footnote Ids

Subject: [xsl] Creating Footnote Ids
From: Jeff Sese <jsese@xxxxxxxxxxxx>
Date: Thu, 05 Jul 2007 14:15:46 +0800
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