This example inserts the text "International Stock
" when the <stock>
element has an attribute named international
.
Note To test this example, you need to use a script. For more information, see Initiate XSLT in a Script.
XML File (stock.xml)
<?xml version='1.0'?> <?xml-stylesheet type="text/xsl" href="ifstock.xsl" ?> <stocks> <stock international="yes">Microsoft</stock> <stock>Wingtip Toys</stock> <stock international="yes">Contoso Pharmaceuticals</stock> <stock>Contoso, Ltd</stock> <stock international="yes">Fabrikam, Inc.</stock> </stocks>
XSLT File (ifstock.xsl)
<?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:template match="/"> <html><body> <xsl:apply-templates/> </body></html> </xsl:template> <xsl:template match="stock"> <p/> <xsl:if test="@international">International Stock </xsl:if> <xsl:apply-templates /> </xsl:template> </xsl:stylesheet>
Output
This is the formatted output:
International Stock Microsoft
Wingtip Toys
International Stock Contoso Pharmaceuticals
Contoso, Ltd
International Stock Fabrikam, Inc.
The following is the processor utput, with line breaks added for clarity.
<html><body> <p></p>International Stock Microsoft <p></p>Wingtip Toys <p> ... </p>International Stock Fabrikam, Inc. </body></html>
Example 1 of <xsl:if> | Example 2 of <xsl:if> | Example 4 of <xsl:if>