mirror of
https://github.com/alexandrev/xslt-lab.git
synced 2026-09-18 19:43:16 +00:00
53e90ef86e
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>
2.8 KiB
2.8 KiB
title, description, date, version, versionLabel, category, syntax, tags
| title | description | date | version | versionLabel | category | syntax | tags | ||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| unparsed-text-lines() | Reads a text file from a URI and returns its lines as a sequence of strings, one item per line. | 2026-04-18T00:00:00Z | 2.0 | XSLT 2.0 | node function | unparsed-text-lines(uri, encoding?) |
|
Description
unparsed-text-lines() retrieves a text resource and returns each line as a separate xs:string item in a sequence. Line endings (\r\n, \n, \r) are removed from each item. An empty trailing line produced by a final newline is not included.
This is more convenient than unparsed-text() followed by tokenize() when you want to iterate over lines directly.
This function is part of XPath 3.0 but is supported by Saxon 9.x+ with XSLT 2.0 stylesheets.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
uri |
xs:string? | Yes | The URI of the text resource. |
encoding |
xs:string | No | Character encoding (e.g., "UTF-8"). |
Return value
xs:string* — a sequence of strings, one per line, or the empty sequence if the URI argument is the empty sequence.
Examples
Parse a CSV file line by line
Stylesheet (reads data.csv):
<?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="/">
<table>
<xsl:for-each select="unparsed-text-lines('data.csv')[position() gt 1]">
<xsl:variable name="fields" select="tokenize(., ',')"/>
<row>
<id><xsl:value-of select="$fields[1]"/></id>
<name><xsl:value-of select="$fields[2]"/></name>
<value><xsl:value-of select="$fields[3]"/></value>
</row>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Output (for data.csv with header + 2 rows):
<table>
<row><id>1</id><name>alpha</name><value>100</value></row>
<row><id>2</id><name>beta</name><value>200</value></row>
</table>
Count non-empty lines in a file
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="text"/>
<xsl:template match="/">
<xsl:value-of select="count(unparsed-text-lines('readme.txt')[normalize-space()])"/>
</xsl:template>
</xsl:stylesheet>
Notes
- Line endings are stripped from each returned string.
- Unlike
tokenize(unparsed-text(...), '\n'), a trailing newline does not produce a spurious empty item. - Use
unparsed-text-available()first if the file may not exist. - Formally part of XPath 3.0; available in Saxon 9.x+ with XSLT 2.0 stylesheets.