Yes, that is the hazard of customizing one of the templates.

If you are willing to add an extra step to your processing, you can run the 
file through an identity stylesheet that leaves everything the same except the 
xlink:href attribute for those links.  Then you could apply the regular 
stylesheet to format it.

If you want to do it all within your customization, then this should work:

<xsl:template match="link[@role = 'relative']">
  <xsl:variable name="new.element">
    <foo>
      <xsl:copy>
        <xsl:attribute name="xlink:href">
         <xsl:value-of select="concat('http://prefix/', @xlink:href)"/>
       </xsl:attribute>
       <xsl:apply-templates/>
      </xsl:copy>
    </foo>
  </xsl:variable>
  <xsl:apply-templates select="exsl:node-set($new.element)/foo/*"/>
</xsl:template>
</xsl:stylesheet>

This puts the modified copy of the element into a fake element, and saves the 
result tree fragment to a variable.  Then it converts the fragment to a 
node-set using the exsl:node-set() function, and then selects the modified link 
element to apply-templates to.  The fake element is needed to avoid processing 
the root of the temporary node set.  In DocBook XSL, the root element is 
handled in a special way to set up an FO document with page-masters, and you 
want to avoid that by *not* processing the root element.  That's the tricky 
part I was referring to earlier.

Bob Stayton
Sagehill Enterprises
[email protected]


From: natk 
Sent: Monday, September 16, 2013 3:30 PM
To: Bob Stayton 
Subject: Re: [docbook-apps] Convert a relative file link to a URL in docbook


Thanks for the prompt reply. Yes I had thought of that solution, but it does 
not seem very maintainable. If that template changes when the library is 
upgraded, my customization layer will mean that I don't see those changes. 





On Tue, Sep 17, 2013 at 7:48 AM, Bob Stayton <[email protected]> wrote:

  Hi Natalie,
  I see what you are trying to do, by using XSL to modify the incoming element 
and then use apply-templates on the result.  However, that is tricky to do in 
XSL, because the xsl:copy creates what's called a "result tree fragment", which 
is not a node in the document, so you cannot directly apply templates to it.  
It can be converted to a node, but then it loses any context, further 
complicating the process.

  I would instead suggest you copy and modify the template named 'simple.xlink' 
from fo/inline.xsl.  That template is used to form the fo:basic-link element 
from any number of DocBook linking elements.  Unfortunately, it is a big 
template because of the variety of linking methods in DocBook, but your change 
is pretty simple.  In your customized template, change this:

         <!-- otherwise it's a URI -->
          <xsl:otherwise>
            <fo:basic-link external-destination="url({$xhref})">
              <xsl:apply-templates select="." mode="simple.xlink.properties"/>
              <xsl:copy-of select="$content"/>
            </fo:basic-link>

  to this:

          <!-- otherwise it's a URI -->
          <xsl:otherwise>
            <xsl:variable name="uri">
              <xsl:choose>
                <xsl:when test="$node/@role = 'relative'"> 

                  <xsl:value-of select="concat('http://prefix/', @xlink:href)"/>

                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="@xlink:href"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <fo:basic-link external-destination="url({$uri})">
              <xsl:apply-templates select="." mode="simple.xlink.properties"/>
              <xsl:copy-of select="$content"/>
            </fo:basic-link>

  Bob Stayton
  Sagehill Enterprises
  [email protected]


  From: Natalie Kershaw 
  Sent: Monday, September 16, 2013 12:13 PM
  To: [email protected] 
  Subject: [docbook-apps] Convert a relative file link to a URL in docbook


  I am using docbook as a source for HTML and PDF output. In the docbook 
source, I have a series of links which are relative (pointing to javadoc 
files), which is fine for the HTML output because the javadoc files are 
deployed alongside the original document. For the PDF output, I would like to 
convert these to absolute links by way of prepending a URL prefix. Is this 
possible? I have added a 'relative' role to these links and have tried adding a 
fo customization layer:

<xsl:template match="d:link[@role='relative']">
  <xsl:copy>
    <xsl:attribute name="xlink:href">
      <xsl:value-of select="concat('http://prefix/', @xlink:href)"/>
    </xsl:attribute>
  </xsl:copy>
  <xsl:apply-templates select="d:link"/>
</xsl:template>
This seems to half-work in that it does prepend the prefix to the link href, 
but then it does not convert the link element into FO.

  Any ideas, or other ways this could be done?


Reply via email to