xsl-list
[Top] [All Lists]

[xsl] Creating Footnote Ids

2007-07-04 23:16:30
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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>