XSLT Document Function - Folder Hierarchy

此生再无相见时 提交于 2020-01-24 12:26:47

问题


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.

回答1:


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.




回答2:


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.


回答3:


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.



来源:https://stackoverflow.com/questions/6338485/xslt-document-function-folder-hierarchy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!