Search code examples
xmlxsltxpathtableofcontents

Creating links within a document using a preexisting Table of Contents


I've got a large number of OCRed documents that have been partially marked up. I'm trying to create working links within the table of contents. The table of contents are formatted

<document>
  <text>
     <list>
        <item>Chapter 1<ref>7</ref></item>
        <item>Chapter 2<ref>27</ref></item>
        <item>Chapter 3<ref>54</ref></item>
        <item>Chapter 4<ref>77</ref></item>
     </list>
     <body>
      OCRED text <pb n="7-8" xml:id="VAB0003"/> OCRED text
     </body>   
</document>

Is there a way to test if the value of ref is the same as part of the value of @n in pb and then if so pull the value of @xml:id and use that in the ref element? Is there a less convoluted way of accomplishing this?


Solution

  • I think you're looking for something like this:

    <xsl:template match="ref*">
      <xsl:variable name="page" select="."/>
      <xsl:variable name="target" 
                    select="//pb[contains(
                            concat(' ',translate(@n,'-',' '),' '), 
                            concat(' ',$page,' '))]/@xml:id"/>
      <xsl:copy>
        <xsl:if test="$target">
          <xsl:attribute name='target'>
            <xsl:value-of select="$target"/>
          </xsl:attribute>
        </xsl:if>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>
    

    The complicated concatenations are there in the definition of target to ensure that you match on a full token of the page number; in XSLT 2.0 it can be done a little more gracefully and without the concats, but not much more succinctly.