-3

我有一个复杂的 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>在 for each 里面做,但它不起作用。

4

2 回答 2

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 中找到的各种可能的详细信息级别 - 每个模板生成一个包含更多或更少单元格的表格行。

于 2012-05-15T20:51:26.913 回答
-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> 
于 2012-05-16T15:46:01.870 回答