<xsl:param> 元素

<xsl:param> 元素

声明一个命名的参数,在 <xsl:stylesheet> 元素或 <xsl:template> 元素中使用。 允许指定默认值。

<xsl:param
  name = QName
  select = Expression
</xsl:param>

name

必需。 指定参数的限定名称 (XSLT)

select

属性值为表达式 (XSLT),变量值为计算该表达式得出的对象。 指定此属性后,<xsl:param> 元素必须是空的。

<xsl:param> 元素上指定的值是绑定的默认值。 如果调用的模板或样式表包含 <xsl:param>,传递的参数将替代默认值。

<xsl:param> 元素必须声明为 <xsl:template> 元素的直接子级。 如果未声明为直接子级,<xsl:param> 元素的值将无法访问,并将出错。 例如:

XML
<xsl:template name="getcount">
   <xsl:element name="strong">
      <xsl:param name="counted">
         <xsl:value-of select="count(//book)"/>
      </xsl:param>
      Total Book Count: <xsl:value-of select="$counted"/>
   </xsl:element>
</xsl:template>


在前一个示例中,<xsl:template> 元素的唯一直接子级是 <strong> 元素。 因此,分析器无法正确计算 <xsl:param> 元素的值,并产生以下错误:

说明 说明

此处不能使用关键字 xsl:param。

放置此元素以便能在 <xsl:template> 元素的上下文之内对它计算的正确方法如下:

<xsl:template name="getcount">
   <xsl:param name="counted">
      <xsl:value-of select="count(//book)"/>
   </xsl:param>
   <xsl:element name="strong">
      Total Book Count: <xsl:value-of select="$counted"/>
   </xsl:element>

参数值可以是任何表达式可以返回的类型的对象。 <xsl:param> 元素可以以三种备选方式指定变量值:

  • 如果元素具有 select 属性,属性值必须是表达式,参数值是计算该表达式得出的对象。 在这种情况下,元素的内容必须是空的。

  • 如果元素没有 select 属性,并且包含非空的内容,例如一个或多个子节点,内容将指定该值。 内容是通过实例化为参数指定值的模板。 值是结果树的一个片断,等效于只包含一个根节点、将通过实例化该模板生成的节点序列作为子节点的节点集。 该结果树片断中的节点的基 URI 是元素的基 URI。

    如果通过实例化该模板创建的节点序列中的某个成员是属性节点或命名空间节点,将出错,因为根节点不能将属性节点或命名空间节点作为子节点。

  • 如果内容是空的并且没有 select 属性,参数值将为空字符串。 因此

    <xsl:param name="x"/>
    

    等效于

    <xsl:param name="x" select="''"/>
    

    如果使用某个参数按位置选择节点,一定不要执行以下指令:

    <xsl:param name="n">2</xsl:param>
    ...
    <xsl:value-of select="item[$n]"/>
    

    这将输出第一项元素的值,因为变量“n”将被绑定到结果树的片段上,而不是数字上。 而应执行以下指令:

    <xsl:param name="n" select="2"/>
    ...
    <xsl:value-of select="item[$n]"/>
    

    <xsl:param name="n">2</xsl:param>
    ...
    <xsl:value-of select="item[number($n)]"/>
    

    通过以下方式可以方便地将空节点集指定为参数的默认值。 <xsl:param name="x" select="/.."/>

该示例使用参数定义“编号块”的命名模板来控制编号的格式。

XML 文件 (catmat.xml)

XML
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="paramelem.xsl"?>
<lists>
   <ol>
      <li>the</li>
      <li>cat</li>
      <ol>
         <li>sat</li>
         <li>on</li>
         <li>the</li>
      </ol>
      <li>mat</li>
   </ol>
</lists>


XSLT 文件 (paramelem.xsl)

XML
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="paramelem.xsl"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:template match="ol/li">
   <br/>
   <xsl:call-template name="numbered-block"/>
</xsl:template>

<xsl:template match="ol//ol/li">
   <br/>&#xA0;&#xA0;&#xA0;
   <xsl:call-template name="numbered-block">
      <xsl:with-param name="format">a. </xsl:with-param>
   </xsl:call-template>
</xsl:template>

<xsl:template name="numbered-block">
   <xsl:param name="format">1. </xsl:param>
   <fo:block>
      <xsl:number format="{$format}"/>
      <xsl:apply-templates/>
   </fo:block>
</xsl:template>

</xsl:stylesheet>


输出

以下是格式化输出:

1. the 2. cat     a. sat     b. on     c. the 3. mat

以下是处理器输出,为了清楚起见,增加了空白。

<?xml version="1.0" encoding="UTF-16"?>
<br xmlns:fo="http://www.w3.org/1999/XSL/Format" />
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">1. the</fo:block>
<br xmlns:fo="http://www.w3.org/1999/XSL/Format" />
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">2. cat</fo:block>
<br xmlns:fo="http://www.w3.org/1999/XSL/Format" />   

<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">a. sat</fo:block>
<br xmlns:fo="http://www.w3.org/1999/XSL/Format" />   

<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">b. on</fo:block>
<br xmlns:fo="http://www.w3.org/1999/XSL/Format" />   

<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">c. the</fo:block>
<br xmlns:fo="http://www.w3.org/1999/XSL/Format" />
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">3. mat</fo:block>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值