Re: crossreferences in installed xml



Here is my current attempt at link-conversion in xsl. The general
approach is the following: 

Copy everything except link elements.
For link elements, check if the linkend is a valid idref. If it is,
copy it. Otherwise check the documents listed in index.xml to see if
one of them contains the id. In that case, construct a ulink using the
url found in index.xml and the id. Otherwise construct a man: ulink.

The index.xml contains a list of docbook files together with uri like
this:

<index>
  <document uri="ghelp://test/test.xml">test/test.xml</document>
  <document uri="ghelp://test2/test2.xml">test2/test2.xml</document>
</index>

Since this is my first xsl stylesheet, and it is only tested on toy
examples so far. I would welcome comments on the general strategy and
the actual implementation from more experienced xsl coders.

Matthias



<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
  <xsl:param name="index" select="'index.xml'"/>

  <xsl:output method="xml"
	      doctype-public="-//OASIS//DTD DocBook XML V4.1.2//EN"
	     
doctype-system="http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"/>

  <xsl:template name="copy" match="*">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="//link">
    <xsl:variable name="linkend" select="@linkend"/>
    <xsl:variable name="linktext">
      <xsl:apply-templates/>
    </xsl:variable>
    <xsl:variable name="emitted">
      <xsl:choose>  
        <xsl:when test="id(@linkend)">
          <xsl:call-template name="copy"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:for-each select="document($index)/index/document">
            <xsl:variable name="uri" select="@uri"/>
	      <xsl:for-each select="document(text())">
                <xsl:if test="id($linkend)">
                  <xsl:element name="ulink">
                    <xsl:attribute name="url">
                      <xsl:value-of select="concat($uri, '#',
$linkend)"/>
                    </xsl:attribute>
                    <xsl:value-of
select="$linktext"/>                       
                  </xsl:element>
                </xsl:if>
              </xsl:for-each>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
    <xsl:choose> 
      <xsl:when test="count($emitted)">
        <xsl:copy-of select="$emitted"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:element name="ulink">
          <xsl:attribute name="url">
            <xsl:value-of select="concat('man:', $linkend)"/>
          </xsl:attribute>
          <xsl:value-of select="$linktext"/>                       
        </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]