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 June 25th, 2013, 11:15 AM
Registered User
Points: 21, Level: 1
Points: 21, Level: 1 Points: 21, Level: 1 Points: 21, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default Copy output XML files to multiple folders

Hello,

I am struggling with the following issue, which is that I need to be able to output XML files to multiple folders based on the XML filename using XSLT.

All files with names starting with B should go to one folder, files starting C go to another folder and so on..

I have the following XSLT and it works fine to generate the needed files, but I am not able to output to multiple folders, which is what I need.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/">
    <xsl:output method="xml" doctype-public="-//OASIS//DTD DITA Glossary//EN"
        doctype-system="glossary.dtd" omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="title"/>
    <xsl:param name="files"
        select="collection('../../../DITA/?select=%5BA-Z%2D%27%20%28%29%2C%5D%7B2,%7D.dita;recurse=yes')"/>
    <xsl:template match="node()">
        <xsl:copy copy-namespaces="no">
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/">
        <xsl:for-each select="$files//topic">
            <xsl:value-of select="text()" disable-output-escaping="yes"/>
            <xsl:result-document href="DITA/glossentries_all/{title/text()|title/b/text()}.dita">
                <glossentry id="{concat('dita', generate-id())}">
                    <glossterm id="{concat('my_title', generate-id())}">
                        <xsl:value-of select="title"/>
                    </glossterm>
                    <glossdef>
                        <xsl:for-each select="body">
                            <xsl:apply-templates/>
                        </xsl:for-each>
                        <shortdesc/>
                    </glossdef>
                </glossentry>
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
Here is the resulting DITA XML file.

Code:
<!DOCTYPE glossentry
  PUBLIC "-//OASIS//DTD DITA Glossary//EN" "glossary.dtd">
<glossentry xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/"
            id="testd30e1">
   <glossterm id="dita_titled30e1">Glossary entry test</glossterm>
   <glossdef>
      <p>DITA XML content text.</p>
      <shortdesc/>
   </glossdef>
</glossentry>
I am using Saxon-EE 9.5.02. The desired filename is derived from the title of the DITA file, so with the above sample the title is Glossary entry test.dita and that part is working fine.

Last edited by eloralon; June 25th, 2013 at 12:53 PM..
  #2 (permalink)  
Old June 25th, 2013, 11:45 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Which XSLT 2.0 processor do you use?
And how does DITA/glossentries_all/{title/text()|title/b/text()}.dita look? Does that already include the wanted file name?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
  #3 (permalink)  
Old June 25th, 2013, 12:09 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

I would expect to see something like

Code:
href="DITA/glossentries_all/{substring(title,1,1)}/{title}.dita
if I have understood the requirement correctly
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
eloralon (June 25th, 2013)
  #4 (permalink)  
Old June 25th, 2013, 12:58 PM
Registered User
Points: 21, Level: 1
Points: 21, Level: 1 Points: 21, Level: 1 Points: 21, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks Michael,

The piece of code you sent did it for me.

Could you please explain a little bit the snippet as I am still learning.

Thanks a million.
  #5 (permalink)  
Old June 25th, 2013, 01:07 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

I'm not sure which bit of it you don't understand.

You had
Code:
{title/text() | title/b/text()}
which looked to me like a slightly clumsy attempt to extract the string value of the title element, which can be done much more simply by just writing "title" (it's atomized automatically, which usually gives you what you want).

substring(title,1,1) just extracts the first character of the string value of the title.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
  #6 (permalink)  
Old June 25th, 2013, 01:18 PM
Registered User
Points: 21, Level: 1
Points: 21, Level: 1 Points: 21, Level: 1 Points: 21, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default

I had this bit
Code:
{title/text() | title/b/text()}
because my source XML files containing a bold tag would not get transformed but with it they got transformed. Your code is better and I even gained about 4 seconds in performance.

Thanks for the additional explanation.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy full XML to output as string jay_c XSLT 2 August 7th, 2009 11:39 AM
Output to multiple files lsantos2000 XSLT 4 October 24th, 2007 02:36 PM
Save (output to) multiple files from single click moo_desanta Classic ASP Basics 2 June 23rd, 2006 02:00 AM
Copy Files and Folders on Local Network rupakroy5 VB.NET 2002/2003 Basics 1 May 22nd, 2006 10:09 AM
Copy files & Folders application in ASP.net rupakroy5 ASP.NET 1.0 and 1.1 Basics 0 May 8th, 2006 10:45 PM





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