<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="/pool/xslt_ht.xslt" type="application/xml"?>
<xsl:stylesheet
  xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
  xmlns:d = "http://herbaer.de/xmlns/20051201/doc"
  xmlns:ht = "http://www.w3.org/1999/xhtml"
  xmlns = "http://www.w3.org/1999/xhtml"
  exclude-result-prefixes = "d xsl ht"
  version = "1.0"
>

<d:info xmlns="http://herbaer.de/xmlns/20051201/doc">
  <title>ht_abs2rel.xslt</title>
  <subtitle>Präfixe von Verweiszielen ersetzen</subtitle>
  <date>2016-02-06</date>
  <author>
    <personname>
      <firstname>Herbert</firstname>
      <surname>Schiemann</surname>
    </personname>
    <email>h.schiemann@herbaer.de</email>
  </author>
</d:info>

<d:para role="stylesheet">
Diese Transformation Präfixe von Verweiszielen ("alte" Präfixe)
durch andere ("neue") Präfixe.
Sie ist insbesondere dazu gedacht, absolute Verweise durch relative Verweise zu ersetzen.
</d:para>

<d:para>
Komma-getrennte Liste der alten Präfixe, die ersetzt werden
</d:para>
<xsl:param name="p_oprf"/>

<d:para>
Komma-getrennt Liste der neuen Präfixe,
die anstelle der alten Präfixe eingesetzt werden
</d:para>
<xsl:param name="p_nprf"/>

<d:para>
Verarbeitungsanweisungen, Text und Attribute werden kopiert
</d:para>
<xsl:template match="processing-instruction() | text() | @*">
  <xsl:copy-of select="."/>
</xsl:template>

<d:para>
Elemente werden &#x201e;rekursiv&#x201d; kopiert
</d:para>
<xsl:template match="*">
  <xsl:copy>
    <xsl:apply-templates select="@* | text() | * | processing-instruction()"/>
  </xsl:copy>
</xsl:template>

<d:para>
Verweisziele
</d:para>
<xsl:template match="ht:a/@href">
  <xsl:attribute name="href">
    <xsl:call-template name="replace_prf">
      <xsl:with-param name="olst" select="$p_oprf"/>
      <xsl:with-param name="nlst" select="$p_nprf"/>
    </xsl:call-template>
  </xsl:attribute>
</xsl:template>

<d:para>
Präfix ersetzen
</d:para>
<xsl:template name="replace_prf">
  <!-- Text, dessen Präfix ersetzt wird -->
  <xsl:param name="txt" select="."/>
  <!-- Liste der "alten" Präfixe -->
  <xsl:param name="olst" select="''"/>
  <!-- Liste der "neuen" Präfixe -->
  <xsl:param name="nlst" select="''"/>
  <!-- Trennzeichenfolge -->
  <xsl:param name="sep" select="','"/>
  <xsl:variable name="o">
    <xsl:choose>
      <xsl:when test="contains ($olst, $sep)">
        <xsl:value-of select="substring-before ($olst, $sep)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$olst"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="n">
    <xsl:choose>
      <xsl:when test="contains ($nlst, $sep)">
        <xsl:value-of select="substring-before ($nlst, $sep)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$nlst"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="starts-with ($txt, $o)">
      <xsl:value-of select="concat ($n, substring-after ($txt, $o))"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="otail">
        <xsl:choose>
          <xsl:when test="contains ($olst, $sep)">
            <xsl:value-of select="substring-after ($olst, $sep)"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="''"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="ntail">
        <xsl:choose>
          <xsl:when test="contains ($nlst, $sep)">
            <xsl:value-of select="substring-after ($nlst, $sep)"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="''"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:choose>
        <xsl:when test="string-length ($otail) &gt; 0 or string-length ($ntail) &gt; 0">
          <xsl:call-template name="replace_prf">
            <xsl:with-param name="txt" select="$txt"/>
            <xsl:with-param name="olst" select="$otail"/>
            <xsl:with-param name="nlst" select="$ntail"/>
            <xsl:with-param name="sep" select="$sep"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$txt"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
