使用xslt将xml拆分为多个html文件

时间:2013-05-31 04:00:21

标签: xml xslt

我有一个非常大的xml文档,看起来像下面的摘录。 我能够使用Web上的一些示例将文件拆分为多个html文件。

我对结果文件的唯一问题是它们应该包含<h1>标记以及下一个<h1>之前的每个元素,然后使用以下元素获取下一个<h1>,依此类推等等。

基本上我需要的是能够为<h1 id=h1>生成一个文件以及以下元素(<p><ol><pre>) 目前,在创建文件时,下一个h1标记之前的以下元素不包含在创建的文档中。我不知道如何调整xslt来做到这一点。 原始xml

<?xml version="1.0" encoding="UTF-8"?>

<paragraphs>

<h1 id= "h1">Header One</h1>

<p>The quick brown fox jumps over the lazy dog. </p>

<p>The quick brown fox jumps over the lazy dog.  </p>

<p>The quick brown fox jumps over the lazy dog.</p>
<ol>
    <li>
        List 1
        </li>
        <li>
            List 2
            </li>

</ol>

            <h1 id= "h2">Header  Two</h1>

            <p>The quick brown fox jumps over the lazy dog. </p>

            <p>The quick brown fox jumps over the lazy dog.</p>
            <ul>
                <li>
                    List 3
                    </li
                >
                    <li>
                        List 4
                        </li>

            </ul>

            <p>The quick brown fox jumps over the lazy dog.</p>

            <h1 id= "h3">Header  Three</h1>

            <pre>my example one</pre>

            <p>The quick brown fox jumps over the lazy dog.</p>

            <pre> Another example</pre>

</paragraphs>  

xslt遵循位于此处的IBM开发人员网站的示例:http://www.ibm.com/developerworks/library/x-tipmultxsl/

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0">

    <xsl:output method="text"/>
    <xsl:output method="html" indent="yes" name="html"/>

    <xsl:template match="/">
        <xsl:for-each select="//h1">
            <xsl:variable name="filename"
                select="concat('output/',@id,'.html')" />
            <xsl:value-of select="$filename" />  <!-- Creating  -->
            <xsl:result-document href="{$filename}" format="html">
                <html><body>
                    <xsl:value-of select="text()"/>
                </body></html>
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

生成的文件如下所示:

<html>
    <body>Header One</body>
    </html>

<html>
    <body>Header Two</body>
    </html>

<html>
    <body>Header Three</body>
    </html>

非常感谢。

1 个答案:

答案 0 :(得分:1)

我认为你正在寻找类似的东西......

XML输入

<paragraphs>
    <h1 id="h1">Header One</h1>
    <p>The quick brown fox jumps over the lazy dog. </p>
    <p>The quick brown fox jumps over the lazy dog. </p>
    <p>The quick brown fox jumps over the lazy dog.</p>
    <ol>
        <li> List 1 </li>
        <li> List 2 </li>
    </ol>
    <h1 id="h2">Header Two</h1>
    <p>The quick brown fox jumps over the lazy dog. </p>
    <p>The quick brown fox jumps over the lazy dog.</p>
    <ul>
        <li> List 3 </li>
        <li> List 4 </li>
    </ul>
    <p>The quick brown fox jumps over the lazy dog.</p>
    <h1 id="h3">Header Three</h1>
    <pre>my example one</pre>
    <p>The quick brown fox jumps over the lazy dog.</p>
    <pre> Another example</pre>
</paragraphs>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
        <xsl:for-each-group select="*" group-starting-with="h1">
            <xsl:result-document href="output/{@id}.html">
                <html>
                    <body>
                        <xsl:copy-of select="current-group()"/>
                    </body>
                </html>
            </xsl:result-document>
        </xsl:for-each-group>
    </xsl:template>

</xsl:stylesheet>

输出文件

<强> h1.html

<html>
   <body>
      <h1 id="h1">Header One</h1>
      <p>The quick brown fox jumps over the lazy dog. </p>
      <p>The quick brown fox jumps over the lazy dog. </p>
      <p>The quick brown fox jumps over the lazy dog.</p>
      <ol>
         <li> List 1 </li>
         <li> List 2 </li>
      </ol>
   </body>
</html>

<强> h2.html

<html>
   <body>
      <h1 id="h2">Header Two</h1>
      <p>The quick brown fox jumps over the lazy dog. </p>
      <p>The quick brown fox jumps over the lazy dog.</p>
      <ul>
         <li> List 3 </li>
         <li> List 4 </li>
      </ul>
      <p>The quick brown fox jumps over the lazy dog.</p>
   </body>
</html>

<强> h3.html

<html>
   <body>
      <h1 id="h3">Header Three</h1>
      <pre>my example one</pre>
      <p>The quick brown fox jumps over the lazy dog.</p>
      <pre> Another example</pre>
   </body>
</html>

此外,如果您需要执行其他转换任务,则可以添加标识转换并使用xsl:apply-templates代替xsl:copy-of。然后,您可以根据需要添加其他模板。

例如,如果您想将所有ol元素更改为ul,您可以这样做:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/*">
        <xsl:for-each-group select="*" group-starting-with="h1">
            <xsl:result-document href="output/{@id}.html">
                <html>
                    <body>
                        <xsl:apply-templates select="current-group()"/>
                    </body>
                </html>
            </xsl:result-document>
        </xsl:for-each-group>
    </xsl:template>

    <xsl:template match="ol">
        <ul>
            <xsl:apply-templates select="@*|node()"/>
        </ul>
    </xsl:template>

</xsl:stylesheet>