XSL基础: call-template,apply-templates,xsl:param,with-param,number format 用法

本文介绍了一种使用XML与XSLT实现有序列表自动编号的方法,通过定义不同的模板来处理不同层级的列表项,并利用XSLT的特性完成格式化输出。

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

最后结果:

XML

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

XSL

<?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"> <!--匹配XML Document中的 ol/li mark--> <xsl:template match="ol/li"> <br/> <!--Invoke template--> <xsl:call-template name="number-block"> </xsl:call-template> </xsl:template> <xsl:template match="ol//ol/li"> <br/> <xsl:call-template name="number-block"> <!--Pass param to template--> <xsl:with-param name="format">a.</xsl:with-param> </xsl:call-template> </xsl:template> <!--Declare template named number-block--> <xsl:template name="number-block"> <!--Declare a para named format --> <xsl:param name="format">1. </xsl:param> <fo:block> <xsl:number format ="{$format}"/> <!--Apply template--> <xsl:apply-templates></xsl:apply-templates> </fo:block> </xsl:template> </xsl:stylesheet>

<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"> <fo:table-row> <xsl:apply-templates select="w:tc"/> </fo:table-row> </xsl:template> <xsl:template match="w:tc"> <fo:table-cell border="1pt solid black" padding="2pt" display-align="center"> <xsl:variable name="currentGridSpan" select="w:tcPr/w:gridSpan/@w:val"/> <xsl:if test="$currentGridSpan"> <xsl:attribute name="number-columns-spanned"> <xsl:value-of select="$currentGridSpan"/> </xsl:attribute> </xsl:if> <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="$precedingSpanSum + 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> 上述xlst样式表使用后报错The column-number or number of cells in the row overflows the number of fo:table-columns specified for the table. (See position 1:10790),怎么修改,请直接给出修改好的样式表,注意这是word的xml转xls-fo的xlst样式表,且版本均为1.0,请直接给出修改结果
03-08
<!-- 单元格处理(行列合并修复版) --> <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
<xsl:template match="w:tc"> <fo:table-cell border="1pt solid black" padding="2pt" display-align="center"> <!-- 原有属性处理逻辑保持不变 --> <xsl:variable name="currentGridSpan" select="w:tcPr/w:gridSpan/@w:val"/> <xsl:if test="$currentGridSpan"> <xsl:attribute name="number-columns-spanned"> <xsl:value-of select="$currentGridSpan"/> </xsl:attribute> </xsl:if> <!-- 原有跨行处理逻辑保持不变 --> <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="$precedingSpanSum + 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:10790),一定要注意这是word的xml转xls-fo的xlst样式表,且xsl和xml版本均为1.0,请直接给出修改结果
最新发布
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值