复杂XML的XSL转换

时间:2012-05-15 20:12:52

标签: xml xslt

我有一个复杂的XML,我正在尝试编写一个XSL转换来将其转换为HTML。有人可以帮帮我吗?

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

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

我不知道如何获得其他信息:我想把它全部放在一张桌子里。我尝试在每个内部执行<xsl:for-each>,但它不起作用。

2 个答案:

答案 0 :(得分:0)

试试这个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>

应用于您的示例产生:

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

我们的想法是拥有与XML中各种可能的详细信息级别相匹配的不同模板 - 每个模板生成一个包含更多或更少单元格的表格行。

答案 1 :(得分:-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>