Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 28th, 2006, 05:08 AM
Authorized User
 
Join Date: Jun 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rajatake
Default problem in merging of XML Files

My requirement is as follows.

I have to merge 10 XML Files.

the input xml file structure is

<?xml version="1.0"?>
<document>
   <title>tile</title>
   <location>Geneva</location>
   <date>April 2003</date>
</document>

the output would be
<?xml version="1.0"?>
<rootnode>
<document>
   <title>tile</title>
   <location>Geneva</location>
   <date>April 2003</date>
</document> //from ten files
</rootnode>


i have written .xsl file which combines all the XML files.but i don't know how to create the <rootnode> rootelement in the .xsl.

please help me.
thanks in advance
Rajraman.N


 
Old April 28th, 2006, 05:45 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Show us your code, and we can show you where to add the element constructor.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 28th, 2006, 07:41 AM
Authorized User
 
Join Date: Jun 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rajatake
Default

The following is my .xsl code

?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" omit-xml-declaration="no" encoding="UTF-8"/>

<xsl:template match="/">
<xsl:for-each select="index/file">
       <xsl:apply-templates select="document(@filename)/root/document"/>
     </xsl:for-each>
</xsl:template>

<xsl:template match="root/document">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="title">
<xsl:copy>
   <xsl:apply-templates/>
   </xsl:copy>
</xsl:template>

<xsl:template match="location">
<xsl:copy>
  <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="date">
<xsl:copy>
   <xsl:apply-templates/>
   </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Thanks in advance
N.Rajaraman

 
Old April 28th, 2006, 08:31 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:output method="xml" omit-xml-declaration="no" encoding="UTF-8"/>
  <xsl:template match="/">
    <documents>
      <xsl:for-each select="index/file">
        <xsl:copy-of select="document(@filename)"/>
      </xsl:for-each>
      </documents>
   </xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
 
Old April 29th, 2006, 07:47 AM
Authorized User
 
Join Date: Jun 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rajatake
Default

Thanks Joe. Its working fine.

I have one more help.

If i stored all XML files in one folder, the following xsl is working fine.

.XSL code
<xsl:template match="/">
<xsl:for-each select="index/file">
       <xsl:apply-templates select="document(@filename)/root/document"/>
     </xsl:for-each>
</xsl:template>

.XML Code
<?xml version="1.0"?>
<index>
   <title>XML Files</title>
   <file filename="geneva.xml"></file>
   <file filename="london.xml"></file>
  </index>

if the each files are stored in sub folders under one root folder, what changes i have to make in .xsl and .xml files.

Thanks in advance.
Rajraman.N


 
Old April 29th, 2006, 12:57 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

When the document() function sees a relative URI, it resolves it against some base URI. If the argument to the document() function is a node, as in your example, then it uses the base URI of that node. In practice (given a relative URI such as geneva.xml) this means it looks in the directory containing the XML document that contains the relative URI. If the argument to the document() function is a string, on the other hand, then it is resolved relative to the stylesheet.

In XSLT 1.0 there are two ways you can change this behaviour. You can specify a base URI in the second argument of the document() function (see the spec for details) or you can modify the relative URI by string manipulation, for example concat("../subdir/", @filename).

In 2.0 there are many more choices, because you have access to functions such as base-uri() and resolve-uri() than give you a lot more control.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
BizTalK - Merging of files BTUser Biztalk 0 November 14th, 2008 10:55 PM
Grabbing content from 2 XML files and merging yanzickp XSLT 4 August 6th, 2007 10:07 AM
Merging files ajindal General .NET 2 January 10th, 2007 06:42 AM
Problem to create an xml file from two xml files saurabh_inblore XSLT 1 April 12th, 2006 02:58 AM
Merging XML Help Please sam666 XML 7 March 8th, 2006 02:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.