xsl-list
[Top] [All Lists]

Generally appending XML document to another one (using XSL)

2003-10-01 00:40:40
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">
        <file>/temp/file1.xml</file>
        <file>/temp/file2.xml</file>
</append>


*******
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
isbn="1">
      <title>XSLT easy</title>    </book>
<title>XSLT easy</title>
   </book>                                                         </book>
   <book isbn="2">                                                 <book
isbn="2">
      <title>Java</title>
<title>Java</title>
   </book>                                                         </book>
</library>                                                         <book
isbn="3">
 
<title>SQL easy</title>
                                                                   </book>
                                                                </library>


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

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


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"
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="{(_at_)root}">
                        <xsl:apply-templates/>
                </xsl:element>
        </xsl:template>

        <xsl:template match="file">
                <xsl:variable name="f"
select="document(string(.),/*)/node()"/>
                <xsl:variable name="pos">
                        <xsl:call-template name="find-pos">
                                <xsl:with-param name="nodes" select="$f"/>
                                <xsl:with-param name="elementName"
select="string(/append/@root)"/>
                        </xsl:call-template>
                </xsl:variable>
                <xsl:copy-of select="$f[position()> $pos]"/>
        </xsl:template>

        <xsl:template name="find-pos">
                <xsl:param name="nodes"/>
                <xsl:param name="elementName"/>
                <xsl:choose>
                        <xsl:when test="count($nodes)=0">
                                <xsl:value-of select="'0'"/>
                        </xsl:when>
                        <xsl:otherwise>
                                <xsl:choose>
                                        <xsl:when test="name($nodes[1]) =
$elementName">
                                                <xsl:value-of
select="number(count(preceding-sibling::node())+1)"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                                <xsl:call-template
name="find-pos">
                                                        <xsl:with-param
name="nodes" select="$nodes[position() > 1]"/>
                                                        <xsl:with-param
name="elementName" select="$elementName"/>
                                                </xsl:call-template>
                                        </xsl:otherwise>
                                </xsl:choose>
                        </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



<Prev in Thread] Current Thread [Next in Thread>