XPath1.0 和 XPath2.0 比較例子

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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章