XSLT - Document Function (document()) for Multi-File Transformations in XSLT
The document() function in XSLT is used to access and process XML data from external files during a transformation. Normally, an XSLT stylesheet processes a single input XML document. However, in many real-world scenarios, data may be distributed across multiple XML files. The document() function allows the stylesheet to retrieve and include information from other XML documents while performing the transformation.
The basic syntax of the function is:
document(uri)
or
document(uri, node)
The uri parameter represents the location of the external XML file. It can be a relative path or an absolute URL. The optional node parameter specifies the base node used to resolve relative paths. When the function is executed, XSLT loads the specified XML document and makes it available as a node set so that XPath expressions can be applied to it just like the main input document.
One of the most common uses of the document() function is combining data from multiple XML sources. For example, suppose one XML file contains product information and another XML file contains pricing details. The stylesheet can read the main XML file and use the document() function to retrieve the pricing information from another file, then merge both sets of data into a single output document such as HTML or another XML structure.
Example scenario:
products.xml
<products>
<product id="101" name="Laptop"/>
<product id="102" name="Phone"/>
</products>
prices.xml
<prices>
<price id="101">800</price>
<price id="102">400</price>
</prices>
XSLT example
<xsl:variable name="priceDoc" select="document('prices.xml')"/>
<xsl:for-each select="products/product">
<xsl:value-of select="@name"/>
<xsl:text> - </xsl:text>
<xsl:value-of select="$priceDoc/prices/price[@id=current()/@id]"/>
</xsl:for-each>
In this transformation, the stylesheet reads products.xml as the main input file. Using document('prices.xml'), it loads the second XML file. The transformation then retrieves the matching price for each product by comparing the product ID.
Another important use of the document() function is referencing configuration or lookup XML files. Large applications often maintain lookup tables in separate XML files, such as lists of countries, codes, or metadata. Instead of embedding this information directly in the main XML document, XSLT can dynamically retrieve it using the document() function. This makes the system more flexible because changes to the lookup data do not require modifying the primary XML file.
The document() function also supports multiple documents at once. If the URI expression returns several document paths, XSLT can load and process all of them together. This feature is useful when transformations must aggregate data from multiple sources, such as merging records from several XML feeds.
While the function is powerful, developers should use it carefully because loading external documents may increase processing time. When working with large XML files or many external references, performance optimization techniques such as caching or using keys may be necessary.
In summary, the document() function plays a crucial role in advanced XSLT transformations. It allows a stylesheet to access external XML files, combine multiple data sources, perform lookups, and build richer output documents. This capability makes XSLT suitable for complex XML processing tasks where information is distributed across several documents rather than stored in a single file.