XSLT Unparsed-Text: Transform Your XML Efficiently 🎯

beginner
5 min

XSLT Unparsed-Text: Transform Your XML Efficiently 🎯

Welcome to our comprehensive guide on XSLT Unparsed-Text! In this lesson, we'll delve into transforming your XML documents using XSLT, focusing on the powerful Unparsed-Text feature. This feature is a game-changer when it comes to dealing with text nodes that aren't XML.

Let's start from the ground up.

What is XSLT? 📝

XSLT (XML Stylesheet Language Transformations) is a language for transforming XML documents into other formats such as HTML, XML, plain text, etc. It's a crucial tool for developers looking to manipulate and present XML data effectively.

Understanding Unparsed-Text 💡

The Unparsed-Text feature in XSLT allows you to include text nodes that aren't XML in your XSLT stylesheet. This can be incredibly useful when you need to include non-XML data like plain text, CSV, or even binary data, in your transformations.

How to Use Unparsed-Text 🎯

To use Unparsed-Text, you need to use the document() function and specify the unparsed-text() method. Here's a simple example:

xml
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <xsl:apply-templates select="document('data.txt')/unparsed-text()"/> </body> </html> </xsl:template> </xsl:stylesheet>

In this example, we're loading a text file data.txt and applying templates to its unparsed text content.

Real-world Application 📝

Unparsed-Text is particularly useful when you need to include external data in your XSLT transformations. For instance, you might have a CSV file containing data that needs to be processed and displayed in an XML document. By using Unparsed-Text, you can easily include this data in your stylesheet and transform it as needed.

Advanced Example 💡

Let's consider a scenario where we have a CSV file containing user data, and we want to transform this data into an HTML table within an XML document.

data.csv:

Name,Age,Email John Doe,30,john.doe@example.com Jane Smith,25,jane.smith@example.com

stylesheet.xsl:

xml
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <table> <thead> <tr> <th>Name</th> <th>Age</th> <th>Email</th> </tr> </thead> <xsl:apply-templates select="document('data.csv')/unparsed-text()/xsl:copy-of/node()"/> </table> </body> </html> </xsl:template> <xsl:template match="*"> <xsl:element name="{local-name()}"> <xsl:apply-templates select="node()"/> </xsl:element> </xsl:template> </xsl:stylesheet>

In this example, we're loading the CSV file and iterating over its nodes. We're using the xsl:copy-of instruction to copy each node and its contents into the HTML table.

Quiz 📝

Quick Quiz
Question 1 of 1

What does the `document()` function with `unparsed-text()` method do in XSLT?

By the end of this lesson, you'll have a solid understanding of XSLT Unparsed-Text and be able to apply this powerful feature to your own projects. Happy coding! ✅