Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

xml - XSLT: How to get file names from a certain directory?

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...