RE: [xsl] Generally appending XML document to another one (using XSL) (SOLVED)

Subject: RE: [xsl] Generally appending XML document to another one (using XSL) (SOLVED)
From: Aitor San Juan <asanjuan@xxxxxxxxxxxxxx>
Date: Mon, 6 Oct 2003 08:56:52 +0200
Hello all! I think I came to a solution of what I was trying to.
What I wanted was to develop an XSL to append the content of one
XML document to another. I also wanted the XSL to be as general as
possible, so that I could use the XSL for any kind of XML document.

I don't know whether it is efficient or not. At least it does what
I was after. It might not be so efficient with large documents to
append.

Now, the input XML document to my XSL is of the form:

<append root="a_qname">
    <file>URI to file</file>
    <file>URI to file</file>
    ...
<append>

The value of the "root" attribute is the name of the root element
to be kept in the output, so any element with that name (which
should be the root element name of the documents to append) will
be ignored in the result.

Example:

input XML document:
===================
<append root="library">
    <file>file1.xml</file>
    <file>file2.xml</file>
<append>

Note: file2.xml could have also a root element <library>; this
      would be ignored.

file1.xml                      file2.xml
=========                      =========
<library>                      <book isbn="77">
   <book isbn="1">                <author>A77</author>
      <author>A1</author>         <title>T77</title>
      <title>T1</title>        </book>
   </book>
   <book isbn="2">
      <author>A2</author>
      <title>T2</title>
   </book>
</library>

RESULT:
=======
<library>
   <book isbn="1">
      <author>A1</author>
      <title>T1</title>
   </book>
   <book isbn="2">
      <author>A2</author>
      <title>T2</title>
   </book>
   <book isbn="77">
      <author>A77</author>
      <title>T77</title>
   </book>
</library>


My XSL:
===================================================================

<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

   <xsl:output encoding="ISO-8859-1"
                method="xml" indent="yes" version="1.0"/>

   <xsl:template match="append">
      <xsl:element name="{@root}">
         <xsl:apply-templates/>
      </xsl:element>
   </xsl:template>

   <xsl:template match="file">
      <xsl:call-template name="copier">
         <xsl:with-param name="nodes"
                         select="document(string(.),/*)/node()"/>
         <xsl:with-param name="elementName"
                         select="string(/append/@root)"/>
      </xsl:call-template>
   </xsl:template>

   <xsl:template name="copier">
      <xsl:param name="nodes"/>
      <xsl:param name="elementName"/>
      <xsl:choose>
         <xsl:when test="count($nodes)=0">
            <xsl:value-of select="$nodes"/>
         </xsl:when>
         <xsl:when test="count($nodes)=1 and (name($nodes[1]) !=
$elementName)">
            <xsl:copy-of select="$nodes"/>
         </xsl:when>
         <xsl:when test="name($nodes[1]) = $elementName">
            <xsl:copy-of select="$nodes/*"/>
         </xsl:when>
         <xsl:otherwise>
            <xsl:call-template name="copier">
               <xsl:with-param name="nodes"
                               select="$nodes[position() > 1]"/>
               <xsl:with-param name="elementName"
                               select="$elementName"/>
            </xsl:call-template>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>

</xsl:stylesheet>


************ LEGEZKO OHARRA / AVISO LEGAL / LEGAL ADVICE *************
Mezu honek isilpeko informazioa gorde dezake, edo jabea duena, edota legez
babestuta dagoena.
Zuri zuzendua ez bada, bidali duenari esan eta ezabatu, inori berbidali
edo gorde gabe,legeak debekatzen duelako mezuak erabiltzea baimenik gabe.
--------------------------------------------------------------------------
Este mensaje puede contener información confidencial, en propiedad o
legalmente protegida.
Si usted no es el destinatario, le rogamos lo comunique al remitente
y proceda a borrarlo, sin reenviarlo ni conservarlo, ya que su uso no 
autorizado está prohibido legalmente.
--------------------------------------------------------------------------
This message may contain confidential, proprietary or legally privileged
information.
If you are not the intended recipient of this message, please notify it to
the sender and delete without resending or backing it, as it is legally
prohibited.
************************************************************************** 



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread