Xsl转换的两种方式

来源:互联网 发布:脚本录制软件 编辑:程序博客网 时间:2024/06/05 02:21

 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>
原创粉丝点击