爱猫的狗

拥抱变化

导航

XPath 入门

xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://MyXPathExample.com"
 xmlns:xs
="http://www.w3.org/2001/XMLSchema"
  xmlns:pb
="http://MyXPathExample.com"
   elementFormDefault
="qualified"> 
  
<xs:element name="list" type="pb:auctionItemList"/>

  
<xs:complexType name="auctionItemList">
    
<xs:sequence>
      
<xs:element name="item" type="pb:auctionItem" minOccurs="0" maxOccurs="unbounded"/>
    
xs:sequence>
  
xs:complexType>
  
<xs:complexType name="auctionItem">
    
<xs:sequence>
      
<xs:element name="bidIncrement" minOccurs="0" maxOccurs="1">
        
<xs:annotation>
          
<xs:documentation>
            Bid increment. Minimum difference between current price and new bid. Increment must be non-null integer value.
          
xs:documentation>
        
xs:annotation>
        
<xs:simpleType>
          
<xs:restriction base="xs:int">
            
<xs:minInclusive value="1"/>
          
xs:restriction>
        
xs:simpleType>
      
xs:element>
      
<xs:element name="currentPrice" type="pb:price" minOccurs="0" maxOccurs="1">
        
<xs:annotation>
          
<xs:documentation>
            Item's current price. The highest bid or the minimum asking price if bidding hasn't started.
          
xs:documentation>
        
xs:annotation>
      
xs:element>
      
<xs:element name="endOfAuction" minOccurs="0" maxOccurs="1">
        
<xs:annotation>
          
<xs:documentation>
            Minutes remaining to end of auction.
          
xs:documentation>
        
xs:annotation>
        
<xs:simpleType>
          
<xs:restriction base="xs:int">
            
<xs:minInclusive value="0"/>
          
xs:restriction>
        
xs:simpleType>
      
xs:element>
      
<xs:element name="description" type="xs:string">
        
<xs:annotation>
          
<xs:documentation>
            Text for the item's description.
          
xs:documentation>
        
xs:annotation>
      
xs:element>
      
<xs:element name="sellerId" type="xs:string" minOccurs="0" maxOccurs="1">
        
<xs:annotation>
          
<xs:documentation>
            Seller's user id.
          
xs:documentation>
        
xs:annotation>
      
xs:element>
    
xs:sequence>
    
<xs:attributeGroup ref="pb:itemAttributes"/>
  
xs:complexType>
  
<xs:simpleType name="tempPrice">
    
<xs:restriction base="xs:decimal">
      
<xs:fractionDigits value="2"/>
      
<xs:minExclusive value="0"/>
    
xs:restriction>
  
xs:simpleType>
  
<xs:complexType name="price">
    
<xs:annotation>
      
<xs:documentation>
        Price used on auction site. A positive decimal value with 2 decimal places which has a currency associated with it.
      
xs:documentation>
    
xs:annotation>
    
<xs:simpleContent>
      
<xs:extension base="pb:tempPrice">
        
<xs:attribute name="currency" type="pb:customCurrency" use="required"/>
      
xs:extension>
    
xs:simpleContent>
  
xs:complexType>
  
<xs:simpleType name="customCurrency">
    
<xs:annotation>
      
<xs:documentation>
        Currencies used on auction site.
      
xs:documentation>
    
xs:annotation>
    
<xs:restriction base="xs:string">
      
<xs:enumeration value="USD"/>
      
<xs:enumeration value="GBP"/>
      
<xs:enumeration value="EUR"/>
    
xs:restriction>
  
xs:simpleType>
  
<xs:attributeGroup name="itemAttributes">
    
<xs:attribute name="type" use="required">
      
<xs:annotation>
        
<xs:documentation>
          Type of listing used to sell item on auction site.
        
xs:documentation>
      
xs:annotation>
      
<xs:simpleType>
        
<xs:restriction base="xs:string">
          
<xs:enumeration value="Unknown"/>
          
<xs:enumeration value="Traditional"/>
          
<xs:enumeration value="BidOnly"/>
          
<xs:enumeration value="FixedPrice"/>
          
<xs:enumeration value="IndividualOffer"/>
        
xs:restriction>
      
xs:simpleType>
    
xs:attribute>
    
<xs:attribute name="id" type="xs:string"/>
    
<xs:attribute name="private" type="xs:boolean" default="false" use="optional"/>
  
xs:attributeGroup>
xs:schema>

