xslt中让两个fo:block处于同一行,展示不同的样式

本文介绍如何在XSLT中使用fo:list-block及其子标签来控制两个fo:block在同一行的不同样式显示,包括加粗标题和普通文本内容,并提供具体的代码示例。

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

在xslt中,有时需要使用两个fo:block在同一行,展示不同的样式,前面的是标题需要加粗,后面的是内容无任何变化,此时直接使用fo:block存在问题。

可以使用fo:list-block已经其子标签进行编写,就能解决此问题。为了实现第一个block加粗,第二个block不加粗,SimHei是黑体加粗,SimSun是宋体不加粗。

代码如下:

<fo:list-block provisional-distance-between-starts="50% + 5pt" 
provisional-label-separation="10pt">
				<!-- 备注 -->
				<fo:list-item>
					<fo:list-item-label end-indent="label-end()">
						<fo:block font-size="15pt" font-family="SimHei" text-indent="2em">
							备注:
						</fo:block> 
					</fo:list-item-label>
					<fo:list-item-body start-indent="body-start()">
						<fo:block font-size="15pt" font-family="SimSun">
							<xsl:value-of select="note/notePara"/>
						</fo:block> 
					</fo:list-item-body>
				</fo:list-item>
				<!-- 活动 -->
				<fo:list-item>
					<fo:list-item-label end-indent="label-end()">
						<fo:block font-size="15pt" font-family="SimHei" text-indent="2em">
							活动:
						</fo:block> 
					</fo:list-item-label>
					<fo:list-item-body start-indent="body-start()">
						<fo:block font-size="15pt" font-family="SimSun">
							<xsl:value-of select="action"/>
						</fo:block> 
					</fo:list-item-body>
				</fo:list-item>
				<!-- 步骤问题 -->
				<fo:list-item>
					<fo:list-item-label>
						<fo:block font-size="15pt" font-family="SimHei" text-indent="2em">
							提问:
						</fo:block> 
					</fo:list-item-label end-indent="label-end()">
					<fo:list-item-body start-indent="body-start()">
						<fo:block font-size="15pt" font-family="SimSun" text-indent="5em">
							<xsl:value-of select="isolationStepQuestion"/>
						</fo:block> 
					</fo:list-item-body>
				</fo:list-item>
			</fo:list-block>

provisional-distance-between-starts,end-indent,start-indent都是为了控制输出的宽度和大小,不是很熟悉,也不用熟悉,有例子能使用就还行。
这个还有问题,利用百分比调整存在位置误差,50%对于长文本不存在问题,对于短文本,A3,A4不同类型百分比控制不住,可以使用缩进方式。

代码如下:

<fo:list-block>
				<!-- 备注 -->
				<fo:list-item>
					<fo:list-item-label>
						<fo:block font-size="15pt" font-family="SimHei" text-indent="2em">
							备注:
						</fo:block> 
					</fo:list-item-label>
					<fo:list-item-body>
						<fo:block font-size="15pt" font-family="SimSun" text-indent="5em">
							<xsl:value-of select="note/notePara"/>
						</fo:block> 
					</fo:list-item-body>
				</fo:list-item>
				<!-- 活动 -->
				<fo:list-item>
					<fo:list-item-label>
						<fo:block font-size="15pt" font-family="SimHei" text-indent="2em">
							活动:
						</fo:block> 
					</fo:list-item-label>
					<fo:list-item-body>
						<fo:block font-size="15pt" font-family="SimSun" text-indent="5em">
							<xsl:value-of select="action"/>
						</fo:block> 
					</fo:list-item-body>
				</fo:list-item>
				<!-- 步骤问题 -->
				<fo:list-item>
					<fo:list-item-label>
						<fo:block font-size="15pt" font-family="SimHei" text-indent="2em">
							提问:
						</fo:block> 
					</fo:list-item-label>
					<fo:list-item-body>
						<fo:block font-size="15pt" font-family="SimSun" text-indent="5em">
							<xsl:value-of select="isolationStepQuestion"/>
						</fo:block> 
					</fo:list-item-body>
				</fo:list-item>
			</fo:list-block>

当然也可以使用表格的方式,将两个fo:block放在表格中,也能解决问题,代码如下:

