2016-02-23 1 views
0

J'ai un fichier XML sur les données météorologiques de 3 villes différentes, maintenant je veux utiliser XSl le convertir en 3 tables selon les attributs de localisation (Calgary, Charlottetown et Ottawa) dans mon xml. Le xls que j'ai maintenant convertit juste le xml en une table, Quelqu'un peut-il me donner un indice sur la façon de convertir le fichier xml en différentes tables par l'attribut xml?
Ceci est mon xmlXSL Convertir le fichier XML en 3 tables

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="weatherdata.xsl"?> 
<weatherdata> 
<stationdata weatherdate="2015-1-1" location="Calgary"> 
<maxtemp>1.1°C</maxtemp> 
<mintemp>-6.1°C</mintemp> 
<totalrain>0 mm</totalrain> 
<totalsnow>0cm</totalsnow> 
</stationdata> 
<stationdata weatherdate="2015-1-2" location="Calgary"> 
<maxtemp>-3.4°C</maxtemp> 
<mintemp>-18.2°C</mintemp> 
<totalrain>0 mm</totalrain> 
<totalsnow>5cm</totalsnow> 
</stationdata> 
<stationdata weatherdate="2015-1-3" location="Calgary"> 
<maxtemp>-18.1°C</maxtemp> 
<mintemp>-21.1°C</mintemp> 
<totalrain>0 mm</totalrain> 
<totalsnow>1.6cm</totalsnow> 
</stationdata> 
<stationdata weatherdate="2015-01-01" location="Charlottetown"> 
<maxtemp>-3.5°C</maxtemp> 
<mintemp>-15°C</mintemp> 
<totalrain>0 mm</totalrain> 
<totalsnow>0.4cm</totalsnow> 
</stationdata> 
<stationdata weatherdate="2015-01-02" location="Charlottetown"> 
<maxtemp>-1°C</maxtemp> 
<mintemp>-13.2°C</mintemp> 
<totalrain>0 mm</totalrain> 
<totalsnow>0.6cm</totalsnow> 
</stationdata> 
<stationdata weatherdate="2015-01-03" location="Charlottetown"> 
<maxtemp>-11.8°C</maxtemp> 
<mintemp>-16.1°C</mintemp> 
<totalrain>0 mm</totalrain> 
<totalsnow>0cm</totalsnow> 
</stationdata> 
stationdata weatherdate="2015-01-01" location="Ottawa"> 
<maxtemp>-3°C</maxtemp> 
<mintemp>-8.1°C</mintemp> 
<totalrain>0 mm</totalrain> 
<totalsnow>0.2cm</totalsnow> 
</stationdata> 
<stationdata weatherdate="2015-01-02" location="Ottawa"> 
<maxtemp>-3.8°C</maxtemp> 
<mintemp>-15.8°C</mintemp> 
<totalrain>0 mm</totalrain> 
<totalsnow>0cm</totalsnow> 
</stationdata> 
<stationdata weatherdate="2015-01-03" location="Ottawa"> 
<maxtemp>-9.6°C</maxtemp> 
<mintemp>-15.5°C</mintemp> 
<totalrain>0 mm</totalrain> 
<totalsnow>18cm</totalsnow> 
</stationdata> 
</weatherdata> 

Ceci est mon xls

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/weatherdata"> 
<html> 
<head> 
<title>weather data</title> 
</head> 
<body> 

<h1>Calgary-Temperature Summary(2015)</h1> 
<table border="1"> 
    <tr> 
     <th>Data</th> 
     <th>Maximum Temperature(°C)</th> 
     <th>Minimum Temperature(°C)</th> 
     <th>Total Rain(mm)</th> 
     <th>Total Snow(cm)</th> 
    </tr> 
<xsl:apply-templates/> 
</table> 
</body> 
</html> 
</xsl:template> 

<xsl:template match="stationdata"> 
<tr> 
       <td><xsl:value-of select="@weatherdate"/></td> 
       <td><xsl:value-of select="maxtemp"/></td> 
       <td><xsl:value-of select="mintemp"/></td> 
       <td><xsl:value-of select="totalrain"/></td> 
       <td><xsl:value-of select="totalsnow"/></td> 
</tr> 
</xsl:template> 
</xsl:stylesheet> 
+0

les noms de lieu - Calgary, Charlottetown et Ottawa - connu à l'avance? IOW, peuvent-ils être codés en dur dans la feuille de style? –