AuctionItemList.xsd 包含针对拍卖物品和拍卖物品列表数据的业务规则,使用 XML Schema 语言进行描述:

      拍卖物品列表只有一个根元素,称为 list,是 auctionItemList 类型元素的列表。
      auctionItemList 由类型为 auctionItem 的一个或多个 item 元素组成。   
      auctionItem 由 5 个元素(bidIncrement、类型为 price 的 currentPrice、endOfAuction、description 和 sellerId)和一个类型为 itemAttributes 的属性组组成。
      price 是一个正十进制值,具有两位小数,必须把类型为 customCurrency 的 currency 属性与它关联。
      customCurrency 必须是 USD、GBP 或 EUR 之一。 
      itemAttributes 组必须包含一个字符串属性 type,一个字符串属性 id,以及一个布尔属性 private,默认情况下,其值是 false。
      type 属性必须是下面值之一:Unknown、Traditional、BidOnly、FixedPrice 或 IndividualOffer。

什么是XPath

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  
<xsl:template match="/">
     
<html>
       
<body>
         
<head><title>Auction Items Summary</title>
        
</head>
         
<xsl:apply-templates select="list"/>
       
</body>
     
</html>
  
</xsl:template>
  
  
<xsl:template match="list">
     
<h1>Current Auction Items</h1>
     
<table border="1">
       
<th>Time remaining</th>
       
<th>ID</th>
       
<th>Description</th>
       
<th>Seller</th>
       
<th>Current Price</th>
       
<th>Bid</th>
       
<th>Type</th>
       
<th>Private</th>
       
<xsl:apply-templates select="item"/>
     
</table>
   
</xsl:template>

   
<xsl:template match="item">
     
<tr>
      
<td><xsl:value-of select="endOfAuction"/></td>
      
<td><xsl:value-of select="@id"/></td>
      
<td><xsl:value-of select="description"/></td>
      
<td><xsl:value-of select="sellerId"/></td>
      
<td><xsl:value-of select="currentPrice/@currency"/><xsl:value-of select="currentPrice"/></td>
      
<td><xsl:value-of select="bidIncrement"/></td>
      
<td><xsl:value-of select="@type"/></td>
      
<td><xsl:value-of select="@private"/></td>
     
</tr>
   
</xsl:template>
   
</xsl:stylesheet>

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="AuctionItemSummary-Base.xsl"?>
<list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation
="http://MyXPathExample.com
  AuctionItemList.xsd"
>

  
<item type="BidOnly" private="false" id="itemId0001">
    
<bidIncrement>1</bidIncrement>
    
<currentPrice currency="USD">5</currentPrice>
    
<endOfAuction>56</endOfAuction>
    
<description>Miles Smiles album, CD</description>
    
<sellerId>miles1965</sellerId>
  
</item>
  
<item type="Unknown" private="false" id="itemId0002">
    
<bidIncrement>10</bidIncrement>
    
<currentPrice currency="EUR">2500</currentPrice>
    
<endOfAuction>15000</endOfAuction>
    
<description>Blue Train, Original Recording, LP record</description>
    
<sellerId>john1957</sellerId>
  
</item>
  
<item type="FixedPrice" private="true" id="itemId0302">
    
<bidIncrement>0</bidIncrement>
    
<currentPrice currency="GBP">70</currentPrice>
    
<endOfAuction>2</endOfAuction>
    
<description>New 256m MP3 player</description>
    
<sellerId>jimmy</sellerId>
  
</item>
  
<item type="IndividualOffer" private="false" id="itemId1202">
    
<currentPrice currency="EUR">0</currentPrice>
    
<endOfAuction>300</endOfAuction>
    
<description>2MP digital camera</description>
    
<sellerId>joe52</sellerId>
  
</item>
  
<item type="BidOnly" private="false" id="itemId0501">
    
<bidIncrement>2</bidIncrement>
    
<currentPrice currency="USD">20</currentPrice>
    
<endOfAuction>9</endOfAuction>
    
<description>NEW Pair of black Jean's</description>
    
<sellerId>vegas2003</sellerId>
  
</item>
  
<item type="BidOnly" private="false" id="itemId0991">
    
<bidIncrement>1</bidIncrement>
    
<currentPrice currency="USD">10</currentPrice>
    
<endOfAuction>1500</endOfAuction>
    
<description>NEW Pair of running shoes</description>
    
<sellerId>nick03</sellerId>
  
</item>
    
<item type="BidOnly" private="false" id="itemId0991">
    
<bidIncrement>0.5</bidIncrement>
    
<currentPrice currency="USD">3</currentPrice>
    
<endOfAuction>3500</endOfAuction>
    