<fo:table border-style="solid" border-width="0pt" table-layout="fixed" width="100%" overflow="auto">
			<fo:table-column column-width="3cm"/>
			<fo:table-column/>
				<fo:table-body>
					<fo:table-row>
						<fo:table-cell>
							<fo:block font-size="15pt" font-family="SimHei" text-indent="2em">
								提问:
							</fo:block> 
						</fo:table-cell>
						<fo:table-cell>
							<fo:block font-size="15pt" font-family="SimSun">
								<xsl:value-of select="isolationStepQuestion"/>
							</fo:block> 
						</fo:table-cell>
					</fo:table-row>
				</fo:table-body>
			</fo:table>

具体情况用具体方法。



<!-- 表格处理(增强修正版) --> <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[not(w:tcPr/w:vMerge/@w:val='continue')]"/> </fo:table-row> </xsl:template> <!-- 单元格处理(合并修复最终版) --> <xsl:template match="w:tc"> <fo:table-cell border="1pt solid black" padding="2pt" display-align="center" text-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="actualRowSpan" select="count(following-sibling::w:tr[ position() <= 10 and w:tc[position()=$currentPos]/w:tcPr/w:vMerge/@w:val='continue' ]) + 1"/> <xsl:attribute name="number-rows-spanned"> <xsl:value-of select="number($actualRowSpan)"/> <!-- 移除原错误限制 --> </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>上述xslt脚本中表格处理行合并单元格没事,但是列合并单元格失效,怎么修改,请给出修改好的完成脚本,注意:上述代码为word的xml转xsl-fo,且xml和xslt的版本都为1.0
03-08
<!-- 表格处理(增强版) --> <xsl:template match="w:tbl"> <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:apply-templates select="w:tr"/> </fo:table-body> </fo:table> </xsl:template> <!-- 表格行(过滤被合并单元格) --> <xsl:template match="w:tr"> <fo:table-row> <!-- 精确过滤被合并单元格:跳过所有标记为continue的单元格 --> <xsl:apply-templates select="w:tc[not(w:tcPr/w:vMerge/@w:val='continue')]"/> </fo:table-row> </xsl:template> <!-- 单元格模板(完整版) --> <xsl:template match="w:tc"> <fo:table-cell border="1pt solid black" padding="2pt" display-align="center"> <!-- 处理列合并 --> <xsl:if test="w:tcPr/w:gridSpan"> <xsl:attribute name="number-columns-spanned"> <xsl:value-of select="w:tcPr/w:gridSpan/@w:val"/> </xsl:attribute> </xsl:if> <!-- 处理行合并 --> <xsl:if test="w:tcPr/w:vMerge[@w:val='restart']"> <!-- 计算实际跨行数 = 下方连续标记为continue的单元格数 + 1 --> <xsl:variable name="rowSpan"> <xsl:value-of select="count(following::w:tr/w:tc[1][w:tcPr/w:vMerge/@w:val='continue']) + 1"/> </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>我这个提示The column-number or number of cells in the row overflows the number of fo:table-columns specified for the table. (See position 1:9340),请直接给出修改后的整体代码
03-08
<!-- 表格处理(增强修正版) --> <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="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="actualRowSpan" select="count(following-sibling::w:tr[1]/w:tc[1][w:tcPr/w:vMerge/@w:val='continue']) + 1"/> <xsl:attribute name="number-rows-spanned"> <xsl:value-of select="($actualRowSpan > 10) * 1 + ($actualRowSpan <= 10) * $actualRowSpan"/> </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>这个改完后,行格式对了,但是该合并展示的并没有合并起来,就是没有执行合并并居中的操作,应该怎么再改一下,请给出改完后的代码
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: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"> <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> <xsl:when test="not($remainingRows) or $position > count($remainingRows[1]/w:tc)"> <xsl:value-of select="$count"/> </xsl:when> <xsl:otherwise> <xsl:variable name="nextCell" select="$remainingRows[1]/w:tc[$position]"/> <xsl:choose> <xsl:when test="$nextCell/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="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>上述xslt样式表中表格的列合并单元格正常,但是行合并失效,我应该怎么修改?一定要注意这是word的xml转xls-fo的xlst样式表,且xsl和xml版本均为1.0,请直接给出修改结果
最新发布
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值