使用 XSLT 将一个 XML 文件部分拆分为多个 html 文件

Split one XML file section to multiple html File with XSLT

我有这个 XML 代码

.xml

<head>heading ONE</head>
<section>Lorem ips</section>
<section>Lorem ips</section>    
<head>heading TWO</head>    
<head>heading THREE</head>
<section>Lorem ips</section>
<section>Lorem ips</section>    
<head>heading FOUR</head>
<section>Lorem ips</section>

如何编写 XSLT 以将其分解为多个文件,如下所示

.html

<head>heading ONE</head>
<section>Lorem ips</section>
<section>Lorem ips</section>

.html

<head>heading TWO</head>

.html

<head>heading THREE</head>
<section>Lorem ips</section>
<section>Lorem ips</section>

.html

<head>heading FOUR</head>
<section>Lorem ips</section>

我想你输入的 XML 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <head>heading ONE</head>
    <section>Lorem ips</section>
    <section>Lorem ips</section>
    <head>heading TWO</head>
    <head>heading THREE</head>
    <section>Lorem ips</section>
    <section>Lorem ips</section>
    <head>heading FOUR</head>
    <section>Lorem ips</section>
</root>

那么 XSLT 就是:

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

    <xsl:template match="root">
        <xsl:for-each-group select="*" group-starting-with="head">
            <xsl:result-document href="{concat(name(),'_',position(),'.html')}" indent="yes">
                <root>
                    <xsl:copy-of select="current-group()"/>
                </root>
            </xsl:result-document>
        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

然后您可以根据需要为每个头获得单独的文件。