<description>NEW Pair of running socks</description>
    
<sellerId>nick03</sellerId>
  
</item>
</list>


      XPath 表达式 /list/item 确定了所有 item 元素。当引用属性时,使用 @ 字符。XPath 也提供函数。
   
   XPath 把 XML 文档看作是一个节点树。节点可以有不同的类型,比如元素节点或者属性节点。元素节点代表 XML 文档中的每个元素。属性节点附属于元素节点,表示 XML 文档中的属性。但是,以 xmlns: 开始的属性在 XPath 中使用名称空间节点表示。其他类型的节点包括文本节点、处理指令节点和注释节点。
      在 XSLT 中,select 属性的值是 XPath 表达式。

      在 XSLT 中,上下文节点是当前正在计算的节点。对于一个 XML 文档,XSLT 模板可能被激活多次并生成不同的结果。在 AuctionItemList.xml 中,第一个和第二个模板(match="/"match ="list")被激活一次,而第三个模板(match="item")被激活七次(每个 item 元素激活一次)。第一次激活“item”模板时,上下文节点是 XML 文档中的第一个 item 元素("Miles Smiles album, CD"),比方说,<xsl:value-of select="@id"/> 的值就是 itemId0001。这个 XSLT 第二次被激活时,上下文节点就是第二个 item 元素("Coltrane Blue Train" CD),<xsl:value-of select="@id"/> 的值是 itemId0002。注意,如果在 select 属性中使用 /list/item/@id 代替 @idxsl-value-of 元素的值就会是空值。

什么是位置路径?      

      位置路径有相对绝对两种类型。
      相对位置路径由使用 / 分隔的定位步序列组成。比如:list/item[currentPrice<20.0]。这个位置路径由两个定位步组成:首先 list 选择和上下文节点有关的一组节点,然后 item[currentPrice<20.0] 在第一步所标识的子集中再选择一组节点;如果还有更多的节点也依此类推。
      绝对位置路径由 / 和后面可选的相对位置路径组成,其中 / 表示根节点。绝对位置路径基本上就是在根节点上下文中计算的相对位置路径,比如:
/list/item[currentPrice<20.0]。对于绝对位置路径(以 / 开始的位置路径),上下文节点是没有意义的,因为路径总是从根节点开始计算。

实用语法

      
@ 用于引用属性。比如,位置路径 @currency 标识了 currency 属性。list/item[@private] 标识带有 private 属性的 item 元素,意即 AuctionItemList.xml 中的所有 item 元素。
      * 用于引用上下文结点的所有子元素。@* 用于引用上下文结点的所有属性。
      [] 也可用于引用有序序列中的特定元素。比如,list/item[2] 代表第二个 item 元素。实际上 [] 是一个谓词。
      // 用于引用上下文节点的所有孩子。比如,//item 表示所有的 item 元素,而 //list/item 引用以 list 为父元素的所有 item 元素(在该例中即所有的 item 元素)。
      . 用于引用上下文节点自身。比如,. 选择上下文节点,而 .//item 代表作为上下文节点孩子的所有 item 元素。
      .. 用于引用上下文节点的父节点。比如,在第一个bidIncrement 元素的上下文中, ../item 就表示第一个 item 元素。

谓词

      
在位置路径中用于筛选当前节点集。谓词包含一个 boolean 表达式(或者很容易转化成 boolean 值的表达式)。用这个布尔表达式测试当前节点集的每个成员,如果表达式成立则保留该成员否则丢弃。谓词放在一对方括号([])中。比如下面的位置路径:list/item/currentPrice[@currency="EUR"]。在计算过程中,AuctionItemList.xml 中所有的 currentPrice 元素都放在选择的节点集中。然后计算谓词 @currency="EUR",货币中不包含值 EURcurrentPrice 元素被抛弃。
      谓词也可以使用关系运算符 ><>=<=!=
      假如要只包含美元为单位的产品,把<xsl:apply-templates select="item"/>改为<xsl:apply-templates select="item[currentPrice/@currency='USD']"/>

布尔表达式\数字\字符串-使用引号('")包围起来
      如果要选出1小时内拍卖的所有物品,把刚才的改为 <xsl:apply-templates select="item[endOfAuction div 60 &lt;1]"/>

函数库
http://www-900.ibm.com/developerWorks/cn/cnonlinetutorial.nsf/gethtml?OpenAgent&url=/developerWorks/cn/education/xml/x-xpath/tutorial/x-xpath-7-1.html

posted on 2005-04-24 15:48  anf  阅读(701)  评论(0)    收藏  举报