Page 1 of 1

Can I use text file as primary input of XSLT?

Posted: Mon Jan 08, 2018 7:40 pm
by serioadamo97
I want to use a text file as primary input.
For example:
In XSLT debugger perspective view, select XML: mytext.txt, select XSLT: myT2X.xsl.
myT2X.xsl is :

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">

<xsl:template match="/text()">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
When I run the debugger, the following error occured:
E Content is not allowed in prolog.
F Content is not allowed in prolog.
F Content is not allowed in prolog.

I can use unparsed-text() to read .txt in stylesheet, but how to use txt file as primary input directly?
Thanks in advance.

Re: Can I use text file as primary input of XSLT?

Posted: Tue Jan 09, 2018 9:28 am
by Radu
Hi,

XSLT needs to be applied over XML content, even if inside it reads a separate text file and uses only that text file contents.

In XSLT 3.0 they added a feature called "initial template". You specify in the transformation scenario that a certain named template is your initial template and you no longer need to provide an input XML to the XSLT:

https://www.oxygenxml.com/doc/versions/ ... hing2.html

Regards,
Radu

Re: Can I use text file as primary input of XSLT?

Posted: Tue Jan 09, 2018 10:18 am
by serioadamo97
Radu wrote:Hi,

XSLT needs to be applied over XML content, even if inside it reads a separate text file and uses only that text file contents.

In XSLT 3.0 they added a feature called "initial template". You specify in the transformation scenario that a certain named template is your initial template and you no longer need to provide an input XML to the XSLT:
Thanks Radu.
But initial template still make stylesheet(eg: myT2X.xsl) depends on specific input file name (eg: myText.txt).
Is there another way to workround? (make a pure text file as primary input)
Because pure text is a legal XDM tree model (although it is not well-formed xml).

Re: Can I use text file as primary input of XSLT?

Posted: Tue Jan 09, 2018 10:31 am
by Radu
Hi,

In my opinion what you want is not possible.
If you want there is a Saxon XSLT processor users list:

https://sourceforge.net/p/saxon/mailman/saxon-help/

where you can register and ask this. But as far as I know the processor will not receive text content as input.

What you could probably do would be to use an xsl:param to pass the path to the text file to the XSLT stylesheet, so that the stylesheet is not hardcoded looking at a certain text file.

Regards,
Radu

Re: Can I use text file as primary input of XSLT?

Posted: Wed Jan 15, 2020 5:00 pm
by relume
at: serioadamo97

I would like to ask if you found a solution to get a plain text file as primary input file for XSLT transformation (also in Oxygen)?

Many thanks in advance and best regards,

Re: Can I use text file as primary input of XSLT?

Posted: Thu Jan 16, 2020 12:41 pm
by Radu
Hi,

There is no way to do this with XSLT, the XSLT templates are applied directly on XML elements and you can only load other files using the "unparsed-text" function:

https://stackoverflow.com/questions/159 ... -with-xslt

Regards,
Radu

Re: Can I use text file as primary input of XSLT?

Posted: Wed Jan 22, 2020 8:17 pm
by relume
Hello Radu

Many thanks for your hints. Practically for oXygen there is needed an source/input XML document. This can be simple as:

Code: Select all

<text2xml></text2xml>
and the XSLT template starts with:

Code: Select all

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vText" select=
   "unparsed-text('new.txt')"/>

 <xsl:variable name="vLines" select=
  "tokenize($vText, '&#xD;?&#xA;')[normalize-space()]"/>

<xsl:template match="/">
   <xsl:for-each select="$vLines">
   ...

best regards,

André

Re: Can I use text file as primary input of XSLT?

Posted: Thu Jan 23, 2020 10:26 am
by Radu
Hi André,

Thanks for posting the sample XSLT content. This limitation of having an XML input is not imposed implicitly by Oxygen but by the XSLT standard.
In such cases you can use as XML source even the XSLT stylesheet if you do not want to have a blank XML document lying around on the hard drive.

Regards,
Radu