XSLT专业站 - 提供xslt和xml相关的资料书籍和教程  
XSLT专业站 - 提供xslt和xml相关的资料书籍和教程
网站地图  收藏本站
首页 | 热门文章 | 精彩实例 | 经典教程 | XSLT语法详解 | 资料下载 | 休闲天地 | 交流论坛
  当前位置:首页>精彩实例>文章内容
XSLT 实例:创建带有“前一页”和“后一页”的 HTML 文档
来源:XSLT.org.cn 作者:XSLT专业站 发布时间:2007-10-27  
XSLT 实例:创建带有“前一页”和“后一页”的 HTML 文档
    利用XML文档创建HTML的链接是一件重复的事情,因此,最好能有一种好维护的方式产生我们所需要的链接。特别是内部链接,链接和链接的目标地址都是在同一个XML文档中产生,如果需要手工维护将是非常麻烦的。本例中,所以的链接和锚点(anchor)都是采用同样的代码产生,这样生成的链接将不会出现错误。
    本例中,采用的是模板(template)进行实现, 最后的5个模板可以通过import方式给其他的XSLT转换文件使用。通过将各个功能进行划分,使它们的逻辑相对独立,这样我们导入模板或者是重写相关模板来生成不同的链接方式。
    在本例的XML文档中,id属性是值唯一的字符串,可以用作锚点,唯一麻烦的是id的值中包含空格,也可能会有#号,这些字符是不能用作锚点的,因此我们可以转换这些字符为下划线(_)。这些模板可以用来生成链接和锚点,我们可以放心使用,不会出现坏链接。

本例使用到的XSLT元素:   xsl:stylesheet  xsl:template  xsl:apply-templates  xsl:for-each  xsl:value-of   xsl:call-template xsl:copy-of  xsl:param  xsl:attribute  xsl:text  xsl:output
本例使用到的XSLT/XPath函数: translate

XML源文件:
<?xml version="1.0"?>
<book id="book oos" title="The origin of species">
  <part id="oos part 1" title="Opening chapters">
    <chapter id="oos ch 1" title="An historical sketch">
      <par id="par 1">  text text text </par> 
      <par id="par 2">  text text text text text </par> 
      <par id="par 3">  text text text text text text text text text </par> 
    </chapter> 
    <chapter id="oos ch 2" title="Introduction">
      <par id="par 4">  text text text text text </par> 
      <par id="par 4">  <b>text text</b> text text text </par> 
      <par id="par 5">  text text text text text </par> 
      <par id="par 6">  text text text text text </par> 
    </chapter> 
  </part> 
  <part id="oos part 2" title="Later chapters">
    <chapter id="oos ch 3" title="Variation under domestication">
      <par id="par 7">  text text text text text </par> 
      <par id="par 8">  text text text text text </par> 
    </chapter> 
    <chapter id="oos ch 4" title="Variation under nature">
      <par id="par 9">  text text text text text </par> 
    </chapter> 
    <chapter id="oos ch 5" title="Struggle for existence">
      <par id="par 10">  text text text text text </par> 
      <par id="par 11">  text text text text text </par> 
      <par id="par 12">  text text text text text </par> 
    </chapter> 
    <chapter id="oos ch 6" title="Natural selection">
      <par id="par 13">  text text text text text </par> 
    </chapter> 
    <chapter id="oos ch 7" title="Laws of variation">
      <par id="par 14">  text text text text text </par> 
    </chapter> 
  </part>
</book>

