xslt创建一个新的xml文件

时间:2013-12-10 19:54:16

标签: xml xslt xslt-1.0

我有这个xslt代码

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <html>
    <head>
    <title></title>
    <link rel="stylesheet" href="css/xsl_style.css" type="text/css"/>
    </head>
    <body>
    <div class="tableStyle" >
      <table id="products_table">
      <tbody>
        <tr bgcolor="#B5E4EA">
          <td>A/A</td>
          <td>Product Name</td>
          <td>Price</td>
          <td>Corpration</td>
          <td>Category</td>
        </tr>
        <xsl:variable name="allProducts" select="auction_products/product" />
        <xsl:for-each select="$allProducts">
          <xsl:sort select="category"/>
          <xsl:variable name="pos" select="position()" />
          <tr>
            <td><xsl:value-of select="$allProducts[$pos]/count_products"/></td>
            <td><a href="offer.php"><xsl:value-of select="product_name"/></a></td>
            <td><a href="offer.php"><xsl:value-of select="price"/></a></td>
            <td><a href="offer.php"><xsl:value-of select="corporation"/></a></td>
            <td><a href="offer.php"><xsl:value-of select="category"/></a></td>
          </tr>
        </xsl:for-each>
        </tbody>
      </table>
    </div>
    </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

我想通过xslt用排序的数据创建一个新的xml文件。在xslt版本1中不可能这样做吗?我尝试了xsl:document,但我没有成功。

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望从同一个XSLT(1.0)样式表中输出多个文档。

正如此链接:Splitting XML into multiple files with XSLT建议,使用XSLT 1.0中提供的方法无法做到这一点。您可以使用EXSLT(http://www.exslt.org/exsl/elements/document/index.html)扩展XSLT 1.0设置,也可以使用XSLT 2.0。

如果您决定使用XSLT 2.0,您显然需要将版本信息更改为:

<xsl:stylesheet version="2.0">

然后,将result-document合并到样式表中。我建议你在网上寻找适当的信息,例如在这里:http://saxonica.com/documentation9.4-demo/html/xsl-elements/result-document.html

请注意,您还需要一台能够XSLT 2.0的处理器,例如Saxon。当你思考这个时,还要考虑是否有必要让一个样式表输出几个文件以及为什么。

答案 1 :(得分:0)

我可以想到使用XSLT 1.0的两种方法 - 一种是将排序后的数据简单地嵌入到输出html中,从中提取所需数据应该是一个相对简单的事情。

另一种方法是使用xsl:message输出您想要的数据作为消息,并实现记录该输出的机制。

相关问题