How to split XML file into many XML files using XSLT

后端 未结 2 1386
春和景丽
春和景丽 2020-12-28 09:59

I would like to know how to write XSLT to split an XML file into multiple XML files according to these requirements:

  • file1.xml - The lakes who type= Natyral
相关标签:
2条回答
  • 2020-12-28 10:19

    Use XSLT 2.0, like this stylesheet:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="/">
            <xsl:for-each-group select="Lakes/Lake" group-by="Type">
                <xsl:result-document href="file{position()}.xml">
                    <Lakes>
                        <xsl:copy-of select="current-group()"/>
                    </Lakes>
                </xsl:result-document>
            </xsl:for-each-group>
        </xsl:template>
    </xsl:stylesheet>
    

    Note: xsl:result-document instruction.

    0 讨论(0)
  • 2020-12-28 10:19

    With standard XSL it is not possible to have more than one output xml (i.e. resulting tree).
    However, using Xalan redirect extension, you can.

    Have a look at the example on the page in the link. I tested the following with Xalan Java 2.7.1

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:redirect="http://xml.apache.org/xalan/redirect" extension-element-prefixes="redirect">
    
        <xsl:output method="xml" indent="yes" />
    
        <xsl:template match="/">
            <xsl:apply-templates />
        </xsl:template>
    
        <xsl:template match="/Lakes/Lake[Type='Natyral']">
            <redirect:write file="/home/me/file1.xml">
                <NatyralLakes>
                    <xsl:copy-of select="." />
                </NatyralLakes>
            </redirect:write>
        </xsl:template>
    
        <xsl:template match="/Lakes/Lake[Type='Artificial']">
            <redirect:write file="/home/me/file1.xml">
                <ArtificialLakes>
                    <xsl:copy-of select="." />
                </ArtificialLakes>
            </redirect:write>
        </xsl:template>
    
        <xsl:template match="/Lakes/Lake[Type='Glacial']">
            <redirect:write file="/home/me/file3.xml">
                <GlacialLakes>
                    <xsl:copy-of select="." />
                </GlacialLakes>
            </redirect:write>
        </xsl:template>
    
    
    </xsl:stylesheet>
    
    0 讨论(0)
提交回复
热议问题