Répondre

1

En supposant que les noms de lieu - Calgary, Charlottetown et Ottawa - sont connus à l'avance et toujours présent, vous pouvez faire:

XSLT 1,0

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

<xsl:variable name="thead"> 
    <tr> 
     <th>Data</th> 
     <th>Maximum Temperature(°C)</th> 
     <th>Minimum Temperature(°C)</th> 
     <th>Total Rain(mm)</th> 
     <th>Total Snow(cm)</th> 
    </tr> 
</xsl:variable> 

<xsl:template match="/weatherdata"> 
    <html> 
     <head> 
      <title>weather data</title> 
     </head> 
     <body> 
      <h1>Calgary-Temperature Summary(2015)</h1> 
      <table border="1"> 
       <xsl:copy-of select="$thead"/> 
       <xsl:apply-templates select="stationdata[@location='Calgary']"/> 
      </table> 
      <h1>Charlottetown-Temperature Summary(2015)</h1> 
      <table border="1"> 
       <xsl:copy-of select="$thead"/> 
       <xsl:apply-templates select="stationdata[@location='Charlottetown']"/> 
      </table> 
      <h1>Ottawa-Temperature Summary(2015)</h1> 
      <table border="1"> 
       <xsl:copy-of select="$thead"/> 
       <xsl:apply-templates select="stationdata[@location='Ottawa']"/> 
      </table> 
     </body> 
    </html> 
</xsl:template> 

<xsl:template match="stationdata"> 
    <tr> 
     <td><xsl:value-of select="@weatherdate"/></td> 
     <td><xsl:value-of select="maxtemp"/></td> 
     <td><xsl:value-of select="mintemp"/></td> 
     <td><xsl:value-of select="totalrain"/></td> 
     <td><xsl:value-of select="totalsnow"/></td> 
    </tr> 
</xsl:template> 

</xsl:stylesheet> 
+0

Merci beaucoup, Il filtre parfaitement les données dans les 3 villes différentes, mais toutes les données ne sont pas organisées dans les tableaux, j'ai essayé d'ajouter

tags, ça ne marche pas. @ michael.hor257k –

+0

"* toutes les données ne sont pas organisées dans les tableaux *" Je ne suis pas sûr de ce que vous entendez par là. Le résultat est trois tables, chacune avec trois lignes de données. Il y a 9 éléments 'stationdata' dans votre XML - alors qu'est-ce qui manque exactement? –

+0

Cela fonctionne maintenant, je viens de remplacer la méthode de sortie = "xml" dans le prologue de la méthode de sortie = "html". @ michael.hor257k –

1

Ceci est une question de regroupement. Puisque vous utilisez XSLT 1.0, vous pouvez utiliser Muenchian Grouping. Vous pouvez créer un xsl:key basé sur l'attribut location.

Ensuite, vous bouclez la première correspondance de chaque touche et affichez l'emplacement.

Ensuite, vous appliquez des modèles à toutes les correspondances avec cette clé.

XML entrée

<weatherdata> 
    <stationdata weatherdate="2015-1-1" location="Calgary"> 
     <maxtemp>1.1°C</maxtemp> 
     <mintemp>-6.1°C</mintemp> 
     <totalrain>0 mm</totalrain> 
     <totalsnow>0cm</totalsnow> 
    </stationdata> 
    <stationdata weatherdate="2015-1-2" location="Calgary"> 
     <maxtemp>-3.4°C</maxtemp> 
     <mintemp>-18.2°C</mintemp> 
     <totalrain>0 mm</totalrain> 
     <totalsnow>5cm</totalsnow> 
    </stationdata> 
    <stationdata weatherdate="2015-1-3" location="Calgary"> 
     <maxtemp>-18.1°C</maxtemp> 
     <mintemp>-21.1°C</mintemp> 
     <totalrain>0 mm</totalrain> 
     <totalsnow>1.6cm</totalsnow> 
    </stationdata> 
    <stationdata weatherdate="2015-01-01" location="Charlottetown"> 
     <maxtemp>-3.5°C</maxtemp> 
     <mintemp>-15°C</mintemp> 
     <totalrain>0 mm</totalrain> 
     <totalsnow>0.4cm</totalsnow> 
    </stationdata> 
    <stationdata weatherdate="2015-01-02" location="Charlottetown"> 
     <maxtemp>-1°C</maxtemp> 
     <mintemp>-13.2°C</mintemp> 
     <totalrain>0 mm</totalrain> 
     <totalsnow>0.6cm</totalsnow> 
    </stationdata> 
    <stationdata weatherdate="2015-01-03" location="Charlottetown"> 
     <maxtemp>-11.8°C</maxtemp> 
     <mintemp>-16.1°C</mintemp> 
     <totalrain>0 mm</totalrain> 
     <totalsnow>0cm</totalsnow> 
    </stationdata> 
    <stationdata weatherdate="2015-01-01" location="Ottawa"> 
     <maxtemp>-3°C</maxtemp> 
     <mintemp>-8.1°C</mintemp> 
     <totalrain>0 mm</totalrain> 
     <totalsnow>0.2cm</totalsnow> 
    </stationdata> 
    <stationdata weatherdate="2015-01-02" location="Ottawa"> 
     <maxtemp>-3.8°C</maxtemp> 
     <mintemp>-15.8°C</mintemp> 
     <totalrain>0 mm</totalrain> 
     <totalsnow>0cm</totalsnow> 
    </stationdata> 
    <stationdata weatherdate="2015-01-03" location="Ottawa"> 
     <maxtemp>-9.6°C</maxtemp> 
     <mintemp>-15.5°C</mintemp> 
     <totalrain>0 mm</totalrain> 
     <totalsnow>18cm</totalsnow> 
    </stationdata> 
