3

I have some XSL files that work through all the files in a directory recurively, basically it reads in a variable called collection:

<xsl:variable name="collection">
        <xsl:copy-of select="collection(iri-to-uri('./?select=*.xml;recurse=yes'))/*"/>
    </xsl:variable>

and then later on, we have something like:

<xsl:for-each-group     select="$collection/configuration/services/service" group-by="serviceKey" >

This was fine for a while but now I want to pass in alist of files that would make up the variable collection. Im working with java so I caneffectively pass in a comma delimined list of files paths or URI's, but I'm not sure how to handle that in the XSL file, so that it populates the collection variable like the recursive iri-to-uri function was working.

1
  • 1
    Do note that instead of <xsl:variable name="collection"><xsl:copy-of select="collection(...)"/></xsl:variable> you can simply use <xsl:variable name="collection" select="collection(...)"/> which should be more efficient as it loads and parses the documents once while your code creates a copy of parsed documents. Commented Jun 23, 2015 at 8:40

2 Answers 2

2

Another solution would be to implement a CollectionURIResolver in your Java application. You can nominate this to Saxon before running your transformation. When you call the collection() function, Saxon will call your CollectionURIResolver, which can pass back a list of documents or URIs.

http://www.saxonica.com/documentation/index.html#!javadoc/net.sf.saxon.lib/CollectionURIResolver

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Michael thanks for the answer. It worked perfectly. Created a CollectionURIResolver that takes in a list of Files and converts them into a list of AnyURIValue objects thats returned via the resolve method.
0

If you want to pass a list of documents from your environment to the stylesheet, use a sequence of document nodes:

<xsl:param name="docs" as="document-node()+"/>

If you prefer passing only their URIs, then use a sequence of strings (or maybe of xs:anyURI):

<xsl:param name="uris" as="xs:string+"/>
<xsl:param name="docs" select="$uris ! doc(.)"/>

If you want to pass a list of collection URIs, like the one you use in your example, giving a directory and a filename pattern (this is the Saxon way to resolve collection URIs), then use the following instead:

<xsl:param name="uris" as="xs:string+"/>
<xsl:param name="docs" select="$uris ! collection(.)"/>

Note that in the 2 last code snippets above, having $docs defined as a parameter, with a default value depending on another parameter, enable the user to pass either the value of docs directly (a sequence of document nodes), or to pass their URIs instead (you have to change xs:string+ to xs:string* though, if you want to accept an empty sequence for it).

If you want the environment to be able to pass document nodes, document URIs and collection URIs, and combine them, then:

<xsl:param name="uris"  as="xs:string*"/>
<xsl:param name="colls" as="xs:string*"/>
<xsl:param name="docs"  as="document-node()*"/>

<xsl:variable name="my-docs" select="
   $uris ! doc(.), $colls ! collection(.), $docs"/>

PS: You do not need iri-to-uri() in your example.

PS II: Note that the operator ! is 3.0. If you cannot use it, use for $uri in $uris return doc($uri) instead of $uris ! doc(.).

6 Comments

Thanks Florent for the answer... So if I provide a parameter via my Java code called uris, which is a comma delimited list of the URI's to go though, how does the use of <xsl:param name="uris" as="xs:string+"/> <xsl:param name="docs" select="$uris ! collection(.)"/> Know how to parse the URIs as different files? Sorry, my XSLT knowledge is pretty basic :(
Why do you want it to be a string to parse? What do not you pass a sequence of strings? If the question now is how to pass a sequence of strings to a parameter using API "xyz" (e.g. Saxon S9API), I would start a new question.
I think the answer should point out that the ! operator is part of XPath 3.0 so even with Saxon it is only available with Saxon 9.6 and a version="3.0" stylesheet.
Florent, I dont know how long the list of files is, it depends on the work done in the Java code before hand. Basically I was looking to provide a parameter into the XSLT file from my java code. but since that list of files could be 1 or 100 files long I would need to parse it no? I don't think its a separate question, though I did edit the title to remove Java from it, so I might add that back in...
And my style sheet version is 2.0
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.