直接上代码
1.xml
<?xml version="1.0" encoding="UTF-8"?> <JobRecords> <JobRecord> <Brand>Corporate1</Brand> <WorkTypes> <WorkTypeRecord> <Title>Permanent1</Title> </WorkTypeRecord> </WorkTypes> </JobRecord> </JobRecords>
2.xml
<?xml version="1.0" encoding="UTF-8"?> <JobRecords> <JobRecord> <Brand>Corporate2</Brand> <WorkTypes> <WorkTypeRecord> <Title>Permanent2</Title> </WorkTypeRecord> </WorkTypes> </JobRecord> </JobRecords>
merge.xslt
<?xml version="1.0" encoding="UTF-8" ?> <!-- New XSLT document created with EditiX XML Editor (http://www.editix.com) at Thu Nov 12 14:29:49 CST 2020 --> <xsl:stylesheet version="2.0" exclude-result-prefixes="xs xdt err fn" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:err="http://www.w3.org/2005/xqt-errors"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <xsl:copy> <xsl:apply-templates mode="rootcopy"></xsl:apply-templates> </xsl:copy> </xsl:template> <xsl:template match="node()" mode="rootcopy"> <xsl:copy> <xsl:variable name="folderURI" select="resolve-uri('.',base-uri())"/> <xsl:for-each select="collection(concat($folderURI, '?select=*.xml;recurse=yes'))/*/node()"> <xsl:apply-templates mode="copy" select="."/> </xsl:for-each> </xsl:copy> </xsl:template> <xsl:template match="node()|@*" mode="copy"> <xsl:copy> <xsl:apply-templates mode="copy" select="@*"/> <xsl:apply-templates mode="copy"/> </xsl:copy> </xsl:template> <!-- Handle default matching --> <xsl:template match="*"/> </xsl:stylesheet>
输出结果:
<?xml version="1.0" encoding="UTF-8"?> <JobRecords> <JobRecord> <Brand>Corporate1</Brand> <WorkTypes> <WorkTypeRecord> <Title>Permanent1</Title> </WorkTypeRecord> </WorkTypes> </JobRecord> <JobRecord> <Brand>Corporate2</Brand> <WorkTypes> <WorkTypeRecord> <Title>Permanent2</Title> </WorkTypeRecord> </WorkTypes> </JobRecord> </JobRecords>