[英]Split XML into multiple HTML files using XSLT
I'm looking for a way to split an XML file into multiple HTML files which in the end, should contain a table.我正在寻找一种将 XML 文件拆分为多个 HTML 文件的方法,这些文件最终应该包含一个表格。
An example xml:一个示例 xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<table>
<row>
<name>user1</name>
<email>email1</email>
</row>
<row>
<name>user2</name>
<email>email2</email>
</row>
<row>
<name>user3</name>
<email>email3</email>
</row>
<row>
<name>user4</name>
<email>email4</email>
</row>
<row>
<name>user5</name>
<email>email5</email>
</row>
...
</table>
Now I want to split this xml file into different HTML files which contain a specific range from the XML file.现在我想将此 xml 文件拆分为不同的 HTML 文件,其中包含来自 XML 文件的特定范围。 Each HTML file should contain 3 rows from the XML for example.
例如,每个 HTML 文件应包含来自 XML 的 3 行。
table1.html表1.html
<table>
<tr>
<td>user1</td>
<td>email1</td>
</tr>
<tr>
<td>user2</td>
<td>email2</td>
</tr>
<tr>
<td>user3</td>
<td>email3</td>
</tr>
</table>
table2.html table2.html
<table>
<tr>
<td>user4</td>
<td>email4</td>
</tr>
<tr>
<td>user5</td>
<td>email5</td>
</tr>
<tr>
<td>user6</td>
<td>email6</td>
</tr>
</table>
Anyone an idea how the XSLT file needs to be formatted?任何人都知道需要如何格式化 XSLT 文件? I'm using ruby-xslt to load the XML and XSLT file and convert the whole thing to HTML.
我正在使用ruby-xslt加载 XML 和 XSLT 文件并将整个文件转换为 HTML。
If you apply this XSLT to your input XML it will split the XML into tables containing 3 records:如果您将此 XSLT 应用到您的输入 XML,它会将 XML 拆分为包含 3 个记录的表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" method="xml"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- split file up into row elements -->
<xsl:template match="row[position() mod 3 = 1]">
<table>
<xsl:copy-of select=".|following-sibling::row[not(position() > 2)]"/>
</table>
</xsl:template>
<xsl:template match="row"/>
</xsl:stylesheet>
OUTPUT XML:输出 XML:
<?xml version="1.0" encoding="UTF-8"?>
<table>
<table>
<row>
<name>user1</name>
<email>email1</email>
</row>
<row>
<name>user2</name>
<email>email2</email>
</row>
<row>
<name>user3</name>
<email>email3</email>
</row>
</table>
<table>
<row>
<name>user4</name>
<email>email4</email>
</row>
<row>
<name>user5</name>
<email>email5</email>
</row>
</table>
</table>
If you run the OUTPUT XML through this XSLT you will get the wanted result:如果您通过此 XSLT 运行 OUTPUT XML,您将获得想要的结果:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="row">
<tr>
<xsl:apply-templates select="node()|@*"/>
</tr>
</xsl:template>
<xsl:template match="email | name">
<td>
<xsl:apply-templates select="node()|@*"/>
</td>
</xsl:template>
</xsl:stylesheet>
final OUTPUT XML:最终输出 XML:
<?xml version="1.0" encoding="UTF-8"?><table>
<table>
<tr>
<td>user1</td>
<td>email1</td>
</tr>
<tr>
<td>user2</td>
<td>email2</td>
</tr>
<tr>
<td>user3</td>
<td>email3</td>
</tr>
</table>
<table>
<tr>
<td>user4</td>
<td>email4</td>
</tr>
<tr>
<td>user5</td>
<td>email5</td>
</tr>
</table>
</table>
In my opinion this should work with XSLT 1.0 as long as you have the opportunity to run 2 stylesheets on your input XML.在我看来,只要您有机会在输入的 XML 上运行 2 个样式表,这应该适用于 XSLT 1.0。
To create multiple HTML files in a single XSLT transformation you will need the xsl:result-document instruction, which is only available in XSLT 2.0.要在单个 XSLT 转换中创建多个 HTML 文件,您将需要 xsl:result-document 指令,该指令仅在 XSLT 2.0 中可用。 You don't get that with ruby-xslt unfortunately.
不幸的是,您无法使用 ruby-xslt。
An XSLT 1.0 transformation can create only a single output . XSLT 1.0 转换只能创建一个输出。
You may still have a single result that contains all the generated tables.您可能仍然有一个包含所有生成表的结果。 Then you need to process this result outside XSLT (or perform N transformations on it) in order to produce the N separate outputs needed.
然后您需要在 XSLT 之外处理此结果(或对其执行 N 次转换),以生成所需的 N 个单独输出。
I strongly recommend that you use XSLT 2.0 for this task -- this can easily be accomplished using the new xsl:result-document
instruction.我强烈建议您使用 XSLT 2.0 来完成这项任务——这可以使用新的
xsl:result-document
指令轻松完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.