<xsl:param> 元素

本文介绍了XSLT中的&lt;xsl:param&gt;元素的使用方法及其特性,包括如何声明参数、设置默认值以及在模板中应用这些参数。通过一个具体的示例展示了如何利用参数来控制输出格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<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>
<xsl:template match="w:tc"><fo:table-cell border="1pt solid black"padding="2pt"display-align="center"><xsl:variable name="totalCols"select="count(ancestor::w:tbl/w:tblGrid/w:gridCol)"/><xsl:variable name="precedingSpanSum"><xsl:choose><xsl:when test="preceding-sibling::w:tc"><xsl:value-of select="sum(preceding-sibling::w:tc/w:tcPr/w:gridSpan/@w:val) + count(preceding-sibling::w:tc[not(w:tcPr/w:gridSpan)])"/></xsl:when><xsl:otherwise>0</xsl:otherwise></xsl:choose></xsl:variable><xsl:if test="w:tcPr/w:gridSpan"><xsl:variable name="declaredSpan"select="w:tcPr/w:gridSpan/@w:val"/><xsl:variable name="remainingCols"select="$totalCols - $precedingSpanSum"/><xsl:attribute name="number-columns-spanned"><xsl:choose><xsl:when test="$declaredSpan > $remainingCols"><xsl:value-of select="$remainingCols"/></xsl:when><xsl:otherwise><xsl:value-of select="$declaredSpan"/></xsl:otherwise></xsl:choose></xsl:attribute></xsl:if><!--修正的行合并逻辑--><xsl:if test="w:tcPr/w:vMerge[@w:val='restart']"><xsl:variable name="currentPos"select="count(preceding-sibling::w:tc) + 1"/><xsl:variable name="rowSpan"><xsl:call-template name="calculateRowSpan"><!--修正点1:直接取后续所有行--><xsl:with-param name="remainingRows"select="../following-sibling::w:tr"/><xsl:with-param name="position"select="$currentPos"/><xsl:with-param name="count"select="1"/></xsl:call-template></xsl:variable><xsl:attribute name="number-rows-spanned"><xsl:value-of select="$rowSpan"/></xsl:attribute></xsl:if><fo:block linefeed-treatment="ignore"white-space-collapse="true"><xsl:apply-templates select=".//w:p"/></fo:block></fo:table-cell></xsl:template><!--新增递归模板--><xsl:template name="calculateRowSpan"><xsl:param name="remainingRows"/><xsl:param name="position"/><xsl:param name="count"/><xsl:choose><!--修正点2:增加空行判断--><xsl:when test="count($remainingRows) = 0"><xsl:value-of select="$count"/></xsl:when><xsl:otherwise><xsl:variable name="currentRow"select="$remainingRows[1]"/><xsl:variable name="targetCell"select="$currentRow/w:tc[$position]"/><!--修正点3:处理单元格位置偏移--><xsl:variable name="precedingSpan"select="sum($currentRow/w:tc[position() < $position]/w:tcPr/w:gridSpan/@w:val)"/><xsl:choose><!--修正点4:精准定位合并目标单元格--><xsl:when test="$targetCell/w:tcPr/w:vMerge[@w:val='continue'] and ($position - $precedingSpan) <= count($currentRow/w:tc)"><xsl:call-template name="calculateRowSpan"><xsl:with-param name="remainingRows"select="$remainingRows[position() > 1]"/><xsl:with-param name="position"select="$position"/><xsl:with-param name="count"select="$count + 1"/></xsl:call-template></xsl:when><xsl:otherwise><xsl:value-of select="$count"/></xsl:otherwise></xsl:choose></xsl:otherwise></xsl:choose></xsl:template>修改这段样式表,使表格的行合并单元格生效,一定要注意这是word的xml转xls-fo的xlst样式表,且xslxml版本均为1.0,请直接给出修改结果
最新发布
03-12
<xsl:template match="w:tbl"> <xsl:variable name="colCount" select="count(w:tblGrid/w:gridCol)"/> <fo:table table-layout="fixed" width="100%" border-collapse="collapse"> <!-- 生成精确列定义 --> <xsl:for-each select="w:tblGrid/w:gridCol"> <fo:table-column column-width="{@w:w div 20}pt"/> </xsl:for-each> <fo:table-body> <!-- 列溢出检测逻辑保持不变 --> <xsl:if test="count(w:tr[1]/w:tc) > $colCount"> <fo:table-row> <fo:table-cell number-columns-spanned="{$colCount}"> <fo:block color="red">表格列数溢出警告</fo:block> </fo:table-cell> </fo:table-row> </xsl:if> <xsl:apply-templates select="w:tr"/> </fo:table-body> </fo:table> </xsl:template> <xsl:template match="w:tr"> <tr> <xsl:apply-templates select="w:tc"/> </tr> </xsl:template> <xsl:template match="w:tc"> <xsl:variable name="pos" select="count(preceding-sibling::w:tc) + 1"/> <xsl:variable name="vMergeStart" select="w:tcPr/w:vMerge/@w:val = 'restart' or (w:tcPr/w:vMerge and not(@w:val))"/> <xsl:variable name="rowspan"> <xsl:choose> <xsl:when test="$vMergeStart"> <xsl:call-template name="calculateRowspan"> <xsl:with-param name="currentRow" select="../following-sibling::w:tr"/> <xsl:with-param name="columnPos" select="$pos"/> <xsl:with-param name="count" select="1"/> </xsl:call-template> </xsl:when> <xsl:otherwise>1</xsl:otherwise> </xsl:choose> </xsl:variable> <td> <xsl:if test="$rowspan > 1"> <xsl:attribute name="rowspan"> <xsl:value-of select="$rowspan"/> </xsl:attribute> </xsl:if> </td> </xsl:template> <xsl:template name="calculateRowspan"> <xsl:param name="currentRow"/> <xsl:param name="columnPos"/> <xsl:param name="count"/> <xsl:choose> <xsl:when test="$currentRow[1]/w:tc[position() = $columnPos]/w:tcPr/w:vMerge"> <xsl:call-template name="calculateRowspan"> <xsl:with-param name="currentRow" select="$currentRow[position() > 1]"/> <xsl:with-param name="columnPos" select="$columnPos"/> <xsl:with-param name="count" select="$count + 1"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$count"/> </xsl:otherwise> </xsl:choose> </xsl:template>我这个word的xml转xls-fo的xlst样式表,且xslxml版本均为1.0,表格处理这块不能用,设计行或者列单元格合并有问题,请修改完善,一定要注意这是word的xml转xls-fo的xlst样式表,且xslxml版本均为1.0,请直接给出修改结果
03-11
<!-- 单元格处理(行列合并修复版) --> <xsl:template match="w:tc"> <fo:table-cell border="1pt solid black" padding="2pt" display-align="center"> <!-- 列合并逻辑保持原样 --> <xsl:variable name="totalCols" select="count(ancestor::w:tbl/w:tblGrid/w:gridCol)"/> <xsl:variable name="precedingSpanSum"> <xsl:choose> <xsl:when test="preceding-sibling::w:tc"> <xsl:value-of select="sum(preceding-sibling::w:tc/w:tcPr/w:gridSpan/@w:val) + count(preceding-sibling::w:tc[not(w:tcPr/w:gridSpan)])"/> </xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </xsl:variable> <!-- 列合并属性生成保持原样 --> <!-- 行合并关键修复 --> <xsl:if test="w:tcPr/w:vMerge[@w:val='restart']"> <xsl:variable name="currentRow" select="ancestor::w:tr"/> <xsl:variable name="currentPos" select="count(preceding-sibling::w:tc) + 1"/> <!-- 计算实际行跨度 --> <xsl:variable name="rowSpan"> <xsl:call-template name="calculateRowSpan"> <xsl:with-param name="remainingRows" select="$currentRow/following-sibling::w:tr"/> <xsl:with-param name="targetColumn" select="$currentPos"/> <xsl:with-param name="accumulator" select="1"/> </xsl:call-template> </xsl:variable> <xsl:attribute name="number-rows-spanned"> <xsl:value-of select="$rowSpan"/> </xsl:attribute> </xsl:if> <fo:block linefeed-treatment="ignore" white-space-collapse="true"> <xsl:apply-templates select=".//w:p"/> </fo:block> </fo:table-cell> </xsl:template> <!-- 新增行跨度计算模板 --> <xsl:template name="calculateRowSpan"> <xsl:param name="remainingRows"/> <xsl:param name="targetColumn"/> <xsl:param name="accumulator"/> <xsl:choose> <xsl:when test="$remainingRows[1]/w:tc[$targetColumn]/w:tcPr/w:vMerge[@w:val='continue']"> <xsl:call-template name="calculateRowSpan"> <xsl:with-param name="remainingRows" select="$remainingRows[position() > 1]"/> <xsl:with-param name="targetColumn" select="$targetColumn"/> <xsl:with-param name="accumulator" select="$accumulator + 1"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$accumulator"/> </xsl:otherwise> </xsl:choose> </xsl:template>报错The column-number or number of cells in the row overflows the number of fo:table-columns specified for the table. (See position 1:10601),注意这是word的xml转xls-fo的xlst样式表,且版本均为1.0,请直接给出修改结果
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值