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

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

问题描述

XSLT 中是否有一个函数可以接收一个目录路径并返回其中的所有文件??

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

我有一个 xml 文件现在是这样的

I have a xml file now reads like this

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

现在,有一个名为 dir 的目录,里面有文件 fileX.xmlfileY.xml 和一堆其他的 xml 文件.我想将这些文件添加到原始 xml 文件中,以便我可以得到:

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>

是否有 XSLT 方法可以做到这一点?接受一个目录根的东西,并且能够遍历其中的所有文件?然后我可以调用类似的东西:

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

所有的答案都非常有帮助.我最终找到了一个外部解决方案(使用撒克逊人).我认为其他人在这里发布我的解决方案可能会有所帮助,尽管它非常适合我自己的情况.

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.

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

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 没有为此任务内置任何内容.XSLT 是一种转换语言 - 对于动态输出,您通常需要一个转换,其中包含已经包含的所有内容(只是形式不同)–你不能从无到有创建 XML.

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. 用编程语言构建 XML,完全省略 XSLT.这是获得所需结果的最简单方法.
  2. 构建一个接受参数的 XSL 样式表,将(预构建的)分隔文件列表放入该参数中,让 XSLT 处理字符串并从中生成 XML.这也涉及外部处理,基本上这是选项 1.此外,您还必须编写一个 XSL 样式表来处理字符串(XSL 还没有适应这种情况)
  3. 使用扩展函数并在 XSL 中进行目录处理.我对此问题的回答.这不是很便携,因为扩展功能是特定于平台的.
  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.

归结为:

  • 需要外部编程语言的帮助
  • 您并不绝对需要 XSLT 可以完成这项任务,因为您只需要 XML 输出并且不需要转换.
  • 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.

因此:不要为此使用 XSL.

Ergo: Don't use XSL for this.

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

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