2012-05-15 5 views
-3

J'ai un XML compliqué et j'essaye d'écrire une transformation XSL pour le convertir en HTML. Quelqu'un pourrait-il m'aider s'il vous plaît?Transformation XSL de XML compliqué

Voici le XML

<?xml version="1.0" encoding="iso-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="Mbooks.xsl"?> 
<project> 
    <books> 
    <bookName>Eclpise</bookName> 
    <bookCount>3</bookCount> 
    <Data> 
     <NEW> 
     <bookNumber>book3687110</bookNumber> 
     <ISBN>927fd6ca660e5a9</ISBN> 
     <Isbninfo> 
      <IsbninfoDetails> 
      <IsbninfoName>new book</IsbninfoName> 
      <IsbninfoVersion>version 1</IsbninfoVersion> 
      </IsbninfoDetails> 
      <IsbninfoDetails> 
      <IsbninfoName> new book 1</IsbninfoName> 
      <IsbninfoVersion>version 2</IsbninfoVersion> 
      </IsbninfoDetails> 
     </Isbninfo> 
     </NEW> 
     <NEW> 
     <bookNumber>book3674796</bookNumber> 
     <ISBN>6fa276825144</ISBN> 
     <Isbninfo> 
      <IsbninfoDetails> 
      <IsbninfoName>new book 3</IsbninfoName> 
      <IsbninfoVersion>version 3</IsbninfoVersion> 
      </IsbninfoDetails> 
      <IsbninfoDetails> 
      <IsbninfoName>new book 4</IsbninfoName> 
      <IsbninfoVersion>version 4</IsbninfoVersion> 
      </IsbninfoDetails> 
     </Isbninfo> 
     </NEW> 
    </Data> 
    </books> 
    <books> 
    <bookName>ORACLE</bookName> 
    <bookCount>0</bookCount> 
    <Data> 
    </Data> 
    </books> 
    <books> 
    <bookName>MUSIC</bookName> 
    <bookCount>0</bookCount> 
    <Data> 

    </Data> 
    </books> 
</project> 

Voici le XSLT.

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

    <xsl:template match="/"> 
    <html> 
     <body> 
     <h2>BOOK_INFORMATION </h2> 
     <table style="display: inline-block; border: 1px solid; float: left; "> 
      <tr bgcolor="#FFA500"> 
      <th>book Name</th> 
      <th>book_Count</th> 
      <th>book_Number</th> 
      <th>ISBN</th> 
      <th>Isbninfo_Name</th> 
      <th>Isbninfo_Version</th> 
      </tr> 

      <xsl:for-each select="project/books"> 
      <tr> 
       <td> 
       <xsl:value-of select="bookName"/> 
       </td> 
       <td> 
       <xsl:value-of select="bookCount"/> 
       </td> 
      </tr> 
      </xsl:for-each> 

     </table> 

     </body> 
    </html> 
    </xsl:template> 

</xsl:stylesheet> 

Je ne sais pas comment pourrais-je obtenir les autres informations: Je veux tout dans une table. J'ai essayé de faire <xsl:for-each> dans le pour chaque mais cela ne fonctionne pas.

Répondre

0

Essayez cette XSLT:

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

    <xsl:template match="text()"> 
    </xsl:template> 

    <xsl:template match="IsbninfoDetails"> 
    <tr> 
     <td> 
     <xsl:value-of select="../../../../bookName"/> 
     </td> 
     <td> 
     <xsl:value-of select="../../../../bookCount"/> 
     </td> 
     <td> 
     <xsl:value-of select="../../bookNumber"/> 
     </td> 
     <td> 
     <xsl:value-of select="../../ISBN"/> 
     </td> 
     <td> 
     <xsl:value-of select="IsbninfoName"/> 
     </td> 
     <td> 
     <xsl:value-of select="IsbninfoVersion"/> 
     </td> 
    </tr> 
    </xsl:template> 

    <xsl:template match="books[not(Data/NEW)]"> 
    <tr> 
     <td> 
     <xsl:value-of select="bookName"/> 
     </td> 
     <td> 
     <xsl:value-of select="bookCount"/> 
     </td> 
    </tr> 
    </xsl:template> 

    <xsl:template match="NEW[not(Isbninfo/IsbninfoDetails)]"> 
    <tr> 
     <td> 
     <xsl:value-of select="../../bookName"/> 
     </td> 
     <td> 
     <xsl:value-of select="../../bookCount"/> 
     </td> 
     <td> 
     <xsl:value-of select="bookNumber"/> 
     </td> 
     <td> 
     <xsl:value-of select="ISBN"/> 
     </td> 
    </tr> 
    </xsl:template> 

    <xsl:template match="/"> 
    <html> 
     <body> 
     <h2>BOOK_INFORMATION </h2> 
     <table style="display: inline-block; border: 1px solid; float: left; "> 
      <tr bgcolor="#FFA500"> 
      <th>book Name</th> 
      <th>book_Count</th> 
      <th>book_Number</th> 
      <th>ISBN</th> 
      <th>Isbninfo_Name</th> 
      <th>Isbninfo_Version</th> 
      </tr> 
      <xsl:apply-templates/> 
     </table> 

     </body> 
    </html> 
    </xsl:template> 

