<xsl:call-template> 的示例

http://blog.163.com/jiamei_lx/blog/static/120972534201153103342725/

<xsl:call-template> 的示例  

2011-06-03 10:44:16|  分类: 学习资料|举报|字号 订阅

下面的示例使用 <xsl:call-template> 元素以模块化方式转换 XSLT。此示例使用三个主要文件:

  • XML 源文件 topic.xml。 此文件提供书籍出版物的主题。

  • 主要 XSLT 文件 topic.xsl。 此文件控制所显示的信息。

  • 调用的 XSLT 文件 ui.xsl。 此文件确定如何显示信息。

XML 文件 (topic.xml):

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="topic.xsl"?>
<topic name="My_topic"
       title="My Topic">
  <meta>
    <owner>
      <name>Jane</name>
      <email>jane@topicfactory.com</email>
      <since></since>
    </owner>
    <history>
      <created-by>
        <name>John</name>
        <email>john@topicfactory.com</email>
        <date>Nov 5, 2001</date>
      </created-by>
      <modifiers>
      </modifiers>
    </history>
    <keyword></keyword>
    <refs></refs>
  </meta
>

  <para name="para1title="First Paragraph">
    The first para has both name and title.
  </para>
  <para title="Second Paragraph">
     the second para has a title but no name.
  </para
>

  <para>
    Third para has neither name nor title.
  </para>
</topic
>

主要 XSLT 文件 (topic.xsl):

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

  <xsl:import href="ui.xsl"/>
  <xsl:param name="editable" select="true"/>

  <xsl:template match="/topic">
    <xsl:if test="@title">
      <xsl:call-template name="topic_title">
         <xsl:with-param name="editable" select="$editable"/>
         <xsl:with-param name="value" select="@title"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- Don't display meta information. -->
  <xsl:template match="meta"/>

  <xsl:template match="para">
    <P>
    <xsl:if test="@title">
      <xsl:call-template name="para_title">
         <xsl:with-param name="value" select="@title"/>
         <xsl:with-param name="editable" select="$editable"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:apply-templates/>
    </P>
  </xsl:template>
  
  <xsl:template match="text()">
    <xsl:call-template name="text">
      <xsl:with-param name="value">
        <xsl:value-of select="."/>
      </xsl:with-param>
      <xsl:with-param name="editable">true</xsl:with-param>
    </xsl:call-template>
  </xsl:template>

</xsl:stylesheet>

组件 XSLT 文件 (ui.xsl):

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

  <xsl:template name="topic_title">
    <xsl:param name="editable"/>
    <xsl:param name="value"/>
    <H2>
      <xsl:attribute name="CONTENTEDITABLE">
         <xsl:value-of select="$editable"/>
      </xsl:attribute>
      <xsl:value-of select="$value"/>
    </H2>
  </xsl:template>

  <xsl:template name="para_title">
    <xsl:param name="value"/>
    <xsl:param name="editable"/>
    <DIV STYLE="font-size:16;
                font-family:Arial;
                font-weight:bold;
                font-style:italic"
         CONTENTEDITABLE="{$editable}">
      <xsl:value-of select="$value"/>
    </DIV>
  </xsl:template>


  <xsl:template name="text">
    <xsl:param name="value"/>
    <xsl:param name="editable"/>
    <SPAN CONTENTEDITABLE="{$editable}">
      <xsl:value-of select="$value"/>
    </SPAN>
  </xsl:template>
</xsl:stylesheet>
输出:
以下是格式化输出:

My Topic

First Paragraph
The first para has both name and title. 

Second Paragraph
the second para has a title but no name. 

Third para has neither name nor title.
以下是处理器输出:

<H2 CONTENTEDITABLE="true">My Topic</H2>
<P>
   <DIV STYLE="font-size:16;
               font-family:Arial;
               font-weight:bold;
               font-style:italic"
        CONTENTEDITABLE="true">First Paragraph<DIV>
   <SPAN CONTENTEDITABLE="true">
     The first para has both name and title.
   </SPAN>
</P>
<P>
   <DIV STYLE="font-size:16;
               font-family:Arial;
               font-weight:bold;
               font-style:italic"
        CONTENTEDITABLE="true">Second Paragraph<DIV>
   <SPAN CONTENTEDITABLE="true">
     The second para has a title but no name.
   </SPAN>
</P>
<P>
   <SPAN CONTENTEDITABLE="true">
     The third para has neither name nor title.
   </SPAN>
</P>
备注:

主要 XSLT 文件 topic.xsl 控制所显示的信息。 它隐藏 <meta> 元素的内容并控制显示项的顺序。还调用组件 XSLT 文件 ui.xsl 中定义的模板规则。

ui.xsl 文件只包含可以从第一个 XSLT 文件中调用的命名模板规则。 每个命名模板规则的用法类似于正则函数,接受两个输入参数 $value 和 $editable 并生成 HTML 输出。 $value 参数传递要显示的文本;$editable 用于确定是否可以编辑输出文本(在使用 Internet Explorer 时)。 但是,与正则函数不同,命名模板规则中的输入参数顺序不必与调用模板规则中指定的顺序匹配。

注意,模板规则与源 XML 文档中定义的节点无关。 因此,ui.xsl 文件是如何编写可以从任何其他 XSLT 文件中调用的通用 UI 库的示例。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值