2017-10-10 1 views
0

Ich habe folgende XML-DateiXSLT-Transformation der Daten in Tabelle incompleteXML

<root> 
 
    <entries> 
 
    <entry> 
 
     <id>1</id> 
 
     <title>entry A</title> 
 
     <group>1</group> 
 
     <category>a</category> 
 
    </entry> 
 
    <entry> 
 
     <id>2</id> 
 
     <title>entry B</title> 
 
     <group>1</group> 
 
     <category>c</category> 
 
    </entry> 
 
    <entry> 
 
     <id>3</id> 
 
     <title>entry C</title> 
 
     <group>2</group> 
 
     <category>b</category> 
 
    </entry> 
 
    <entry> 
 
     <id>4</id> 
 
     <title>entry D</title> 
 
     <group>2</group> 
 
     <category>c</category> 
 
    </entry> 
 
    <entry> 
 
     <id>5</id> 
 
     <title>entry E</title> 
 
     <group>3</group> 
 
     <category>a</category> 
 
    </entry> 
 
    <entry> 
 
     <id>6</id> 
 
     <title>entry F</title> 
 
     <group>4</group> 
 
     <category>c</category> 
 
    </entry> 
 
    </entries> 
 
    <groups> 
 
    <group id="1"> 
 
     <title>Group 1</title> 
 
    </group> 
 
    <group id="2"> 
 
     <title>Group 2</title> 
 
    </group> 
 
    <group id="3"> 
 
     <title>Group 3</title> 
 
    </group> 
 
    <group id="4"> 
 
     <title>Group 4</title> 
 
    </group> 
 
    </groups> 
 
    <categories> 
 
    <category id="a"> 
 
     <title>A</title> 
 
    </category> 
 
    <category id="b"> 
 
     <title>B</title> 
 
    </category> 
 
    <category id="c"> 
 
     <title>C</title> 
 
    </category> 
 
    </categories> 
 
</root>

die Tabellendefinition enthält (Knotengruppen entspricht Zeilen und Kategorieknoten entspricht Spalten) und Tabelleneinträge. Jeder Eintrag wird durch die Gruppen- und Spalten-ID identifiziert, und die Einträge sind nicht für alle Zellen definiert.

Ich brauche als Ausgabe eine Tabelle, wie dieser:

<table> 
 
    <thead> 
 
    <tr> 
 
     <th>Groups</th> 
 
     <th>A</th> 
 
     <!-- category name --> 
 
     <th>B</th> 
 
     <!-- category name --> 
 
     <th>C</th> 
 
     <!-- category name --> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <th group-id="1">Group 1</th> 
 
     <!-- group name --> 
 
     <th group-id="1" category-id="a"><span>entry A</span></th> 
 
     <th group-id="1" category-id="b"></th> 
 
     <th group-id="1" category-id="c"><span>entry B</span></th> 
 
    </tr> 
 
    <tr> 
 
     <th group-id="3">Group 2</th> 
 
     <!-- group name --> 
 
     <th group-id="2" category-id="a"></th> 
 
     <th group-id="2" category-id="b"><span>entry C</span></th> 
 
     <th group-id="2" category-id="c"><span>entry D</span></th> 
 
    </tr> 
 
    <tr> 
 
     <th group-id="3">Group 3</th> 
 
     <!-- group name --> 
 
     <th group-id="3" category-id="a"><span>entry E</span></th> 
 
     <th group-id="3" category-id="b"></th> 
 
     <th group-id="3" category-id="c"></th> 
 
    </tr> 
 
    <tr> 
 
     <th group-id="4">Group 4</th> 
 
     <!-- group name --> 
 
     <th group-id="4" category-id="a"></th> 
 
     <th group-id="4" category-id="b"></th> 
 
     <th group-id="4" category-id="c"><span>entry F</span></th> 
 
    </tr> 
 
    </tbody> 
 
</table>

, die mit leeren Spannweiten alle Zellen der Tabelle füllen würde, wenn es keine Daten in ersten xml sind .

Jede Hilfe wird geschätzt!

+0

Nun, für Ihre Zeilen möchten Sie zum Beispiel ' ...' und der Inhalt jedes 'tr', das Sie ausfüllen von Ihrem bekannten '' entweder mit ' 'oder mit ähnlichen Anwendungsvorlagen. –

+0

Hallo! Danke für Ihre Antwort! ... könnte gut funktionieren, wenn ich alle Gruppen abgedeckt hätte durch Einträge. Es kann einige leere Gruppen geben, ohne dass ein Eintrag vorhanden ist. – oachkatzlschwoaf

+0

Ich habe gerade versucht, das zusammen bei http://xsltransform.net/bEzjRKP zu geigen, zumindest für das gepostete Sample scheint es der Ansatz funktioniert. Sie können natürlich auch 'groups/group' verarbeiten, um die' tr' Zeilen im Tabellenkörper zu erzeugen und mit einem Schlüssel auf die entsprechenden 'entry' Elemente zuzugreifen. –

Antwort

0

Basierend auf Ihre Kommentare möchten Sie vielleicht Schlüssel zum Querverweis aus dem gegebenen Gruppen eingesetzt werden:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 

<xsl:output method="html" indent="yes"/> 

<xsl:key name="group-title" match="groups/group/title" use="../@id"/> 

<xsl:key name="entry-group" match="entries/entry" use="group"/> 

<xsl:template match="root"> 
    <table> 
     <thead> 
      <tr> 
       <th>Groups</th> 
       <xsl:for-each select="categories/category"> 
        <th> 
         <xsl:value-of select="title"/> 
        </th> 
       </xsl:for-each> 
      </tr> 
     </thead> 
     <tbody> 
      <xsl:variable name="categories" select="categories/category"/> 
      <xsl:for-each select="groups/group"> 
       <tr> 
        <td group-id="{@id}"> 
         <xsl:value-of select="title"/> 
        </td> 
        <xsl:variable name="group-id" select="@id"/> 
        <xsl:for-each select="$categories"> 
         <xsl:variable name="group" select="key('entry-group', $group-id)"/> 
         <td group-id="{$group-id}" category-id="{@id}"> 
          <xsl:value-of select="$group[category = current()/@id]/title"/> 
         </td> 
        </xsl:for-each>     
       </tr> 
      </xsl:for-each> 
     </tbody> 
    </table> 
</xsl:template> 

</xsl:transform> 

http://xsltransform.net/bEzjRKP/1

+0

Danke! das hat für mich funktioniert! – oachkatzlschwoaf

Verwandte Themen