Hypertext links

Many electronic documents contain hypertext links. These are links from references to the objects they reference, which users may follow if they desire. The popular Web browsers have been built around this concept from the start. But for a link to work, the destination object must have a unique identifier of some kind. In some cases, the source document will already have a unique identifier, identified as such within a DTD:

<!DOCTYPE book [ ...
<!ELEMENT chapter (para+)>
<!ATTLIST chapter ident ID #REQUIRED>
... ]>
<book>
  <chapter ident="ch1">
  ...
  </chapter>
  <chapter>
    <para>Link to chapter
      <linkto idref="ch1">one</linkto>.
    </para>
  </chapter>
</book>

No special effort is needed to convert such a document using XSLT, because the attribute values that link the reference to the destination are already in place in the source document and so simply need to be copied-through to the output:

<BODY>
  <DIV><A NAME="ch1"></A>
  ...
  </DIV>
  <DIV>
    <P>Link to chapter<A HREF="#ch1">one</A>.</P>
  </DIV>
</BODY>

A more significant problem is linking to an object that does not have a unique identifier in the source document. Using XPath expressions, it is possible to identify such elements, but not to assign identifiers to them. The generate-id() function can be used to create a unique identifier. This function is replaced by a unique value for the element, assigned by the XSLT formatter. For example, this function can be used to give every paragraph in the output document a unique identifier. The node to assign the identifier to must be passed to it, and in this case the '.' symbol is used to identify the current node:

<xsl:template match="para">
  <P ID="generate-id(.)">
    <xsl:apply-templates/>
  </P>
</xsl:template>
  <P ID="N123456">...</P>
  <P ID="N123457">...</P>
  <P ID="N123458">...</P>

An element will always have the same unique identifier, even if this function is used to refer to the same element from elsewhere. This fact is very important. For example, every paragraph could be given a link to the chapter containing it, acting as a 'back-to-the-top' feature. It would be very important that all the generated identifiers are the same as each other, and as the chapter itself:

<xsl:template match="chapter">
  <DIV ID="generate-id(.)">
    <xsl:apply-templates/>
  </DIV>
</xsl:template>

<xsl:template match="para">
  <P><A HREF="#{generate-id(ancestor::chapter)">TOP </A>
    <xsl:apply-templates/>
  </P>
</xsl:template>


   <DIV ID="N987654">
      <P><A HREF="#N987654">TOP </A>...</P>
      <P><A HREF="#N987654">TOP </A>...</P>
      <P><A HREF="#N987654">TOP </A>...</P>
   </DIV>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset