<?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:xl = "http://www.w3.org/1999/xlink"
  version = "1.0"
  exclude-result-prefixes = "d xl"
>

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

<d:para>
Sollen Verarbeitungsanweisungen übernommen werden (1 ja / 0 nein)
</d:para>
<xsl:param name="p_pi" select="1"/>

<d:para>
Soll Text normalisiert werden? (0 nein / 1 moderat / 2 ja)
</d:para>
<xsl:param name="p_normtext" select="1"/>

<d:para>
Leerzeichen aus "Container"-Elementen entfernen (0 nein / 1 ja)
</d:para>
<xsl:param name="p_purecont" select="1"/>

<xsl:output method="xml" encoding="utf-8"/>

<d:para>
Verarbeitungsanweisungen
</d:para>
<xsl:template match="processing-instruction()">
  <xsl:if test="$p_pi &gt; 0">
    <xsl:copy-of select="."/>
  </xsl:if>
</xsl:template>

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

<d:para>Alle anderen Elemente kopieren</d:para>
<xsl:template match="*">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:choose>
      <xsl:when test="$p_purecont = 0 or text() [string-length (normalize-space(.)) &gt; 0]"
      >
        <xsl:apply-templates select="*|text()|processing-instruction()"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="*|processing-instruction()"/>
      </xsl:otherwise>
    </xsl:choose>
  </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()">
  <xsl:choose>
    <xsl:when test="$p_normtext = 0">
      <xsl:value-of select="."/>
    </xsl:when>
    <xsl:when test="$p_normtext = 1">
      <xsl:variable name="tmp" select="normalize-space (concat ('x', ., 'x'))"/>
      <xsl:value-of select="substring ($tmp, 2, string-length ($tmp) - 2)"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="normalize-space(.)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
