Can I use text file as primary input of XSLT?

Having trouble installing Oxygen? Got a bug to report? Post it all here.
serioadamo97
Posts: 22
Joined: Tue Oct 17, 2017 5:05 pm

Can I use text file as primary input of XSLT?

Post 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.
Radu
Posts: 9018
Joined: Fri Jul 09, 2004 5:18 pm

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

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
serioadamo97
Posts: 22
Joined: Tue Oct 17, 2017 5:05 pm

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

Post 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).
Radu
Posts: 9018
Joined: Fri Jul 09, 2004 5:18 pm

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

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
relume
Posts: 6
Joined: Wed Aug 09, 2006 1:03 pm

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

Post 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,
Radu
Posts: 9018
Joined: Fri Jul 09, 2004 5:18 pm

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

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
relume
Posts: 6
Joined: Wed Aug 09, 2006 1:03 pm

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

Post 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é
Radu
Posts: 9018
Joined: Fri Jul 09, 2004 5:18 pm

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

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply