[xml] Rewriting URLs



I am trying to implement a "document insert" using XSLTPROC. I have a
table of contents that I want to insert into every document on my
website. I want to rewrite the links in the menu as I insert it so that
they end up being relative to the new containing document.

The core of my rewrite is currently being done by:

  <xsl:template match="*" mode="menu">
    <xsl:element name="{name()}">
      <xsl:apply-templates mode="menu" select="*|@*|text()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="@href|@src" mode="menu">
    <xsl:attribute name="{name()}">
      <!-- If the href is not absolute, patch the offset: -->
      <xsl:if test="substring(., 1, 1) != '#'">
        <xsl:value-of select="$webroot"/>
        <xsl:text>/</xsl:text>
      </xsl:if>
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

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

  <xsl:template match="text()" mode="menu">
    <xsl:value-of select="."/>
  </xsl:template>

The parameter $webroot holds the directory of the menu insert file.

The difficulty is that I need to *parameterize* this. That is, I need to
be able to send in different values for $webroot (which is currently a
global parameter). [I am aware that the test in the XSL:IF is not
sufficient in general]

Can someone suggest a way to do this in XSLTPROC? I thought about using
a named template and calling it, sticking each of the cases into a
CHOOSE block, but it's not obvious how to select out the attrbute cases.

Suggestions would be greatly appreciated.


shap




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