xsl-list
[Top] [All Lists]

Re: [xsl] XSLT merge documents into variable

2007-08-10 00:13:16
Please try the following stylesheet:

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

  <xsl:output method="xml" indent="yes" />

  <xsl:variable name="doc" select="/" />

  <xsl:template match="/files">
    <data>
      <xsl:for-each select="document(file[1]/@href)/data/line">
        <line>
          <xsl:copy-of select="@ref" />
          <xsl:variable name="ref" select="@ref" />
          <xsl:attribute name="amount">
            <xsl:variable name="sumOfAmounts">
              <xsl:for-each select="$doc/files/file">
                <num><xsl:value-of
select="document(@href)/data/line[(_at_)ref = $ref]/@amount" /></num>
              </xsl:for-each>
            </xsl:variable>
            <xsl:value-of select="sum(exslt:node-set($sumOfAmounts)/num)" />
          </xsl:attribute>
        </line>
      </xsl:for-each>
    </data>
  </xsl:template>

</xsl:stylesheet>

On 8/10/07, Lars Kappert <larskappert(_at_)gmail(_dot_)com> wrote:
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[(_at_)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>


-- 
Regards,
Mukul Gandhi

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--