This example demonstrates how to use <xsl:apply-imports>
when you want to add to the functionality of a rule in an imported file, but you don't want to replace the rule entirely. This example uses three main files:
<xsl:apply-imports>
tag applies the matching rules in the imported sample-import style sheet.Note To test this example, you need to use a script. For more information, see Initiate XSLT in a Script.
XML File (books.xml)
Use the Sample XML File (books.xml).
Main XSLT File (sample.xsl)
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:import href="sample-import.xsl"/> <xsl:output method="html"/> <xsl:template match="book"> <font face="Arial"> <xsl:apply-imports/> </font> </xsl:template> </xsl:stylesheet>
Imported XSLT File (sample-import.xsl)
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- Override built-in template. --> <xsl:template match="text()"/> <xsl:template match="/"> <html> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="book"> <i> <xsl:apply-templates select="title"/> </i> <xsl:text> By: </xsl:text> <xsl:apply-templates select="author"/> <br/> </xsl:template> <xsl:template match="title"> <b> <xsl:value-of select="."/> </b> </xsl:template> <xsl:template match="author"> <font color="blue"> <xsl:value-of select="."/> </font> </xsl:template> </xsl:stylesheet>
Try It!
<?xml version="1.0"?>
, add the following line:
<?xml-stylesheet type="text/xsl" href="sample.xsl"?>
Output
This is the formatted output:
This is the processor output:
<html> <body> <font face="Arial"><i><b>XML Developer's Guide</b></i> By: <font color="blue">Gambardella, Matthew</font><br></font> <font face="Arial"><i><b>Midnight Rain</b></i> By: <font color="blue">Ralls, Kim</font><br></font> ... <font face="Arial"><i><b>Visual Studio 7: A Comprehensive Guide</b></i> By: <font color="blue">Galos, Mike</font><br></font> </body> </html>