Xsl转换的两种方式

 Template-Driven Transformations

<? xml version="1.0" ?>
< xsl:stylesheet  version ="1.0"  
                xmlns:xsl
="http://www.w3.org/1999/XSL/Transform" >
     
< xsl:template  match ="/" >
    
< HTML >
      
< HEAD >
      
</ HEAD >
      
< BODY >
         
< H1 >< xsl:value-of  select ="/books/book/title" /></ H1 >
         
< H3 >< xsl:value-of  select ="/books/book/author" /></ H3 >
         
< P >
           
< SPAN > Abstract: </ SPAN >
           
< SPAN >< xsl:value-of  select ="/books/book/abstract" /></ SPAN >
         
</ P >
      
</ BODY >  
    
</ HTML >
  
</ xsl:template >
</ xsl:stylesheet >
Here, the style sheet expects certain data elements from a source document, which might look like this:

<? xml version="1.0" ?>
< books >
   
< book >
      
< title > Synchronized Jamming </ title >
      
< author > Kari Hensien </ author >
      
< abstract >
         A post modern flight of fancy.
      
</ abstract >
   
</ book >
</ books >

 

Data-Driven Transformations

<? xml version="1.0" ?>
< xsl:stylesheet  version ="1.0"
      xmlns:xsl
="http://www.w3.org/1999/XSL/Transform" >
  
< xsl:template  match ="/" >
    
< HTML >
      
< HEAD >
      
</ HEAD >
      
< BODY >
         
< xsl:apply-templates  />
      
</ BODY >  
    
</ HTML >
  
</ xsl:template >

  
< xsl:template  match ="book-review" >
      
< P >< xsl:apply-templates  /></ P >
  
</ xsl:template >

  
< xsl:template  match ="title" >
      
< SPAN  style ="font-weight:bold" >< xsl:value-of  select ="." /></ SPAN >
  
</ xsl:template >

  
< xsl:template  match ="author" >
      
< SPAN  style ="font-style:italic" >< xsl:value-of  select ="." /></ SPAN >
  
</ xsl:template >
  
< xsl:template  match ="publisher" >
      
< SPAN  style ="color:blue" >< xsl:value-of  select ="." /></ SPAN >
  
</ xsl:template >
  
< xsl:template  match ="date" >
      
< SPAN  style ="font-family:courier" >< xsl:value-of  select ="." /></ SPAN >
  
</ xsl:template >

</ xsl:stylesheet >

Sample
用数据驱动,针对某些section进行处理

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="msxsl"
                xmlns:ms="urn:anything"
>
  <xsl:include href="CommonFunc.xslt"/>
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:template match="Document">
    <Document>
          <Title>
            <xsl:value-of select="Title"/>
          </Title>
          <Author>
            <xsl:for-each select="Authors/Author">
              <xsl:choose>
                <xsl:when test="position() &gt; 1 and position() != last()">
                  ,
                </xsl:when>
                <xsl:when test="position() &gt; 1 and position() = last()">
                  , and
                </xsl:when>
              </xsl:choose>
              <xsl:value-of select="ByLine"/>
            </xsl:for-each>
          </Author>
          <Date>
            <xsl:value-of select ="PublishDate/@PublishMonthNameAbbr" />&#160;<xsl:value-of select ="PublishDate/@PublishDayNumber" />,&#160;<xsl:value-of select ="PublishDate/@PublishFourDigitYear" />&#160;<xsl:value-of select ="PublishDate/@PublishHour" />:<xsl:value-of select ="PublishDate/@PublishMinute" />&#160;<xsl:value-of select ="PublishDate/@PublishAMPM" />
          </Date>
          <Body>
            <xsl:apply-templates select="content/docBody"/>
          </Body>
        </Document>
  </xsl:template>
  <xsl:template match="Ticker">(<xsl:value-of select="."/>)</xsl:template>
</xsl:stylesheet>

 

< xsl:stylesheet  xmlns:HTML ="http://www.w3.org/Profiles/XHTML-transitional"  xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"  version ="1.0" >
< xsl:output  omit-xml-declaration ="yes"  method ="html"  media-type ="text/html"  indent ="no"  encoding ="iso8859-1" />

< xsl:template  match ="/" >

< script  language ="Javascript" >< xsl:comment > <![CDATA[
......
<xsl:apply-templates select="/Fund/Content/Analysis/RoleInPortfolio" />
......
 
</xsl:template>
<xsl:template match="Fund/Content/Analysis/Take/Long">
  <xsl:apply-templates /> 
</xsl:template>
<xsl:template match="Fund/Content/Analysis/Strategy">
    <xsl:apply-templates />
</xsl:template>
<xsl:template match="Fund/Content/Analysis/Management">
    <xsl:apply-templates />
</xsl:template>
<xsl:template match="Fund/Content/Analysis/Kudos/Kudo">
    <xsl:apply-templates />
</xsl:template>
<xsl:template match="Fund/Content/Analysis/Concerns/Concern">
    <xsl:apply-templates />
</xsl:template>
<xsl:template match="Fund/Content/Analysis/RoleInPortfolio">
    <xsl:apply-templates />
</xsl:template>
<xsl:template match="Fund/Content/Analysis/Analyst/Bio">
    <xsl:apply-templates />
</xsl:template>
<xsl:template match="ticker">
  <a><xsl:attribute name="href"><xsl:choose>
    <xsl:when test="/Fund/Footer/HostServer[. != 'online.morningstar.com']">  
       http://quote.morningstar.com/Switch.html?ticker=<xsl:value-of select="."/>
    </xsl:when>
    <xsl:otherwise>
       /quote/Switch.html?ticker=<xsl:value-of select="."/>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:attribute><script>caseConvert('<xsl:value-of select="."/>')</script></a>
</xsl:template>
<xsl:template match="text()">
   <xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>
</xsl:stylesheet>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值