Subject: XSLT merge documents into variable
From: "Lars Kappert" <larskappert@xxxxxxxxx>
Date: Fri, 10 Aug 2007 08:27:58 +0200
|
Bit of a newbie here. I want do some quite simple file processing. I have a
basic source file with file references in it. These files should be merged
into a variable, which can then be further processed; in my example below the
@amount of each line[@ref] is summed. But I'm already stuck in the first part
of this, how do I correctly put file1.xml and file2.xml into a variable which
then contains (psuedo)
"<root><data><lines/></data>><data><lines/></data></root>"? And how can I then
use that variable as a basis for further processing?
I'm using XSLT 1.0.
<!-- To XML to process -->
<?xml version="1.0"?>
<files>
<file href="file1.xml"/>
<file href="file2.xml"/>
</files >
<!-- file1.xml -->
<data>
<line ref="A" amount="2"/>
<line ref="B" amount="3"/>
</data>
<!-- file2.xml -->
<data>
<line ref="A" amount="4"/>
<line ref="B" amount="8"/>
</data>
<!-- result should be -->
<data>
<line ref="A" amount="6"/>
<line ref="B" amount="11"/>
</data>
<!-- XSLT -->
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>
<xsl:templates match="files">
<xsl:variable name="m">
<xsl:apply-templates select="document(file/@href)"/>
</xsl:variable>
<xsl:apply-templates select="exslt:node-set($m)" mode="merge"/>
</xsl:templates>
<xsl:template match="data" mode="merge">
<root>
<xsl:copy-of select="."/>
</root>
</xsl:template>
</xsl:stylesheet>
|