XPath中相对路径和绝对路径

XPath中相对路径和绝对路径
相对路径:即相对于上下文节点的路径
绝对路径:即从根目录开始的完整的路径
下面的xml和xslt写得相当好,初学者看一下
<?xml version = "1.0"?>
<?xml-stylesheet type="text/xsl" href="catalog_ui.xsl"?>

<Catalog>
 <Publisher isbn = "186100">
  <CorporateName>Wrox Press Ltd</CorporateName>
  <Address headquarters = "yes">
   <Street>Arden House</Street>
   <Street>1102 Warwick Road, Acocks Green</Street>
   <City>Birmingham</City>
   <Country>UK</Country>
   <PostalCode>B27 6BH</PostalCode>
  </Address>
  <Address headquarters = "no">
   <Street>Suite 520</Street>
   <Street>29 S. Lasalle Street</Street>
   <City>Chicago</City>
   <PoliticalDivision>IL</PoliticalDivision>
   <Country>USA</Country>
   <PostalCode>60603</PostalCode>
  </Address>
  <Imprints>
   <Imprint shortImprintName = "XML">XML and Scripting</Imprint>
   <Imprint shortImprintName ="Linux">GNU/Linux</Imprint>
   <Imprint shortImprintName  ="Java">Java</Imprint>
   <Imprint shortImprintName ="ASP">Active Server Pages</Imprint>
  </Imprints>
  <Author authorCiteID = "smohr">
   <FirstName>Stephen</FirstName>
   <LastName>Mohr</LastName>
   <Biographical>Stephen Mohr is a senior systems architect...</Biographical>
  </Author>
  <Author authorCiteID = "nozu">
   <FirstName>Nikola</FirstName>
   <LastName>Ozu</LastName>
   <Biographical>Nikola Ozu is a systems architect and consultant...</Biographical>
  </Author> 
  <Author authorCiteID = "jond">
   <FirstName>Jon</FirstName>
   <LastName>Duckett</LastName>
   <Biographical>The brains behind the Dr Evil organization, Jon is...</Biographical>
  </Author>
  <Author authorCiteID = "mbirbeck">
   <FirstName>Mark</FirstName>
   <LastName>Birbeck</LastName>
   <Biographical>Mark Birbeck has been a professional programmer for...</Biographical>
  </Author> 
  <Author authorCiteID = "mkay">
   <FirstName>Michael</FirstName>
   <LastName>Kay</LastName>
   <Biographical>Michael Kay has spent most of ...</Biographical>
  </Author> 
  <Author authorCiteID = "scottwoo">
   <FirstName>Scott</FirstName>
   <LastName>Woodgate</LastName>
   <Biographical>Scott Woodgate, a Microsoft Certified Solution Developer, ...</Biographical>
  </Author>       
 </Publisher>
 <Thread threadID = "coreXML">Core XML</Thread>
 <Thread threadID ="proXML">Advanced Topics in XML</Thread>
 <Thread threadID ="msXML">Microsoft XML Products</Thread>
 <Book ISBN = "1861005059" level = "pro" pubdate = "06-01-2001" pageCount = "800" authors = "smohr mbirbeck nozu jond" threads = "coreXML proXML" imprint = "XML">
  <Title>Professional XML, 2nd Edition</Title>
  <Abstract>An update to the wildly successful first edition, Pro XML covers the full ...</Abstract>
  <RecSubjCategories>
   <Category>Internet</Category>
   <Category>Internet Programming</Category>
   <Category>XML</Category>
  </RecSubjCategories>
  <Price currency = "USD">49.99</Price>
 </Book>
 <Book ISBN = "1861003129" level = "pro" pubdate = "07-2000" pageCount = "780" authors = "mkay" threads = "proXML" imprint = "XML">
  <Title>XSLT Programmer's Reference</Title>
  <Abstract>The definitive guide to XSLT and XSLT programming ...</Abstract>
  <RecSubjCategories>
   <Category>Internet</Category>
   <Category>XML</Category>
   <Category>XSL</Category>
  </RecSubjCategories>
  <Price currency = "USD">34.99</Price>
 </Book>
 <Book ISBN = "1861003293" level = "pro" pubdate = "12-2000" pageCount = "700" authors = "smohr scottwoo" threads = "proXML msXML" imprint = "XML">
  <Title>Professional BizTalk</Title>
  <Abstract>The first comprehensive guide to using and programming Microsoft BizTalk Server ...</Abstract>
  <RecSubjCategories>
   <Category>XML</Category>
   <Category>E-Commerce</Category>
  </RecSubjCategories>
  <Price currency = "USD">49.99</Price>
 </Book>   