</weatherdata> 

XSLT 1,0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:key name="locations" match="stationdata" use="@location"/> 

    <xsl:template match="/weatherdata"> 
    <html> 
     <head> 
     <title>weather data</title> 
     </head> 
     <body> 
     <xsl:for-each select="stationdata[count(.|key('locations',@location)[1])=1]"> 
      <h1><xsl:value-of select="@location"/>-Temperature Summary (<xsl:value-of select="substring-before(@weatherdate,'-')"/>)</h1> 
      <table border="1"> 
      <tr> 
       <th>Data</th> 
       <th>Maximum Temperature(°C)</th> 
       <th>Minimum Temperature(°C)</th> 
       <th>Total Rain(mm)</th> 
       <th>Total Snow(cm)</th> 
      </tr> 
      <xsl:apply-templates select="key('locations',@location)"/> 
      </table> 
     </xsl:for-each> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="stationdata"> 
    <tr> 
     <td><xsl:value-of select="@weatherdate"/></td> 
     <td><xsl:value-of select="maxtemp"/></td> 
     <td><xsl:value-of select="mintemp"/></td> 
     <td><xsl:value-of select="totalrain"/></td> 
     <td><xsl:value-of select="totalsnow"/></td> 
    </tr> 
    </xsl:template> 

</xsl:stylesheet> 

HTML sortie

