create on output multiple xml files

Here should go questions about transforming XML with XSLT and FOP.
JMarie
Posts: 2

create on output multiple xml files

Tue Feb 12, 2013 10:25 pm

Hi, I'm very new to transformation scenarios. I'm practicing with a test xml file (see below). What I would like is to create multiple .xml files where each <cd> and its children and information is in a new cd_{position #}.xml file. I've tried using xsl:result-document (see below). In the results, all I get is the following:
<?xml version="1.0" encoding="UTF-8"?> and no files are written to my folder where my xml and xsl files are located. I've also tried using the extension for xslt 1.0 and the result is again nothing written in my folder but the ext:document shows up in my results. I'd prefer to figure this out with xslt 2.0 and xsl:result-document. Any help would be much appreciated.

Thanks,
Jen

xml file:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
</catalog>


xslt file:

Code: Select all

<?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" encoding="UTF-8" indent="yes" name="xml"/>
    <xsl:template match="/">
        <xsl:for-each select="cd">
            <xsl:result-document method="xml" href="cd_{position()}.xml" encoding="UTF-8" indent="yes">
                <catalog>
                    <xsl:copy-of select="."/>
                </catalog>
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
adrian
Posts: 2453

Re: create on output multiple xml files

Wed Feb 13, 2013 1:02 pm

Hi,

Use:

Code: Select all

<xsl:for-each select="catalog/cd">
This is necessary because <xsl:template match="/"> actually matches the XML document root(#document) not the root element (catalog).

Alternatively, you can change the template to match the root element:

Code: Select all

<xsl:template match="/*">

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
JMarie
Posts: 2

Re: create on output multiple xml files

Wed Feb 13, 2013 4:41 pm

Hi Adrian,

I began with <xsl:for-each select="catalog/cd">. I changed it back. I receive the message that the transformation was successful and took 0.0s to complete and nothing pops up in the result window. Also, no separate xml files are written to my folder where I want them. So I'm still unsure as to what's going on.
adrian
Posts: 2453

Re: create on output multiple xml files

Wed Feb 13, 2013 5:24 pm

Hi,

Have you tested this change on the XML sample you provided above? That what I tested and it worked for me.
Or do you have a larger XML file and you're testing it against that?

You should consider using the XSLT debugger from Oxygen (Document > Transformation > Debug Scenario) to investigate what's going on.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
kalinma
Posts: 2

Re: create on output multiple xml files

Tue Jun 23, 2015 12:06 am

Just in case anyone else has had the same problem as JMarie here:
In the results, all I get is the following:
<?xml version="1.0" encoding="UTF-8"?> and no files are written to my folder where my xml and xsl files are located.
. . .
I receive the message that the transformation was successful and took 0.0s to complete and nothing pops up in the result window.

I solved it when I checked the actual transformer being used in the "Configure Transformation Scenario" dialog box. Even though I had chosen Saxon PE 9.5 in the general preferences, is was not selected in the Transformation Scenario. After selecting Saxon PE 9.5 in that dialog, my transformation worked perfectly. Very puzzling, but happily on to another puzzle now. :)
adrian
Posts: 2453

Re: create on output multiple xml files

Tue Jun 23, 2015 12:27 pm

Hello kalinma,

kalinma wrote:I solved it when I checked the actual transformer being used in the "Configure Transformation Scenario" dialog box. Even though I had chosen Saxon PE 9.5 in the general preferences, is was not selected in the Transformation Scenario. After selecting Saxon PE 9.5 in that dialog, my transformation worked perfectly. Very puzzling, but happily on to another puzzle now. :)

What transformer did you previously use? If you're using the wrong transformer for your stylesheet version, when you run the transformation, Oxygen will prompt you and recommend to switch to Saxon-HE/PE/EE (for XSLT 2.0 or 3.0).
But in JMarie's case, it's unlikely that was the problem, because xsl:result-document always fails with an error if using the wrong (XSLT 1.0) transformer.

Note that there is no general preference that sets the default transformer, there's only a general preference for XSLT validation where you can pick the default XSLT engine to be used when validating a specific version of XSLT. The transformer must be selected in the transformation scenario configuration.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
kalinma
Posts: 2

Re: create on output multiple xml files

Tue Jun 23, 2015 4:42 pm

Hi Adrian,
If you go to Options->Preferences and select XSLT in the list on the left side, you will see options for "XML / XSLT-FO-XQuery / XSLT" and underneath that "JAXP XSLT Transformer". There you can choose a validation engine for each of XSLT 1.0, 2.0, 3.0. This dialog is not the same as the one in "Configure Transformation Scenario." Perhaps this difference is the result of my using oXygen Author (v. 15.2) as my company will not spring for oXygen Developer.
Thanks.
adrian
Posts: 2453

Re: create on output multiple xml files

Tue Jun 23, 2015 5:33 pm

Hi,

The preferences section you mentioned (Options > Preferences, XML > XSLT-FO-XQuery > XSLT) allows you to pick the XSLT validation engine, but that doesn't affect the transformations in any way. There is no difference regarding these options between Author, Developer, Editor, however, in Author they are actually meaningless, because there's no specialized editor for XSL files, so no support for XSL editing and validation, just support for XSLT transformations (in the "Configure Transformation Scenario(s)" dialog and corresponding view).

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 0 guests