2

I am working with xslt 1.0 and trying to use the XSLT document function to apply the stylesheet to a hierarchy of folders. The folder structures is as below, but I cannot seem to find any reliable references on the Web on how to do this.

a/
└── b
    └── c
        ├── d
        ├── e
        ├── f

Is there a way I can apply my stylesheet to nodes, in a file, in folder f via a file in folder a (a has links to file names in folder hierarchy).

Update #2

book01.xml
<?xml version="1.0" encoding="utf-8" ?>
<book location="../collection/book01.xml">
    <chapter>chapter001</chapteer>
</book>

chapter01.xml
<?xml version="1.0" encoding="utf-8" ?>
<chapter location="../collection/book01/chapter01.xml">
    <page>page01</page>
</chapter>

page01.xml
<?xml version="1.0" encoding="utf-8" ?>
<page location="../collection/book01/chapter01/page01.xml">
    <pagenumber>page 1</pagenumber>
    <text>
      page one.
    </text>
</page>

Output

Book Name: Book XX
  Chapter XX
    Page XX
      page xx.
lightonphiri
  • 782
  • 2
  • 14
  • 29
  • I forgot to mention that my initial plan was to work with for-each elements, but not quite sure if nesting the elements could help... perhaps its something I ought to try out. – lightonphiri Jun 14 '11 at 02:41
  • Which XSLT processor are you using? – Emiliano Poggi Jun 14 '11 at 05:46
  • Do you need to apply the same stylesheet to a set of files and get a different output for each one of them also? I mean do you need a result document each time the transform is applied? – Emiliano Poggi Jun 14 '11 at 05:47
  • @empo: I am using xsltproc and yes, the idea is to apply the same stylesheet. The folder hierarchy is such that the information only makes sense when combined from the hierarchy, so I need the stylesheet applied recursively in a way. – lightonphiri Jun 14 '11 at 07:28
  • Can you provide a small but meaningful example of file a and f and a sample output? – Emiliano Poggi Jun 14 '11 at 07:56
  • @empo: please see update #2 in original post. thank you. – lightonphiri Jun 14 '11 at 08:36

3 Answers3

2

I'm not sure this is a feasable/reasonable way to implement what you want achieve in the context of your use case; however, you can stay with you initial plan, that is working with xsl:for-each and document().

For example, assume you have the input file with the list of paths:

<files>
    <file>book001.xml</file>
    <file>chapter001.xml</file>
</files>

This input can be reasonably used to define a variable containing all your input documents and apply templates:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    version="1.0">

    <xsl:template match="files">
        <xsl:variable name="docs">
            <docs>
                <xsl:for-each select="file">
                    <xsl:copy-of select="document(.)"/>
                </xsl:for-each>
            </docs>
        </xsl:variable>

        <xsl:apply-templates select="msxsl:node-set($docs)"/>
    </xsl:template>

    <!-- now you can match elements of your xml files -->

</xsl:stylesheet>

Notice that I needed the extension function in order to evaluate to a node-set. This is definetly available in xsltproc, or you can get it from EXSLT anyway.

In the example I've assumed that the input file is in the same folder of the book001.xml and chapter001.xml file.

Emiliano Poggi
  • 24,390
  • 8
  • 55
  • 67
  • I will try this out; Thank you. What would be a feasible/reasonable way of tackling such a problem? any suggestions on how I can go about this? PS: I am working on a linux machine; I am assuming msxsl will only work with Windows... am I right? – lightonphiri Jun 14 '11 at 11:01
  • This is might be a reasonable way if fits your requirements :), and no, apart the msxsl node-set extension it will work on any system. You have the same kind of extensions in xsltproc anyway. – Emiliano Poggi Jun 14 '11 at 11:17
  • For node-set via EXSLT you can see the @Dimitre answer to this [SO question](http://stackoverflow.com/questions/92076/how-to-use-node-set-function-in-a-platform-independent-way). – Emiliano Poggi Jun 14 '11 at 11:39
0

If the links are relative then they are resolved against the base URI of the initial stylesheet, so that might not work. In XSLT 2.0 you have the resolve-uri function for that.


Maybe you could implement a resolve-uri extension function.
Max Toro
  • 28,282
  • 11
  • 76
  • 114
0

Many XSLT 2.0 processors implement the collection() function in such a way as to allow you to interrogate directory structures in filestore. I'm not aware of any equivalent extensions in XSLT 1.0 processors.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • thank you; however, my solution needs to make use of XSLT 1.0. On a side note, your XSLT programmer's reference book is a great source of help. – lightonphiri Jun 14 '11 at 08:39