簡體   English   中英

使用xslt將xml轉換為html

[英]xml transform to html using xslt

我正在嘗試使用鍵函數將xml轉換為html,如下所示:

但是我不知道如何制作兩張桌子,因為我的if函數在其他下面寫了一本書。 XML看起來像:

<library>
<books>
    <book aut="JKR">
        <title>Harry Potter and the Sorcerer's Stone</title>
        <quantity>5</quantity>
    </book>
</books>
<books>
    <book aut="JKR">
        <title>example</title>
        <quantity>3</quantity>
    </book>
    <book aut="AC">
        <title>example</title>
        <quantity>2</quantity>
</books>
<authors>
    <author id="JKR">J.K.Rowling</author>
    <author id="AC"> Agatha Christie</author>
<authors>

片段是xslt代碼:

  <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" />
    <xsl:output method="html"  encoding="iso-8859-2" />
    <xsl:key name="idauthor" match="author" use="@id"/>
    <xsl:template match="/">
        <html>
            <head>
                <meta content="text/html" />
                       </head>
     <body>

      <table border="1">
            <xsl:apply-templates select="//book"/>
      </table>

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

<xsl:template match="book">


   <xsl:if test="key('idauthor',@author)">

      <tr>   author:
            <td> <xsl:value-of select="title" /></td>
            <td> <xsl:value-of select="quantity" /></td>
      </tr>
     </xsl:if >
  </xsl:template>   
</xsl:stylesheet>

如果您的輸入具有authors索引,並且您希望按作者報告,那么您應該從那開始。

<xsl:apply-templates select="authors/author"/>

然后,您希望將密鑰應用於書籍,而不是作者:

<xsl:key name="books-by" match="book" use="@aut"/>

最后,對於每位作者,您希望按作者的ID選擇書籍:

<table border="1">
    <xsl:apply-templates select="key('books-by', @id)" />
</table>

以下轉換

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" />
    <xsl:output method="html"    encoding="iso-8859-2" />
    <xsl:key name="books-by" match="book" use="@aut"/>

    <xsl:template match="/library">
        <html>
            <head>
                <meta content="text/html" />
            </head>
            <body>
                <xsl:apply-templates select="authors/author"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="author">
        <h2>
            <xsl:value-of select="."/>
        </h2>
        <table border="1">
            <xsl:apply-templates select="key('books-by', @id)" />
        </table>

    </xsl:template>

    <xsl:template match="book">
        <tr>
            <td> <xsl:value-of select="position()" /></td>
            <td> <xsl:value-of select="title" /></td>
            <td> <xsl:value-of select="quantity" /></td>
        </tr>
    </xsl:template>     
</xsl:stylesheet>

應用於您的(更正的)輸入,

<library>
    <books>
        <book aut="JKR">
            <title>Harry Potter and the Sorcerer's Stone</title>
            <quantity>5</quantity>
        </book>
    </books>
    <books>
        <book aut="JKR">
            <title>example</title>
            <quantity>3</quantity>
        </book>
        <book aut="AC">
            <title>example</title>
            <quantity>2</quantity>
        </book>
    </books>
    <authors>
        <author id="JKR">J.K.Rowling</author>
        <author id="AC"> Agatha Christie</author>
    </authors>
</library>

生成以下HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <META http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta content="text/html">
    </head>
    <body>
        <h2>J.K.Rowling</h2>
        <table border="1">
            <tr>
                <td>1</td>
                <td>Harry Potter and the Sorcerer's Stone</td>
                <td>5</td>
            </tr>
            <tr>
                <td>2</td>
                <td>example</td>
                <td>3</td>
            </tr>
        </table>
        <h2> Agatha Christie</h2>
        <table border="1">
            <tr>
                <td>1</td>
                <td>example</td>
                <td>2</td>
            </tr>
        </table>
    </body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM