1. 针对的XML
<?xml version="1.0" encoding="ISO-8859-1"?> <po:PurchaseOrder xmlns:po="http://www.marchal.com/2006/po"> <po:Buyer>Pineapplesoft<po:Buyer> <po:Seller>Bookstore<po:Seller> <po:OrderLines> <po:Line> <po:Code type="ISBN">0-7897-2504-5<po:Code> <po:Quantity>1<po:Quantity> <po:Description>XML by Example<po:Description> <po:Price>29.99<po:Price> </po:Line> <po:Line> <po:Code type="ISBN">0-672-32054-1</po:Code> <po:Quantity>2<po:Quantity> <po:Description>Applied XML Solutions<po:Description> <po:Price>44.99</po:Price> </po:Line> <po:Line> <po:Code type="ISBN">2-10-005763-4<po:Code> <po:Quantity>2<po:Quantity> <po:Description>Huit Solutions Concrètes avec XML et Java</po:Description> <po:Price>40.00<po:Price> <po:Line> <po:Line> <po:Quantity>1<po:Quantity> <po:Description>Internet Magazine<po:Description> <po:Price>3.10<po:Price> <po:Line> </po:OrderLines> <po:PurchaseOrder>
2. 2.0的用法(for语句; 返回序列可继续作为函数的参数)
for $line in /po:PurchaseOrder/po:OrderLines/po:Line return $line/po:Price * $line/po:Quantity如果要计算它们的和用:fn:sum(for $line in /po:PurchaseOrder/po:OrderLines/po:Line return $line/po:Price * $line/po:Quantity)3. 计算他们的和用1.0<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:po="http://www.marchal.com/2006/po" xmlns:exslt="http://exslt.org/common" version="1.0"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:variable name="lines"> <xsl:for-each select="/po:PurchaseOrder/po:OrderLines/po:Line"> <line-total><xsl:value-of select="po:Price * po:Quantity"/><line-total> <xsl:for-each> </xsl:variable> <xsl:value-of select="sum(exslt:node-set($lines)/line-total)"/> <xsl:template> </xsl:stylesheet>