</Catalog>

 

 


/*--------------*/
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" />

<xsl:template match="/">
   <xsl:element name="HTML">
      <xsl:element name="HEAD">
         <xsl:element name="TITLE">Book Catalog</xsl:element>
      </xsl:element>
      <xsl:element name="BODY">
         <xsl:apply-templates select="/Catalog/Book"/>
      </xsl:element>
   </xsl:element>
</xsl:template>

<xsl:template match="Book">
   <xsl:apply-templates select="Title"/>
   <xsl:variable name="isbn" select="@ISBN"/>
   <xsl:element name="DIV">
      <xsl:attribute name="style">font-family:Verdana;font-size:10pt;color:black;font-weight:bolder</xsl:attribute>
      <xsl:value-of select="/Catalog/Publisher[contains($isbn,@isbn)]/CorporateName"/>
   </xsl:element>
   <xsl:variable name="by" select="@authors"/>
   <xsl:call-template name="fetch-authors">
      <xsl:with-param name="authors" select="/Catalog/Publisher/Author[contains($by,@authorCiteID)]"/>
   </xsl:call-template>
  
   <xsl:element name="DIV">
      <xsl:attribute name="style">color:black;font-size:10pt;font-family:Verdana</xsl:attribute>
      <xsl:text>Price: </xsl:text><xsl:value-of select="Price"/><xsl:text> </xsl:text><xsl:value-of select="Price/@currency"/>
   </xsl:element>
  
   <xsl:apply-templates select="*[name() != 'Title']"/>
   <xsl:element name="P"/>
</xsl:template>

<xsl:template match="Title">
   <xsl:element name="DIV">
      <xsl:attribute name="style">background:teal;color:black;font-family:Verdana;font-size:14pt;font-weight:bolder</xsl:attribute>
      <xsl:value-of select="."/>
   </xsl:element>
</xsl:template>

<xsl:template match="Abstract">
   <xsl:element name="P"/>
   <xsl:element name="DIV">
      <xsl:attribute name="style">font-family:Verdana;font-size:12pt</xsl:attribute>
      <xsl:value-of select="."/>
   </xsl:element>
</xsl:template>

<xsl:template match="RecSubjCategories">
   <xsl:element name="P"/>
   <xsl:for-each select="Category">
      <xsl:element name="SPAN">
         <xsl:attribute name="style">font-family:verdana;font-size:8pt;background:black;color:white;width:3.5cm;text-align:center</xsl:attribute>
         <xsl:value-of select="."/>
      </xsl:element>
      <xsl:text> </xsl:text>
   </xsl:for-each>
</xsl:template>

<xsl:template match="text()"/>

<xsl:template name="fetch-authors">
   <xsl:param name="authors" select="anonymous"/>
   <xsl:variable name="first" select="$authors[1]"/>
   <xsl:variable name="rest" select="$authors[position()!=1]"/>
   <xsl:element name="DIV">
      <xsl:attribute name="style">font-family:Verdana;font-size:10pt;color:gray;</xsl:attribute>
      <xsl:value-of select="$first/FirstName"/><xsl:text> </xsl:text><xsl:value-of select="$first/LastName"/>
   </xsl:element>

   <xsl:if test="$rest">
      <xsl:call-template name="fetch-authors">
         <xsl:with-param name="authors" select="$rest"/>
      </xsl:call-template>
   </xsl:if>
</xsl:template>

</xsl:stylesheet>

转载于:https://www.cnblogs.com/ipointer/archive/2005/07/28/202254.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值