Page 1 of 1

Custom Output File Name in XSLT Transformation Scenarios

Posted: Sat Feb 02, 2013 10:58 pm
by sarcanon
There must be a way to do this, but after spending almost an hour reviewing the docs and this forum, I cannot find it.

I would like to define the output file name in an XSLT Transformation Scenario using content from within the XML document currently being transformed. I've carefully reviewed all of the Editor Variables available, but they all seem to reference information external to the XML document, mostly file paths and file system information. I've also looked at Custom Editor variables, but insofar as I can tell this functionality only allows the definition of static values. There are ways to reference environment and Java system variables, but, again, these are all external to the file being transformed.

Ideally, I would like to access a processing instruction or provide an XPath to an element within the file that I could use to construct the output file name.

Is this sort of thing possible?

Thank you in advance.

Re: Custom Output File Name in XSLT Transformation Scenarios

Posted: Mon Feb 04, 2013 4:39 pm
by adrian
Hello,

I'm afraid that's not possible, at least not for the main output file specified in the transformation scenario. The output is specified for the transformer/engine at the moment the transformation starts, so you can't make it dynamic depending on the content.

However, assuming you're using XSLT 2.0, why not use xsl:result-document in your stylesheet?
e.g.

Code: Select all

<xsl:result-document href="file{element/id}.xml">
...
</xsl:result-document>
This xsl:result-document block will output to a file named: fileN.xml where N is has the value of the XPath expression element/id.

Regards,
Adrian

Re: Custom Output File Name in XSLT Transformation Scenarios

Posted: Mon Feb 04, 2013 6:29 pm
by sarcanon
So, use xsl:result-document in lieu of the Save As action in the transformation scenario?

That seems like a perfectly satisfactory solution.

Thank you.

Re: Custom Output File Name in XSLT Transformation Scenarios

Posted: Tue May 27, 2014 2:42 am
by Rumplestiltzkin
Just wondering if anything has changed in this respect in the last 5 years? I would _really_ like to be able to have output file names from an XSL transformation that vary according to content. There are reasons why I can't really use xsl:result-document, so if the output naming functionality in XSLT Transformation Scenarios has improved, I'd love to find out about it.

Re: Custom Output File Name in XSLT Transformation Scenarios

Posted: Tue May 27, 2014 2:44 am
by Rumplestiltzkin
Whoops! Sorry; I misread the user join time as the message posting time. :oops: It's not been 5 years; only 1 or so, so the answer, I'm sure is "no."

Re: Custom Output File Name in XSLT Transformation Scenarios

Posted: Tue May 27, 2014 3:35 pm
by adrian
Hi,

Starting with v16.0 there is a new editor variable that can be used to evaluate an XPath directly in the Output/Save As field:

Code: Select all

${xpath_eval(expression)}
So, you can use an XPath expression to contribute to the output file path and name.
e.g.

Code: Select all

${cfd}/${xpath_eval(doc('personal.xml')/personnel[1]/person[1]/@id)}.xml
However, in Windows, due to a bug (present in v16.0 builds, up to 2014052022), you must escape the forward slash characters '/', to avoid the Windows path correction (replaces '/' with'\' and breaks the XPath expression).
So, for the example above, in order to work in Windows, the expression must look like this (replaced '/' with '&#x2f;'):

Code: Select all

${cfd}\${xpath_eval(doc('personal.xml')&#x2f;personnel[1]&#x2f;person[1]&#x2f;@id)}.xml
Regards,
Adrian

Re: Custom Output File Name in XSLT Transformation Scenarios

Posted: Wed May 28, 2014 2:12 am
by Rumplestiltzkin
Suite!!! :D :D :D