<html> 
 
    <head> 
 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
 
    
 
     <title>weather data</title> 
 
    </head> 
 
    <body> 
 
     <h1>Calgary-Temperature Summary (2015)</h1> 
 
     <table border="1"> 
 
     <tr> 
 
      <th>Data</th> 
 
      <th>Maximum Temperature(&deg;C)</th> 
 
      <th>Minimum Temperature(&deg;C)</th> 
 
      <th>Total Rain(mm)</th> 
 
      <th>Total Snow(cm)</th> 
 
     </tr> 
 
     <tr> 
 
      <td>2015-1-1</td> 
 
      <td>1.1&deg;C</td> 
 
      <td>-6.1&deg;C</td> 
 
      <td>0 mm</td> 
 
      <td>0cm</td> 
 
     </tr> 
 
     <tr> 
 
      <td>2015-1-2</td> 
 
      <td>-3.4&deg;C</td> 
 
      <td>-18.2&deg;C</td> 
 
      <td>0 mm</td> 
 
      <td>5cm</td> 
 
     </tr> 
 
     <tr> 
 
      <td>2015-1-3</td> 
 
      <td>-18.1&deg;C</td> 
 
      <td>-21.1&deg;C</td> 
 
      <td>0 mm</td> 
 
      <td>1.6cm</td> 
 
     </tr> 
 
     </table> 
 
     <h1>Charlottetown-Temperature Summary (2015)</h1> 
 
     <table border="1"> 
 
     <tr> 
 
      <th>Data</th> 
 
      <th>Maximum Temperature(&deg;C)</th> 
 
      <th>Minimum Temperature(&deg;C)</th> 
 
      <th>Total Rain(mm)</th> 
 
      <th>Total Snow(cm)</th> 
 
     </tr> 
 
     <tr> 
 
      <td>2015-01-01</td> 
 
      <td>-3.5&deg;C</td> 
 
      <td>-15&deg;C</td> 
 
      <td>0 mm</td> 
 
      <td>0.4cm</td> 
 
     </tr> 
 
     <tr> 
 
      <td>2015-01-02</td> 
 
      <td>-1&deg;C</td> 
 
      <td>-13.2&deg;C</td> 
 
      <td>0 mm</td> 
 
      <td>0.6cm</td> 
 
     </tr> 
 
     <tr> 
 
      <td>2015-01-03</td> 
 
      <td>-11.8&deg;C</td> 
 
      <td>-16.1&deg;C</td> 
 
      <td>0 mm</td> 
 
      <td>0cm</td> 
 
     </tr> 
 
     </table> 
 
     <h1>Ottawa-Temperature Summary (2015)</h1> 
 
     <table border="1"> 
 
     <tr> 
 
      <th>Data</th> 
 
      <th>Maximum Temperature(&deg;C)</th> 
 
      <th>Minimum Temperature(&deg;C)</th> 
 
      <th>Total Rain(mm)</th> 
 
      <th>Total Snow(cm)</th> 
 
     </tr> 
 
     <tr> 
 
      <td>2015-01-01</td> 
 
      <td>-3&deg;C</td> 
 
      <td>-8.1&deg;C</td> 
 
      <td>0 mm</td> 
 
      <td>0.2cm</td> 
 
     </tr> 
 
     <tr> 
 
      <td>2015-01-02</td> 
 
      <td>-3.8&deg;C</td> 
 
      <td>-15.8&deg;C</td> 
 
      <td>0 mm</td> 
 
      <td>0cm</td> 
 
     </tr> 
 
     <tr> 
 
      <td>2015-01-03</td> 
 
      <td>-9.6&deg;C</td> 
 
      <td>-15.5&deg;C</td> 
 
      <td>0 mm</td> 
 
      <td>18cm</td> 
 
     </tr> 
 
     </table> 
 
    </body> 
 
</html>


XSLT 2.0 exemple mentionné dans les commentaires. Comme vous pouvez le voir, il n'y a pas beaucoup de différence pour cet exemple de base. Nous utilisons simplement xsl:for-each-group au lieu d'une clé.

XSLT 2,0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/weatherdata"> 
    <html> 
     <head> 
     <title>weather data</title> 
     </head> 
     <body> 
     <xsl:for-each-group select="stationdata" group-by="@location"> 
      <h1><xsl:value-of select="current-grouping-key()"/>-Temperature Summary (<xsl:value-of select="substring-before(@weatherdate,'-')"/>)</h1> 
      <table border="1"> 
      <tr> 
       <th>Data</th> 
       <th>Maximum Temperature(°C)</th> 
       <th>Minimum Temperature(°C)</th> 
       <th>Total Rain(mm)</th> 
       <th>Total Snow(cm)</th> 
      </tr> 
      <xsl:apply-templates select="current-group()"/> 
      </table> 
     </xsl:for-each-group> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="stationdata"> 
    <tr> 
     <td><xsl:value-of select="@weatherdate"/></td> 
     <td><xsl:value-of select="maxtemp"/></td> 
     <td><xsl:value-of select="mintemp"/></td> 
     <td><xsl:value-of select="totalrain"/></td> 
     <td><xsl:value-of select="totalsnow"/></td> 
    </tr> 
    </xsl:template> 

</xsl:stylesheet> 
+0

Merci beaucoup, cela fonctionne parfaitement. Je suis nouveau à XSL, est-ce que ce serait beaucoup plus facile sur XSL 2.0? @Daniel Haley –

+0

@Z.Zhe - Pour ce petit exemple, ce n'est pas beaucoup plus facile en 2.0. Cependant, si votre regroupement devient plus compliqué ou s'il y a d'autres domaines qui doivent être transformés davantage (comme les dates de mise en forme, etc.), cela sera beaucoup plus facile dans la version 2.0. Je vais mettre à jour ma réponse avec un exemple 2.0. Aussi, si cette réponse est suffisante, veuillez l'accepter en cliquant sur la coche (✅) à côté de celle-ci. Merci! –

+0

Merci, @ Daniel Haley, je vais essayer d'étudier le lien que vous fournissez. vraiment apprécié –