BENBUN Coder
BENBUN Coder

Reputation: 4881

XSLT inserting a TXT file

I am using XSLT to transform a XML file in to a HTML file.

I would like to insert a plain text file in to the HTML as part of the transform.

Is this possible?

Upvotes: 10

Views: 6131

Answers (3)

BENBUN Coder
BENBUN Coder

Reputation: 4881

Well the solution that I ended up using was somewhat simpler than those suggested above. I used the following :

<xsl:variable name="input-text" as="xs:string" select="unparsed-text('../Input/pipehat.txt', 'iso-8859-1')"/>

to get the text into a variable then just output the variable in the appropriate place.

<xsl:value-of select="$input-text"/>

Upvotes: 3

Robert Rossney
Robert Rossney

Reputation: 96830

In addition to the options Matthijs Bierman mentioned, you can also pass the contents of the text file into the transform as an argument, if the environment you're working with supports this.

For instance, in .NET, a program that performs a transform can read a file into a string variable, add it as a parameter to an XsltArgumentList object, and pass this object to the XslCompiledTransform's Transform method.

Upvotes: 0

Matthijs Bierman
Matthijs Bierman

Reputation: 1757

You have a few options:

  1. If you can surround the plain text in <text></text> tags, you may import it with the document() function. This works in XSLT1.0, and does not require another programming language.
  2. If you cannot modify the source file, but you do have XSLT2.0, then you may do it using unparsed-text().
  3. If you do not have XSLT2.0, but you are using Java, then you may be able to invoke a Java function. Microsoft platforms will probably offer similar functionality.

Upvotes: 8

Related Questions