2017-10-10 27 views
0

nous avons suivant fichier XMLtransformation XSLT de données incompleteXML dans la table

<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>

qui contient la définition de table (nœud groupes correspond à des rangées et le noeud de catégorie correspond à des colonnes) et les entrées de table. Chaque entrée est identifiée par un identifiant de groupe et de colonne, et les entrées ne sont pas définies pour toutes les cellules.

J'ai besoin en tant que sortie d'une table, comme celui-ci:

<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>

qui remplirait toutes les cellules de la table avec des portées vides, s'il n'y a pas de données au format XML initial .

Toute aide appréciée!

+0

Eh bien, pour vos lignes que vous voulez par exemple ' ...' et le contenu de chaque 'tr' que vous remplissez à partir de vos colonnes ' 'soit avec' 'ou avec un template-apply similaire. –

+0

Salut! Merci pour votre réponse! ... pourrait fonctionner correctement, si tous les groupes étaient couverts par des entrées. Il peut y avoir des groupes vides, sans aucune entrée. – oachkatzlschwoaf

+0

J'ai juste essayé de bidouiller cela ensemble à http://xsltransform.net/bEzjRKP, au moins pour l'échantillon posté, il semble que l'approche fonctionne. Mais vous pouvez bien sûr aussi traiter 'groups/group' pour créer les lignes' tr' dans le corps de la table et utiliser une clé pour accéder aux éléments 'entry' correspondants. –

Répondre

0

Sur la base de vos commentaires que vous pouvez utiliser les touches de référence croisée de vos groupes donnés:

<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

Merci! ça a marché pour moi! – oachkatzlschwoaf