1
0
mirror of https://github.com/alexandrev/xslt-lab.git synced 2026-09-18 19:43:16 +00:00
Files
xslt-lab/site/content/xslt/functions/xpath-unparsed-text.md
T
alexandrev-tibco 53e90ef86e feat(blog): complete XSLT/XPath reference — 229 function pages
Full coverage of XSLT 1.0, 2.0 and 3.0 elements and XPath functions:
- 59 XSLT elements (xsl:stylesheet → xsl:use-accumulators)
- 170 XPath functions (1.0 node/string/numeric/boolean, 2.0 sequence/
  date/QName/string, 3.0 HOF/map/array/JSON/streaming)
Each page: description, parameters table, return value, 2 runnable
Saxon examples with input XML + stylesheet + output, notes, cross-links.
xsltCompletions.js: all 229 entries now have blogSlug for hover links.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 09:28:05 +02:00

3.0 KiB

title, description, date, version, versionLabel, category, syntax, tags
title description date version versionLabel category syntax tags
unparsed-text() Reads a plain text file from a URI and returns its contents as an xs:string, with optional encoding specification. 2026-04-18T00:00:00Z 2.0 XSLT 2.0 node function unparsed-text(uri, encoding?)
xslt
reference
xpath
xslt2

Description

unparsed-text() retrieves the contents of a text resource at the given URI and returns it as a single xs:string. The resource is treated as plain text — no XML parsing is performed. This is useful for reading CSV files, plain-text templates, or any non-XML data that must be processed within an XSLT transformation.

If the URI argument is the empty sequence, the empty sequence is returned. The function raises an error if the resource cannot be retrieved or decoded.

Parameters

Parameter Type Required Description
uri xs:string? Yes The URI of the text resource. Relative URIs are resolved against the static base URI.
encoding xs:string No The character encoding to use (e.g., "UTF-8", "ISO-8859-1"). If omitted, the encoding is determined from the resource's BOM or defaults to UTF-8.

Return value

xs:string? — the text content of the resource as a single string, or the empty sequence if the URI argument is the empty sequence.

Examples

Read a CSV file and split into rows

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <xsl:variable name="csv" select="unparsed-text('data.csv')"/>
    <rows>
      <xsl:for-each select="tokenize($csv, '\n')[normalize-space()]">
        <row><xsl:value-of select="."/></row>
      </xsl:for-each>
    </rows>
  </xsl:template>
</xsl:stylesheet>

Output (for a data.csv with three lines):

<rows>
  <row>id,name,value</row>
  <row>1,alpha,100</row>
  <row>2,beta,200</row>
</rows>

Embed a text file as a CDATA section

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" cdata-section-elements="content"/>

  <xsl:template match="/">
    <document>
      <content><xsl:value-of select="unparsed-text('readme.txt', 'UTF-8')"/></content>
    </document>
  </xsl:template>
</xsl:stylesheet>

Notes

  • Use unparsed-text-available() to guard against missing files without raising an error.
  • For processing line by line without constructing the full string first, use unparsed-text-lines().
  • Relative URIs are resolved against the static base URI of the stylesheet, not the source document.
  • Line endings in the returned string are normalised to &#10; by the processor.

See also