newbie : processing a folder filled with xml-files

K

Koen Wybo

I'm rather new to xml/xslt.
I'm looking for a possibility to proces a folder filled with xml-files
with XSLT. Don't know if this is possible.
In the folder are hundreds of xml-files (with same DTD-structure).
Do know that
<xsl:for-each select="document('first.xml') / |
document('second.xml') /"> processes all the files that are named. But how
can you do this with a folder (without knowing the names of the
files except that it ends with .xml)?

Koen Wybo
 
D

Deirdre Saoirse Moen

Koen Wybo said:
I'm rather new to xml/xslt.
I'm looking for a possibility to proces a folder filled with xml-files
with XSLT. Don't know if this is possible.
In the folder are hundreds of xml-files (with same DTD-structure).
Do know that
<xsl:for-each select="document('first.xml') / |
document('second.xml') /"> processes all the files that are named. But how
can you do this with a folder (without knowing the names of the
files except that it ends with .xml)?

There's always a script way of doing it, which is something that you can
do with 10000000 different tools.

Do you want to process each of them separately or plop them all into one
big document? The former is a bit easier than the latter.

The following assumes unix.

For a very ugly, but prototypable, way to do the former:

ls *.xml | awk '{print("process this "$1)}' | /bin/bash

Substitute "process this" with whatever command you want to use. Omit
the | /bin/bash (or whatever shell) until you have it right.

For example (I could remove the .xml from the filename, but I'm tired,
so I won't):

ls *.xml | awk '{print("/usr/local/bin/fop/fop.sh -xml "$1" -xsl my.xsl
-pdf "$1".pdf")}' | /bin/bash
 
P

Patrick TJ McPhee

[...]

% <xsl:for-each select="document('first.xml') / |
% document('second.xml') /"> processes all the files that are named. But how
% can you do this with a folder (without knowing the names of the
% files except that it ends with .xml)?

You can't, without going outside xslt. One way would be to generate
this list to start with:

echo '<files>' > file.dir
for a in *.xml
do
echo ' <file>'$a'</file>' >> file.dir
done
echo '</files>' >> file.dir

xsltproc file.xsl file.dir

file.xsl would contain something like

<xsl:template match='file'>
<xsl:for-each select="document(.)">
...
</xsl:for-each>
</xsl:template>

with ... being replaced by whatever you currently do with the output
of document().

If your xslt process supports extension, you can write an extension
function to return this information from within the style sheet.
 
R

Rolf Magnus

Patrick said:
echo '<files>' > file.dir
for a in *.xml
do
echo ' <file>'$a'</file>' >> file.dir
done
echo '</files>' >> file.dir

xsltproc file.xsl file.dir

file.xsl would contain something like

<xsl:template match='file'>
<xsl:for-each select="document(.)">
...
</xsl:for-each>
</xsl:template>

with ... being replaced by whatever you currently do with the output
of document().

Or you could use XInclude.

echo '<files xmlns:xi="http://www.w3.org/2001/XInclude">' > file.dir
for a in *.xml
do
echo ' <xi:include href="'$a'"/>' >> file.dir
done
echo '</files>' >> file.dir

xsltproc --xinclude file.xsl file.dir
 
M

Michael TEpperis

Rolf Magnus said:
Or you could use XInclude.

echo '<files xmlns:xi="http://www.w3.org/2001/XInclude">' > file.dir
for a in *.xml
do
echo ' <xi:include href="'$a'"/>' >> file.dir
done
echo '</files>' >> file.dir

xsltproc --xinclude file.xsl file.dir

the first example works for me with saxon but not with xsltproc.
xsltproc -V gives me
using libxml 20610, libxslt 10107 and libexslt 805

what result should the second example give?
how does it works?
i have read the tutorial on www.zvon.org but did not get results.

greetings
michael
 
R

Rolf Magnus

Michael said:
the first example works for me with saxon but not with xsltproc.
xsltproc -V gives me
using libxml 20610, libxslt 10107 and libexslt 805

Mine says:
Using libxml 20510, libxslt 10032 and libexslt 721
what result should the second example give?
how does it works?

xinclude is a standard for including any well-formed xml document into
another xml document by doing something like:

<xi:include href="somefile.xml"/>

Try this:

xinclude.xml:

<?xml version="1.0"?>
<test xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="xinclude2.xml"/>
</test>

xinclude2.xml:

<?xml version="1.0">
<something>
<blah/>
</something>

And now run it through xmllint --xinclude xinclude.xml
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top