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

Subject: [xsl] Generally appending XML document to another one (using XSL)
From: Aitor San Juan <asanjuan@xxxxxxxxxxxxxx>
Date: Wed, 1 Oct 2003 09:40:40 +0200
Hello all!

	I am trying to write an XSL as general as possible to append
documents at the end of others. You can see the XML input file and the XSL
I'm working on. The "root" attribute of the <append> element is the name of
the root element to be kept in the output, and under which all the child
elements are appended. Let me explain myself with the following examples.

XML Input file:

<?xml version="1.0"?>
<append root="library">

Case 1: Only one of the files have a root element name which is the same as
the value specified
******* for the "root" attribute of tag <append>.

file1.xml:                        file2.xml:                    result.xml:
==========                        ==========                    ===========
<library>                         <book isbn="3">               <library>
   <book isbn="1">                   <title>SQL easy</title>       <book
      <title>XSLT easy</title>    </book>
<title>XSLT easy</title>
   </book>                                                         </book>
   <book isbn="2">                                                 <book
   </book>                                                         </book>
</library>                                                         <book
<title>SQL easy</title>

Case 2: Both files have a root element name with the same name specified in
"root" attribute of
******* <append> tag

file1.xml:                        file2.xml:
==========                        ==========
<library>                         <library>                       <library>
   <book isbn="1">                   <book isbn="3">                 <book
      <title>XSLT easy</title>          <title>SQL easy</title>
<title>XSLT easy</title>
   </book>                           </book>                         </book>
   <book isbn="2">                </library>                         <book
   </book>                                                           </book>
</library>                                                           <book
<title>SQL easy</title>

This is the XSL I'm working on. For each <file> element, I parse and load
the document and save the tree in a variable. My idea is to find the
position of the "root" element inside the document XML just loaded, in the
examples, the position of "library" (if there's one), and copy in the output
tree all its children except that root. The files pointed by the <file>
elements may include comments, so in the examples, the <library> might not
be the first node. CAn anybody help me on this? I don't know what I am doing
wrong in the XSL.

Thanks in advance.

My XSL sheet:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0"

	<xsl:output encoding="ISO-8859-1" method="xml" indent="yes"

	<xsl:template match="append">
		<xsl:element name="{@root}">

	<xsl:template match="file">
		<xsl:variable name="f"
		<xsl:variable name="pos">
			<xsl:call-template name="find-pos">
				<xsl:with-param name="nodes" select="$f"/>
				<xsl:with-param name="elementName"
		<xsl:copy-of select="$f[position()> $pos]"/>

	<xsl:template name="find-pos">
		<xsl:param name="nodes"/>
		<xsl:param name="elementName"/>
			<xsl:when test="count($nodes)=0">
				<xsl:value-of select="'0'"/>
					<xsl:when test="name($nodes[1]) =
name="nodes" select="$nodes[position() > 1]"/>
name="elementName" select="$elementName"/>


************ 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
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

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

Current Thread