XSLT源文件:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml"/>

  <xsl:template match="/book">
    <html><body>
      <xsl:call-template name="anchor-tag-for"/>
      <h1><xsl:value-of select="@title"/></h1>
      <xsl:apply-templates select="part"/>
    </body></html>
  </xsl:template>

  <xsl:template match="part">
    <xsl:call-template name="anchor-tag-for"/>
    <h2><xsl:value-of select="@title"/></h2>
    <xsl:apply-templates select="chapter"/>
  </xsl:template>

  <xsl:template match="chapter">
    <xsl:for-each select="preceding-sibling::chapter[1]">
      Prev: <xsl:call-template name="link-to"/><br/>
    </xsl:for-each>
    <xsl:for-each select="following-sibling::chapter[1]">
      Next: <xsl:call-template name="link-to"/><br/>
    </xsl:for-each>
    <xsl:call-template name="anchor-tag-for"/>
    <h3><xsl:value-of select="@title"/></h3>
    <xsl:apply-templates select="par"/>
  </xsl:template>

  <xsl:template match="par">
    <p>
      <xsl:call-template name="anchor-tag-for"/>
      <xsl:copy-of select="node()"/>
    </p>
  </xsl:template>

  <!-- It is important to have a way to reference to a specific location in the
       source document with a string value. This can be used to create links to
       a specific location in the content and much more. In this case, every
       node we want to use as a link has a unique attribute called id. -->
  <xsl:template name="node-identifier">
    <xsl:param name="id-node" select="self::*"/>
    <xsl:value-of select="translate($id-node/@id, ' #', '__')"/>
  </xsl:template>

  <!-- Create an HTML link to any section in the document. A link should always
       be the same, both from the index or from another chapter. We locate the
       logic of link-creation in this template. It relies on other templates for
       determining the file location and the need for an anchor string (trailing #blabla) -->
  <xsl:template name="link-to">
    <xsl:param name="linktext" select="@title"/>
    <a>
    <xsl:attribute name="href">
      <!-- first the filename -->
      <xsl:call-template name="file-of"/>
      <xsl:text>#</xsl:text>
      <xsl:call-template name="anchor-of"/>
    </xsl:attribute>
    <xsl:value-of select="$linktext"/>
    </a>
  </xsl:template>

  <!-- For creating links, one must know the filename. Calculating the filename
       is isolated in this template. In later versions, when we distribute the
       content over many files, we will overrule this template with much more
       interesting versions. -->
  <xsl:template name="file-of">
    <xsl:text>doc.htm</xsl:text>
  </xsl:template>
 
  <!-- Here we decide if we need a trailing hash-string to identify the exact
    spot in a page where to find a certain piece of information. For now we have
    only one file, so we'll always need this extra info. -->
  <xsl:template name="anchor-of">
     <xsl:call-template name="node-identifier"/>
  </xsl:template>

  <xsl:template name="anchor-tag-for">
    <xsl:param name="anchor-node" select="self::*"/>
    <xsl:for-each select="$anchor-node">
      <a>
        <xsl:attribute name="name">
          <xsl:call-template name="anchor-of"/>
        </xsl:attribute>
      </a>
    </xsl:for-each>
  </xsl:template>
 

</xsl:stylesheet>

最后输出结果:
<?xml version="1.0" encoding="utf-8"?><html><body><a name="book_oos"/><h1>The origin of species</h1><a name="oos_part_1"/><h2>Opening chapters</h2>
      Next: <a href="doc.htm#oos_ch_2">Introduction</a><br/><a name="oos_ch_1"/><h3>An historical sketch</h3><p><a name="par_1"/>
         text text text
      </p><p><a name="par_2"/>
         text text text text text
      </p><p><a name="par_3"/>
         text text text text text text text text text
      </p>
      Prev: <a href="doc.htm#oos_ch_1">An historical sketch</a><br/><a name="oos_ch_2"/><h3>Introduction</h3><p><a name="par_4"/>
         text text text text text
      </p><p><a name="par_4"/>
         <b>text text</b> text text text
      </p><p><a name="par_5"/>
         text text text text text
      </p><p><a name="par_6"/>
         text text text text text
      </p><a name="oos_part_2"/><h2>Later chapters</h2>
      Next: <a href="doc.htm#oos_ch_4">Variation under nature</a><br/><a name="oos_ch_3"/><h3>Variation under domestication</h3><p><a name="par_7"/>
         text text text text text
      </p><p><a name="par_8"/>
         text text text text text
      </p>
      Prev: <a href="doc.htm#oos_ch_3">Variation under domestication</a><br/>
      Next: <a href="doc.htm#oos_ch_5">Struggle for existence</a><br/><a name="oos_ch_4"/><h3>Variation under nature</h3><p><a name="par_9"/>
         text text text text text
      </p>
      Prev: <a href="doc.htm#oos_ch_4">Variation under nature</a><br/>
      Next: <a href="doc.htm#oos_ch_6">Natural selection</a><br/><a name="oos_ch_5"/><h3>Struggle for existence</h3><p><a name="par_10"/>
         text text text text text
      </p><p><a name="par_11"/>
         text text text text text
      </p><p><a name="par_12"/>
         text text text text text
      </p>
      Prev: <a href="doc.htm#oos_ch_5">Struggle for existence</a><br/>
      Next: <a href="doc.htm#oos_ch_7">Laws of variation</a><br/><a name="oos_ch_6"/><h3>Natural selection</h3><p><a name="par_13"/>
         text text text text text
      </p>
      Prev: <a href="doc.htm#oos_ch_6">Natural selection</a><br/><a name="oos_ch_7"/><h3>Laws of variation</h3><p><a name="par_14"/>
         text text text text text
      </p></body></html>


标题: XSLT 实例:创建带有“前一页”和“后一页”的 HTML 文档
关键字:xsl:stylesheet xsl:template xsl:apply-templates xsl:for-e
上一篇:XSLT 实例:统计作者出版物的销量   下一篇:XSLT 实例:使用变量(Variable)创建单选框和复选框
  精彩实例热点文章
·XSLT 实例:使用不同的轴(Axis
·查找公共祖先节点
·利用 xslt 对 xml 进行缩进格式
·利用 XSL 解析 RSS
·把数据库输表用 XSLT 转化为树状
·查找节点的父节点
·用 XML/XSL 来生成动态页面
·利用 XSL 对 XML 数据进行加密和
·XSLT 实例:统计作者出版物的销
·XSLT 实例:使用变量(Variable)
  精彩实例相关文章
·XSLT 实例:统计作者出版物的销
·XSLT 实例:使用变量(Variable)
·XSLT 实例:使用不同的轴(Axis
·利用 XSL 对 XML 数据进行加密和
·利用 xslt 对 xml 进行缩进格式
·用 XML/XSL 来生成动态页面
·查找公共祖先节点
·查找节点的父节点
·把数据库输表用 XSLT 转化为树状
·利用 XSL 解析 RSS
Copyright© 2007 xslt.org.cn All rights reserved.