<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="/pool/xslt_ht.xslt" type="application/xml"?>
<xsl:stylesheet
  xmlns = "http://www.w3.org/1999/xhtml"
  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:xl = "http://www.w3.org/1999/xlink"
  version = "1.0"
  exclude-result-prefixes = "d ht xl"
>

<d:info xmlns="http://herbaer.de/xmlns/20051201/doc">
  <title>xhtml_minimize.xslt</title>
  <subtitle>XHTML-Datei "minimieren"</subtitle>
  <date>2011-06-11</date>
  <author>
    <personname>
      <firstname>Herbert</firstname>
      <surname>Schiemann</surname>
    </personname>
    <email>h.schiemann@herbaer.de</email>
  </author>
</d:info>

<d:para>Wurzel: nur Wurzelelement und Verarbeitungsanweisung übernehmen</d:para>
<xsl:template match="/">
  <xsl:copy-of select="processing-instruction()"/>
  <xsl:apply-templates select="*"/>
</xsl:template>

<d:para>Manche Elemente enthalten nur Attribute und Kindelemente</d:para>
<xsl:template
  match = "
    ht:html | ht:head | ht:body | ht:audio
    | ht:table | ht:thead | ht:tbody | ht:tfoot | ht:tr
    | ht:ul | ht:ol | ht:dl | ht:audio | ht:object
  "
>
  <xsl:element name="{local-name()}">
    <xsl:apply-templates select="@*|*"/>
  </xsl:element>
</xsl:template>

<d:para>HTML-Elemente mit oder ohne Text-Inhalt</d:para>
<xsl:template match="ht:section | ht:div | ht:td | ht:select | ht:dd | ht:a">
  <xsl:copy>
    <xsl:choose>
      <xsl:when test="text() [string-length (normalize-space(.)) &gt; 0] | ht:span">
        <xsl:apply-templates select="@*|*|text()"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="@*|*"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:copy>
</xsl:template>

<d:para>Alle anderen Elemente kopieren</d:para>
<xsl:template match="*">
  <xsl:copy>
    <xsl:apply-templates select="@*|*|text()"/>
  </xsl:copy>
</xsl:template>

<d:para>Attribute "normalisieren"</d:para>
<xsl:template match="@*">
  <xsl:copy>
    <xsl:value-of select="normalize-space(.)"/>
  </xsl:copy>
</xsl:template>

<d:para>Text normalisieren</d:para>
<xsl:template match="text() [not (ancestor::ht:pre)]">
  <xsl:variable name="p" select="(preceding-sibling::* | preceding-sibling::text()) [last()]"
  />
  <xsl:variable name="f" select="(following-sibling::* | following-sibling::text()) [1]"/>
  <xsl:variable name="t">
    <xsl:if
      test =
      "not ($p) or contains ('#section#div#p#br#', concat ('#', local-name ($p), '#'))"
    >
      <xsl:text>v</xsl:text>
    </xsl:if>
    <xsl:if
      test =
      "not ($f) or contains ('#section#div#p#br#', concat ('#', local-name ($f), '#'))"
    >
      <xsl:text>h</xsl:text>
    </xsl:if>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$t = 'vh'">
      <xsl:value-of select="normalize-space(.)"/>
    </xsl:when>
    <xsl:when test="$t = 'v'">
      <xsl:variable name="tmp" select="normalize-space (concat (., 'x'))"/>
      <xsl:value-of select="substring ($tmp, 1, string-length ($tmp) - 1)"/>
    </xsl:when>
    <xsl:when test="$t = 'h'">
      <xsl:variable name="tmp" select="normalize-space (concat ('x', .))"/>
      <xsl:value-of select="substring ($tmp, 2)"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="tmp" select="normalize-space (concat ('x', ., 'x'))"/>
      <xsl:value-of select="substring ($tmp, 2, string-length ($tmp) - 2)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
