Announcement

Collapse
No announcement yet.

Komplexe Ausgabe (Zeilen zu Spalten)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Komplexe Ausgabe (Zeilen zu Spalten)

    Hallo,

    ich habe ein XML dass in etwa so aufgebaut ist:
    Code:
    <ROW>
    <JAHR>2009</JAHR>
    <LAND>DE</LAND>
    <PREIS>12</PREIS>
    </ROW>
    <ROW>
    <JAHR>2010</JAHR>
    <LAND>DE</LAND>
    <PREIS>13</PREIS>
    </ROW>
    <ROW>
    <JAHR>2009</JAHR>
    <LAND>E</LAND>
    <PREIS>123</PREIS>
    </ROW>
    <ROW>
    <JAHR>2010</JAHR>
    <LAND>F</LAND>
    <PREIS>232</PREIS>
    </ROW>
    Ich möchte nun eine Tabelle in HTML erzeugen, die in etwa so aussehen soll:
    Code:
    +----+----+----+
    !Land!2009!2010!
    !DE  !12  !13  !
    !E   !123 !    !
    !F   !    !232 !
    +----+----+----+
    wobei ich nicht weiss, bei welchem Jahr es anfängt noch wieviel Jahre oder wieviel Länder kommen.

    Ist sowas mit XSLT überhaupt möglich?

    Vielen Dank im Voraus

    Alfonso

  • #2
    Hier ist eine XSLT 2.0 Lösung:
    Code:
    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xsd"
      version="2.0">
      
      <xsl:output method="html" indent="yes"/>
      
      <xsl:template match="ROWS">
        <xsl:variable name="years" as="xsd:integer*">
          <xsl:perform-sort select="distinct-values(ROW/JAHR/xsd:integer(.))">
            <xsl:sort select="."/>
          </xsl:perform-sort>
        </xsl:variable>
        <table>
          <thead>
            <tr>
              <th>Land</th>
              <xsl:for-each select="$years">
                <th><xsl:value-of select="."/></th>
              </xsl:for-each>
            </tr>
          </thead>
          <tbody>
            <xsl:for-each-group select="ROW" group-by="LAND">
              <tr>
                <td><xsl:value-of select="current-grouping-key()"/></td>
                <xsl:for-each select="$years">
                  <td>
                    <xsl:value-of select="if (current-group()[xsd:integer(JAHR) eq current()])
                                          then current-group()[xsd:integer(JAHR) eq current()]/PREIS
                                          else '&#160;'"/>
                  </td>
                </xsl:for-each>
              </tr>
            </xsl:for-each-group>
          </tbody>
        </table>
      </xsl:template>
    
    </xsl:stylesheet>
    Angewendet auf
    Code:
    <ROWS>
    <ROW>
    <JAHR>2009</JAHR>
    <LAND>DE</LAND>
    <PREIS>12</PREIS>
    </ROW>
    <ROW>
    <JAHR>2010</JAHR>
    <LAND>DE</LAND>
    <PREIS>13</PREIS>
    </ROW>
    <ROW>
    <JAHR>2009</JAHR>
    <LAND>E</LAND>
    <PREIS>123</PREIS>
    </ROW>
    <ROW>
    <JAHR>2010</JAHR>
    <LAND>F</LAND>
    <PREIS>232</PREIS>
    </ROW>
    </ROWS>
    erhält man als Ausgabe:
    Code:
    <table>
       <thead>
          <tr>
             <th>Land</th>
             <th>2009</th>
             <th>2010</th>
          </tr>
       </thead>
       <tbody>
          <tr>
             <td>DE</td>
             <td>12</td>
             <td>13</td>
          </tr>
          <tr>
             <td>E</td>
             <td>123</td>
             <td>&nbsp;</td>
          </tr>
          <tr>
             <td>F</td>
             <td>&nbsp;</td>
             <td>232</td>
          </tr>
       </tbody>
    </table>

    Comment

    Working...
    X