Announcement

Collapse
No announcement yet.

Dynamische Gruppierung nach Elementnamen

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

  • Dynamische Gruppierung nach Elementnamen

    Hallo,

    Ich habe ein definiertes und nicht abänderbares Eingabeformat, das ich in ein Ausgabeformat umwandeln muss. Die Quelle ist ein Dataset, das ich aus einem Textfile ziehe, und das Ausgabeformat ist SpreadsheetML (XML-Format für Excel). Details darüber sind aber nicht so wichtig für diese Frage.

    Das Format dieses Datasets ist folgendermassen (ein Beispiel):

    HTML Code:
    <dataset>
    <Table1>
    <col1>Wert a</col1>
    <col2>Wert b</col2>
    <col3>Wert c</col3>
    </Table1>
    <Table1>
    <col1>Wert d</col1>
    <col2>Wert e</col2>
    <col3>Wert f</col2>
    </Table1>
    <Table2>
    <col1>Wert g</col1>
    <col2>Wert h</col2>
    </Table2>
    </dataset>
    Das Ausgangsfromat sieht so aus:


    HTML Code:
    <Worksheet>
    <table>
    <row>
    <cell></cell>
    </row>
    </table>
    </Worksheet>
    Bei Ausgangsformat gibt es also für jede Tabelle ein Tag, im Eingangsformat werden aber Tabellen und Rows sozusagen in einem Tag zusammengefasst.

    Ich muss jetzt pro Tabelle ein Worksheet erstellen. Im Beispiel oben wären das 2 Worksheets. Das grösste Problem dabei ist, dass die Anzahl Tabellen variabel ist. Bis jetzt habe ich es "hardcodiert" für eine feste Anzahl Tabellen, schlussendlich muss es aber variabel sein.

    Eingesetzt werden kann leider nur XSLT1.0, die Version 2.0 wird von meiner Entwicklungsumgebung (Visual Studio) noch nicht unterstützt.

    Ich wäre sehr dankbar für Hilfe, weil ich jetzt nämlich schon einige Tage an dem Problem rumkaue und es einfach nicht schaffe.


    PS: Hier noch mein aktueller XSLT-Code, falls meine Beschreibung noch nicht ganz klar ist:
    HTML Code:
    <xsl:stylesheet version="1.0"
     xmlns="urn:schemas-microsoft-com:office:spreadsheet"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxsl="urn:schemas-microsoft-com:xslt"
     xmlns:user="urn:my-scripts"
     xmlns:o="urn:schemas-microsoft-com:office:office"
     xmlns:x="urn:schemas-microsoft-com:office:excel"
     xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
    
      <xsl:template match="/">
        <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:o="urn:schemas-microsoft-com:office:office"
          xmlns:x="urn:schemas-microsoft-com:office:excel"
          xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:html="http://www.w3.org/TR/REC-html40">
          <Styles>
            <Style ss:ID="s21">
              <NumberFormat ss:Format="0"/>
            </Style>
          </Styles>
          <Worksheet ss:Name="Table">
            <Table x:FullColumns="1" x:FullRows="1">
              <Row>
                <xsl:for-each select="/*/Table[position() = 1]/*">
                  <Cell>
                    <Data ss:Type="String">
                      <xsl:value-of select="local-name()"/>
                    </Data>
                  </Cell>
                </xsl:for-each>
              </Row>
              <xsl:apply-templates select="/*/Table"/>
            </Table>
          </Worksheet>
          <Worksheet ss:Name="Zusammenstellung">
            <Table x:FullColumns="1" x:FullRows="1">
              <Row>
                <xsl:for-each select="/*/Zusammenstellung[position() = 1]/*">
                  <Cell>
                    <Data ss:Type="String">
                      <xsl:value-of select="local-name()"/>
                    </Data>
                  </Cell>
                </xsl:for-each>
              </Row>
              <xsl:apply-templates select="/*/Zusammenstellung"/>
            </Table>
          </Worksheet>
        </Workbook>
      </xsl:template>
      <xsl:template match="/*/*">
        <Row>
          <xsl:apply-templates/>
        </Row>
      </xsl:template>
      <xsl:template match="GESICHERTEGB">
        <Cell ss:StyleID="s21">
          <Data ss:Type="Number">
            <xsl:value-of select="."/>
          </Data>
        </Cell>
      </xsl:template>
      <xsl:template match="MASTERSERVER | SICHERUNGSTYP | F7 | HALTEFRIST | JOBID | F15 | F16 | F17 | F18 | F19 | Haltezeit | SummeMonat">
        <Cell>
          <Data ss:Type="Number">
            <xsl:value-of select="."/>
          </Data>
        </Cell>
      </xsl:template>
      <xsl:template match="STARTSICHERUNG | ENDESICHERUNG | MASTERSERVER |SICHERUNGSTYP | NETBACKUPCLIENT | SICHERUNGSART | MEDIASERVER | POLICY | SCHEDULE | LAUFWERK">
        <Cell>
          <Data ss:Type="String">
            <xsl:value-of select="."/>
          </Data>
        </Cell>
      </xsl:template>
    </xsl:stylesheet>

  • #2
    Juhuu ich hab's nach stundenlangem Probieren, Experimentieren und Googeln endlich geschafft

    Das Prinzip ist folgendes:
    - Mit der ersten for-each Schleife durchlaufe ich alle Tabellen, deren Name nicht der gleiche ist wie der Name ihres nächsten Siblings (der nächsten Tabelle).
    Es wird also bei jeder Tabelle beim letzen Eintrag (der letzen "Row") der Inhalt der for-each Schleife ausgeführt.
    - In die Variable tablename speichere ich den Namen dieses Tags/dieser Tabelle und gebe dem Worksheet diesen Namen.
    - In der nächsten fore-each Schleife gebe ich die Titel der einzelnen Spalten der Tabelle aus. Dazu nutze ich die Key-Funktion, welche mir alle gleichnamigen Tabellen-Einträge ausgibt. Mit position() = 1 greife ich auf den ersten dieser Einträge zu.
    - Die Key-Funktion verwende ich auch für die Ausgabe der Tabelleninhalte.

    HTML Code:
    <xsl:stylesheet version="1.0"
     xmlns="urn:schemas-microsoft-com:office:spreadsheet"                
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxsl="urn:schemas-microsoft-com:xslt"
     xmlns:user="urn:my-scripts"
     xmlns:o="urn:schemas-microsoft-com:office:office"
     xmlns:x="urn:schemas-microsoft-com:office:excel"
     xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
    
      <xsl:key name="Tables" match="/*/*" use="local-name(.)" />
      <xsl:template match="/">
        <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:o="urn:schemas-microsoft-com:office:office"
          xmlns:x="urn:schemas-microsoft-com:office:excel"
          xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:html="http://www.w3.org/TR/REC-html40">      
          <Styles>
            <Style ss:ID="s21">
              <NumberFormat ss:Format="0"/>
            </Style>
          </Styles>
          <xsl:for-each select="/*/*[not(local-name() = local-name(./following-sibling::*))]">
            <xsl:variable name="tablename" select="local-name()" />
            <Worksheet ss:Name="{$tablename}">
              <Table x:FullColumns="1" x:FullRows="1">
                <Row>
                  <xsl:for-each select="key('Tables', $tablename)[position() = 1]/*">
                    <Cell>
                      <Data ss:Type="String">
                        <xsl:value-of select="local-name(.)"/>
                      </Data>
                    </Cell>
                    </xsl:for-each>
                  </Row>
                <xsl:apply-templates select="key('Tables', $tablename)"/>
              </Table>
            </Worksheet>
          </xsl:for-each>
        </Workbook>
      </xsl:template>
      <xsl:template match="/*/*">
        <Row>
          <xsl:apply-templates/>
        </Row>
      </xsl:template>
      <xsl:template match="GESICHERTEGB">
        <Cell ss:StyleID="s21">
          <Data ss:Type="Number">
            <xsl:value-of select="."/>
          </Data>
        </Cell>
      </xsl:template>
      <xsl:template match="MASTERSERVER | SICHERUNGSTYP | F7 | HALTEFRIST | JOBID | F15 | F16 | F17 | F18 | F19 | Haltezeit | SummeMonat">
        <Cell>
          <Data ss:Type="Number">
            <xsl:value-of select="."/>
          </Data>
        </Cell>
      </xsl:template>
      <xsl:template match="STARTSICHERUNG | ENDESICHERUNG | MASTERSERVER |SICHERUNGSTYP | NETBACKUPCLIENT | SICHERUNGSART | MEDIASERVER | POLICY | SCHEDULE | LAUFWERK">
        <Cell>
          <Data ss:Type="String">
            <xsl:value-of select="."/>
          </Data>
        </Cell>
      </xsl:template>
    </xsl:stylesheet>

    Comment

    Working...
    X