Internal links

Links can be set to internal destinations (within the given document) and may be used in the PDF as hyperlinks.

In this section the use of the <fo:basic-link> element for the setting of internal references which can also be used as hyperlinks is demonstrated by an example. The functionality of the hyperlink is dependent on the appropriate support in the PDF generation and in the PDF-Viewer.

Excerpt from the DTD:

<!ELEMENT Section (Title, ...)>            ❶ 
<!ATTLIST Section ID CDATA #IMPLIED>       ❶ 
<!ELEMENT Title (#PCDATA)>
...
<!ELEMENT iR (#PCDATA)>                    ❷ 
<!ATTLIST iR RefID CDATA #REQUIRED>        ❷ 

The <Section> element is the reference destination. The ID attribute gives each section to which reference shall be made an unique identification for the entire document.

In this example it is simply assumed that the section titles contain only text and no substructures.

With the <iR> elment reference can be made to individual sections (the same attribute value for RefID as for the destination with ID). For pragmatic reasons, the attribute value range is defined with CDATA, and not with IDREF. However, the attribute values for ID and RefID have to syntactically conform to the rules for XML IDs because they have to be equivalent to the IDs and IDREFs in the XSL-FO representation. Otherwise the hyperlink will not work. The #PCDATA content only takes in help texts in the editing process, but in this example it is not used in the processing.

Excerpt from the XML instance:

<Section ID="Example">
   <Title>Example-Application</Title>                 ❶ 
...
</Section> 
...... 
...<iR RefID="Example">Example-Application</iR> ...   ❷
..........

❶ The given section (the reference destination) has the ID with the value Example.

❷ The reference <iR> also has the value Example for the RefID attribute. The textual content of <iR> only helps to orientate in the editing process. In the processing it is replaced by the link to the appropriate page and the title of the section which is linked.

Stylesheet:

<xsl:template match="Section">
   <fo:block id="{generate-id()}">                                           ❶ 
      <xsl:apply-templates/>                                                 ❷
   </fo:block>
</xsl:template>
<xsl:template match="iR">
   <xsl:for-each select="//Section[contains(@ID,current()/@RefID)]">         ❸ 
      <fo:inline text-decoration="underline">                                ❹ 
         <fo:basic-link internal-destination="{generate-id()}">              ❺ 
            (see page <fo:page-number-citation ref-id="{generate-id()}"/>,   ❻ 
             <xsl:value-of select="Title"/>)                               
         </fo:basic-link>
      </fo:inline>
   </xsl:for-each>
</xsl:template>

❶ An id attribute is added to the block which shall enclose the entire content of the section. The value of the attribute shall be generated by the XSLT processor and is specified with the generate-id() function. This identification is needed for the hyperlink to this section. It is not to confuse with the ID="Example" attribute for this section which is only used for the addressing of the section to which the link refers.

❷ In the specified block the entire content of <Section> is included taking into account the subordinated templates.

❸ For each section in which the value for the ID attribute is the same as for the RefID attribute of the given link, the following instructions are followed.

❹ For the reference an inline element is created whose content shall be underlined in order to indicate that a link is provided at this position.

❺ With <fo:basic-link> the link is specified. The internal-destination attribute receives the id value generated with the generate-id() function of the block enclosing the destination section.

At first, the reference starts with generated text. The text which encloses the link and its punctuation have to be adjusted accordingly to this reference text.

❻ With <fo:page-number-citation> and the generate-id() function reference is made to the first page of the destination section.

Finally, the reference text also includes the title of the destination section. Please note that <Title> has to be a child element of <Section>. With <xsl:value-of> the complete title would only be displayed if this title only contains text and no substructures.

 

<< back next >>

Copyright © dpunkt.verlag GmbH 2004
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "XSL-FO in der Praxis" ("XSL-FO in practice") is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.

dpunkt.verlag GmbH, Ringstraße 19B, 69115 Heidelberg, fon 06221-14830, fax 06221-148399, hallo@dpunkt.de