Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old October 24th, 2007, 12:58 PM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Output to multiple files

Hi, I need to create a XSLT to output to multiple files. I tried using <xsl:document href...> or <saxon:output href..> with no luck.

Something like this

     <xsl:for-each...>
        <xsl:variable name="filename">
            <xsl:value-of select="name"/>.xml
        </xsl:variable>

        <saxon:output href="$filename" method="xml">

        </saxon:output>
     <xsl:for-each/>

I still get everything in one file and the file is not named the way I want.

Let me explain, in the following sample, I would need one person per file, I just need to figure out where to insert the above code in the XSLT (tried different places and still does not work)

XML:

<?xml version="1.0" encoding="UTF-8"?>
<people>
    <person>
        <name>Leo</name>
        <last>Santos</last>
    </person>
    <person>
        <name>Ka</name>
        <last>Lopez</last>
    </person>
    <person>
        <name>Seb</name>
        <last>Santos</last>
    </person>
</people>

XSLT:

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

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


    <xsl:template match="people" name="process_multiple">
        <xsl:element name="mypeople">
            <xsl:for-each select="person">
                    <xsl:variable name="filename"><xsl:value-of select="name"/>.dita </xsl:variable>
                    <xsl:element name="Name">
                        <xsl:value-of select="name"/>
                    </xsl:element>
                    <xsl:element name="Last">
                        <xsl:value-of select="last"/>
                    </xsl:element>
            </xsl:for-each>
        </xsl:element>

    </xsl:template>

</xsl:stylesheet>

Any ideas???

Thanks

Leo


  #2 (permalink)  
Old October 24th, 2007, 01:10 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Rather than using vendor-specific extensions to XSLT 1.0, you would be better off using XSLT 2.0, for example in Saxon 8.9.

Then it's

<xsl:for-each select="person">
        <xsl:result-document href="{name}.xml" method="xml">

        </xsl:result-document>
</xsl:for-each>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #3 (permalink)  
Old October 24th, 2007, 01:36 PM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your quick response.

I tried this using Oxygen:

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

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


    <xsl:template match="people" name="process_multiple">
        <xsl:element name="mypeople">
            <xsl:for-each select="person">
                <xsl:result-document href="{name}.xml" method="xml">
                    <xsl:element name="Name">
                        <xsl:value-of select="name"/>
                    </xsl:element>
                    <xsl:element name="Last">
                        <xsl:value-of select="last"/>
                    </xsl:element>
                </xsl:result-document>
            </xsl:for-each>
        </xsl:element>

    </xsl:template>

But the output is an empty file (named Untitle48.xml)

<?xml version="1.0" encoding="UTF-8"?>
<mypeople/>

If I remove the <xsl:result-document..> statements then I get the desired output but in one file

<?xml version="1.0" encoding="UTF-8"?>
<mypeople>
   <Name>Leo</Name>
   <Last>Santos</Last>
   <Name>Ka</Name>
   <Last>Lopez</Last>
   <Name>Seb</Name>
   <Last>Santos</Last>
</mypeople>

I am using Oxygen to trigger the transform, is it possible it can't handle opening multiple file outputs?

Leo

  #4 (permalink)  
Old October 24th, 2007, 02:21 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The main output file only contains the <mypeople/> element because the rest of the content has been written elsewhere, as you requested.

If you're not sure exactly where it has been written, the Saxon -t flag will tell you. I don't know how you set that in Oxygen.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #5 (permalink)  
Old October 24th, 2007, 02:36 PM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's right, found the files in the same folder where the XSL is, I guess I was just expecting Oxygen to auto-open them as it does with the main file

Thanks a lot, you have been of a great help. I am new to 2.0, it seems to add features to easy programming in general, I just need time to learn so I just ordered your XSLT 2.0 Programmer's Reference from amazon.ca.



Similar Threads
Thread Thread Starter Forum Replies Last Post
compare 2 files and output third files mtnguye6 VBScript 0 April 14th, 2008 11:03 AM
jaxp and xslt2 multiple output aowss XSLT 2 October 10th, 2007 01:21 AM
Save (output to) multiple files from single click moo_desanta Classic ASP Basics 2 June 23rd, 2006 02:00 AM
Importing Multiple files in Multiple tables Versi Suomi Access 6 June 1st, 2005 08:47 AM
Rich Text Files -Output on Landscape on 8.5 x 11 aster Pro VB 6 5 February 18th, 2004 12:06 PM





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