</xsl:stylesheet> 

appliquée à votre exemple produit ceci:

<html> 
    <body> 
    <h2>BOOK_INFORMATION </h2> 
    <table style="display: inline-block; border: 1px solid; float: left; "> 
     <tr bgcolor="#FFA500"> 
     <th>book Name</th> 
     <th>book_Count</th> 
     <th>book_Number</th> 
     <th>ISBN</th> 
     <th>Isbninfo_Name</th> 
     <th>Isbninfo_Version</th> 
     </tr> 
     <tr> 
     <td>Eclpise</td> 
     <td>3</td> 
     <td>book3687110</td> 
     <td>927fd6ca660e5a9</td> 
     <td>new book</td> 
     <td>version 1</td> 
     </tr> 
     <tr> 
     <td>Eclpise</td> 
     <td>3</td> 
     <td>book3687110</td> 
     <td>927fd6ca660e5a9</td> 
     <td> new book 1</td> 
     <td>version 2</td> 
     </tr> 
     <tr> 
     <td>Eclpise</td> 
     <td>3</td> 
     <td>book3674796</td> 
     <td>6fa276825144</td> 
     <td>new book 3</td> 
     <td>version 3</td> 
     </tr> 
     <tr> 
     <td>Eclpise</td> 
     <td>3</td> 
     <td>book3674796</td> 
     <td>6fa276825144</td> 
     <td>new book 4</td> 
     <td>version 4</td> 
     </tr> 
     <tr> 
     <td>ORACLE</td> 
     <td>0</td> 
     </tr> 
     <tr> 
     <td>MUSIC</td> 
     <td>0</td> 
     </tr> 
    </table> 
    </body> 
</html> 

L'idée est d'avoir différents modèles qui correspondent à différents niveaux possibles de détails trouvés dans le fichier XML - chaque modèle générant une ligne de table avec plus ou moins de cellules.

+0

hey merci pour votre réponse, mais est-il possible de ne pas dupliquer le nom du livre, book_count, BOOK_NUMBER et le numéro ISBN et nous pouvons ajouter quelque chose comme la durée de ligne ou de façon et la faire ressembler à une bonne table? Merci pour votre aide. – Maxyie

+0

Oui, c'est certainement possible, mais vous devriez spécifier un peu mieux ce que vous voulez exactement - et ce que vous avez essayé. – MiMo

+0

Hey désolé pour la confusion que j'ai cause. Je veux une bonne table dans laquelle les champs nécessaires ne sont pas dupliqués. J'essaie d'utiliser une boucle foreach à l'intérieur de la boucle foreach mais je suppose que cela ne fonctionne pas comme java ou perl fonctionne. J'ai "ajouté un exemple html" dans un nouveau fil comme je suis incapable de le dd ici – Maxyie

-1
<html> 
<body>  
<h2>BOOK_INFORMATION </h2> 
    <table style="display: inline-block; border: 1px solid;  float: left; ">  
<tr bgcolor="#FFA500">   
<th>book Name</th>  
<th>book_Count</th>  
<th>book_Number</th>  
<th>ISBN</th>   
<th>Isbninfo_Name</th>  
<th>Isbninfo_Version</th> 
</tr>  
<tr>  
<td rowspan ="4">Eclpise</td>  
<td rowspan ="4">3</td>  
<td rowspan = "2">book3687110</td> 
    <td rowspan = "2">927fd6ca660e5a9</td>   
<td>new book</td> 
<td>version 1</td> 
</tr> 
<tr> 
<td>new book1</td> 
<td>version 2</td> 
</tr> 
<tr> 
<td rowspan ="2">book3674796</td>  
<td rowspan ="2">6fa276825144</td> 
<td>new book2</td> 
<td>version 3</td> 
</tr> 
<tr> 
<td>new book3</td> 
<td>version 4</td> 
</tr>  
<tr>  
<td>ORACLE</td> 
<td>0</td> 
</tr>  
<tr>  
<td>MUSIC</td> 
<td>0</td>  
</tr> 
</table> 
</body> 
</html> 
Questions connexes