How to Use Text Files as Primary Input for XSLT in Oxygen XML Editor
When working with Oxygen XML Editor, many users are accustomed to transforming XML or JSON documents using XSLT or XQuery. But what if you want to process a plain text file—perhaps a log, configuration, or other non-XML data—using XSLT? With Oxygen’s powerful transformation scenarios and XSLT 3.0’s new features, you can!
In this article, I’ll show you how you can use a text file as the primary input for an XSLT transformation inside Oxygen XML Editor, even if the file isn’t XML or JSON.
Background: Transformations in Oxygen XML Editor
Oxygen provides a robust set of features for transforming XML and JSON documents (XML Transformation with XSLT/XQuery, JSON Transformation with XSLT/XQuery). Users typically create transformation scenarios to automate these actions.
Traditionally, XSLT requires an XML or JSON input, but as of XSLT 3.0, you
can use the initial template feature. This allows you to define a
xsl:initial-template
. entry point for your transformation,
with no input document required. This feature opens up new
possibilities—including processing text files!
1. Create an XSLT using an Initial Template
Write an XSLT 3.0 stylesheet that declares an initial template with
xsl:initial-template
. This template will serve as the main
entry point.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- PARAMETERS • $input-uri – location of person.txt • $delim – field delimiter -->
<xsl:param name="input-uri" select="'person.txt'"/> <!-- default -->
<xsl:param name="delim" select="'\|'" /> <!-- “|” escaped for regex -->
<!-- result -->
<xsl:output method="xml" indent="yes"/>
<!-- entry point (initial template) -->
<xsl:template name="xsl:initial-template">
<persons>
<!-- read whole file, split it into separate lines -->
<xsl:variable name="lines"
select="tokenize(unparsed-text($input-uri), '\r?\n')" />
<!-- iterate over the lines, skipping empties -->
<xsl:for-each select="$lines[normalize-space()]">
<!-- tokenize the current line into its fields -->
<xsl:variable name="f" select="tokenize(., $delim)" />
<person>
<id> <xsl:value-of select="$f[1]"/></id>
<firstName> <xsl:value-of select="$f[2]"/></firstName>
<lastName> <xsl:value-of select="$f[3]"/></lastName>
<age> <xsl:value-of select="$f[4]"/></age>
<city> <xsl:value-of select="$f[5]"/></city>
</person>
</xsl:for-each>
</persons>
</xsl:template>
</xsl:stylesheet>
2. Define the Text File Location via Parameter
Instead of hardcoding the file path, make the text file’s location a parameter
(as shown above, $input-uri
).
id | firstName | lastName | age | city
001|John |Doe |29 |New York
002|Jane |Smith |34 |London
003|Paul |Adams |42 |Sydney
3. Create a Transformation Scenario
- Go to Transformation Scenarios in Oxygen.
- Create a new XSLT transformation scenario.
- Specify your XSLT as the stylesheet.
- In the parameters tab, add:
- Name:
input-uri
- Value:
${currentFileURL}
- Name:
The ${currentFileURL}
variable automatically passes the path of
your currently edited or selected file to the stylesheet.
4. Associate and Apply the Scenario
You can:
- Open the text file in the editor and press the Transform button in the Transformation view.
- Select multiple text files in the Project View, right-click, and run the scenario on all of them.
- Automate batch processing The scenario will automatically process each selected text file, regardless of format, as long as your stylesheet can handle it.
Now, your XSLT can process non-XML files using the full power of the XSLT 3.0 language!
- Convert plain text lists into XML
- Parse configuration logs
- Normalize or restructure CSV or tab-delimited files (with some tweaking)
- Batch transform multiple text documents from the Project view
- XSLT 3.0 Initial Template in Oxygen
- XML Transformation with XSLT/XQuery
- JSON Transformation with XSLT/XQuery
Conclusion
With Oxygen XML Editor and XSLT 3.0’s initial template mechanism, you’re no longer limited to XML or JSON data. Whether you’re transforming text logs, flattening CSV, or assembling custom data workflows, you can harness the flexibility of scenarios, parameters, and XSLT for all your file types.
Give it a try and unlock new transformation possibilities in Oxygen!