XSLT:如何从某个目录获取文件名? [英] XSLT: How to get file names from a certain directory?

查看:121
本文介绍了XSLT:如何从某个目录获取文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在XSLT中有一个函数可以接受一个目录路径,并将其中的所有文件都返回?

我有一个xml文件,现在这样读取

 < filelist> 
< file> fileA.xml< / file>
< file> fileB.xml< / file>
< / filelist>

现在有一个名为 dir 的目录,文件 fileX.xml fileY.xml 以及其他一些xml文件。我想将这些文件添加到原始XML文件中,以便我可以得到:

 < filelist> 
< file> fileA.xml< / file>
< file> fileB.xml< / file>
< file> fileX.xml< / file>
< file> fileY.xml< / file>
....<! - 其他文件 - >
< / filelist>

是否有XSLT方法来执行此操作?东西,需要在一个目录根,并能够通过它的所有文件迭代?然后我可以调用类似于:

 < xsl:element name = file> 
< xsl:copy> <! - 任何文件名称 - > < XSL:复制和GT;
< / xsl:element> 0



[Edit-solution]



所有的答案都非常有帮助。我最终找到了一个外部解决方案(使用撒克逊)。我认为可能有助于其他人在这里发表我的解决方案,虽然这是非常具体的我自己的情况。

我使用Ant来构建一个java web应用程序,并且在部署之前需要转换一些xml文件。因此,我使用 xslt 任务通过在类路径中添加saxon9.jar来完成这项工作。在我的xsl文件中,我只是做了这样的事情:

 < xsl:for-each select =collection(' ../dir/?select=*.xml')> 
< xsl:element name ='file'>
< / xsl:element>
< / xsl:for-each>


解决方案

XSLT没有内置任务。 XSLT是一种转换语言 - 对于动态输出,您通常需要一个包含所有东西的转换 source (只是以不同的形式)–你不能从空中创建XML。



解决这个问题的三种方法是:
$ b $ ol

  • 用编程语言构建XML,完全舍弃XSLT。这是获得所需结果的最简单的方法。
  • 构建一个接受参数的XSL样式表,将一个(预先构建的)分隔的文件列表放到该参数中,让XSLT处理字符串并从中创建XML。这也涉及到外部处理,基本上这是选项1.另外,你必须编写一个XSL样式表来处理字符串处理(XSL没有适应的东西)。

  • 使用扩展功能,并在XSL内进行目录处理。一个示例如何开始可以在我对这个问题的答案。这是不是很便携,因为扩展功能是特定于平台的。

  • 归结为:


    • 您需要外部编程语言的帮助

    • 您绝对不需要 em> XSLT完成任务,因为XML输出是您所需要的,不需要任何转换。



    错误:不要使用XSL为此。


    Is there a function in XSLT that can takes in a directory path and gives back all the files in it??

    I have a xml file now reads like this

    <filelist>
        <file>fileA.xml</file>
        <file>fileB.xml</file>
    </filelist>
    

    Now, there's directory called dir, has files fileX.xml, fileY.xml and a bunch of other xml files in it. I want to add these files on to the orginal xml file, so that I can get:

    <filelist>
        <file>fileA.xml</file>
        <file>fileB.xml</file>
        <file>fileX.xml</file>
        <file>fileY.xml</file>
        .... <!-- other files -->
    </filelist>
    

    Is there an XSLT way to do this?? something that takes in a dir root, and is able to iterator through all of the files in it?? And then I could just call something like:

    <xsl:element name = file >
         <xsl:copy> <!--whatever file name--> <xsl:copy>
    </xsl:element>0
    

    [Edit-solution]

    all of the answers were very helpful. I ended up finding an external solution (using saxon). I thought it may be helpful for other people to post my solution here, although it is very specific to my own situation.

    I use Ant to build a java web app and need to translate some xml files before deployment. Hence, I was using the xslt task to do the job by adding the "saxon9.jar" in the classpath. And in my xsl file, I just did something like this:

    <xsl:for-each select="collection('../dir/?select=*.xml')" >
         <xsl:element name='file'>
            <xsl:value-of select="tokenize(document-uri(.), '/')[last()]"/>
         </xsl:element>
    </xsl:for-each>
    

    解决方案

    XSLT has nothing built-in for this task. XSLT is a transformation language - for dynamic output you generally need a transformation source that contains everything already (just in a different form) – you cannot create XML from nothing.

    The three ways you can tackle the problem are:

    1. Build the XML in a programming language, leaving out XSLT altogether. This is the simplest way to get the result you want.
    2. Build an XSL stylesheet that accepts a parameter, put a (pre-built) delimited list of files into that parameter, let the XSLT handle the string and make XML from it. This involves external handling as well, basically this is option 1., plus you'd have to write an XSL stylesheet that does string handling (something that XSL has not been geared to)
    3. Use extension functions and do the directory processing from within the XSL. An example how to get started can be found in my answer to this question. This is not very portable as extension functions are platform specific.

    It boils down to this:

    • You will need help from an external programming language
    • You do not absolutely need XSLT do accomplish the task, since XML output is all you need and no transformation is required.

    Ergo: Don't use XSL for this.

    这篇关于XSLT:如何从某个目录获取文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