XSLT:如何从某个目录获取文件名? [英] XSLT: How to get file names from a certain directory?
问题描述
在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
归结为:
- 您需要外部编程语言的帮助
- 您绝对不需要 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:
- Build the XML in a programming language, leaving out XSLT altogether. This is the simplest way to get the result you want.
- 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)
- 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屋!