How to read a txt file from disk and embed content using XSTL?

I have xslt and I need to read the file from disk. The file is a simple text file that I would like to read the full content and embed it in the html / pdf output file. Is it possible?

+3
source share
3 answers

Here's how to do it using the XSLT 2.0 feature unparsed-text():

D:\MiLu\Dev\XML :: more > eins.txt
Ich bin die eins.
^Z

D:\MiLu\Dev\XML :: more > zwei.txt
Ich bin die zwei.
^Z

D:\MiLu\Dev\XML :: saxon unparsed-text.xml unparsed-text.xsl
<?xml version="1.0" encoding="UTF-8"?>
<eins>
   <zwei> bla </zwei>
   <drei>Ich bin die eins.&#xD;
&#xD;
</drei>
   <vier>Ich bin die zwei.&#xD;
&#xD;
</vier>
</eins>

D:\MiLu\Dev\XML :: more /t1 unparsed-text.xml
<eins>
 <zwei> bla </zwei>
 <drei>
  <textfile href="eins.txt"/>
 </drei>
 <vier>
  <textfile href="zwei.txt"/>
 </vier>
</eins>

D:\MiLu\Dev\XML :: more /t1 unparsed-text.xsl
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:strip-space elements="*"/>
 <xsl:output method="xml" indent="yes"/>

 <xsl:template match="textfile[ @href ]">
  <xsl:copy-of select="unparsed-text( @href )"/>
 </xsl:template>

 <xsl:template match="@* | node()">
  <xsl:copy>
   <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
 </xsl:template>

</xsl:stylesheet>

For XSLT 1.0, you'll need a workaround using an XML wrapper file that references a text file using an external object and function document().

+3
source

Xslt . xsl. .NET . , / .

With built-in scripts / extension objects, you can use the IO class class libraries to open the file and return a line.

-2
source

All Articles