XSL使用写法与效果

本文介绍了一个使用XML结合XSLT进行数据展示的例子。通过具体的XML文件和XSL样式表代码,展示了如何将结构化的XML数据转换为易于阅读的HTML表格格式。

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

data.xml

<?xml-stylesheet type="text/xsl" href="getdata.xsl"?>
<ROOT>
<ASPNETX Username="Joe" City="Wuhan" Cool="70" />
<ASPNETX Username="Tim" City="TaiWan" Cool="100" />
<ASPNETX Username="Van" City="TaiWan" Cool="100" />
<ASPNETX Username="Joy" City="TaiWan" Cool="80" />
<ASPNETX Username="Kenny" City="TaiWan" Cool="60" />
</ROOT>


getdata.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<table align="left" cellpadding="2" cellspacing="5">
<tr>
<td style="font-family: Verdana; font-size: 15px; font-weight: bold;">XSL Test Result:</td>
</tr>
<tr>
<td style="font-family: Verdana; font-size: 10px;">
<table width="500px" align="left" cellpadding="2" cellspacing="0" style="font-family: Verdana; font-size: 10px;">
<tr>
<td bgcolor="#808080">
<font color="#FFFFFF">
<b>User name</b>
</font>
</td>
<td bgcolor="#808080">
<font color="#FFFFFF">
<b>City</b>
</font>
</td>
<td bgcolor="#808080">
<font color="#FFFFFF">
<b>Cool</b>
</font>
</td>
</tr>
<xsl:for-each select="ROOT/ASPNETX">
<tr>
<td style="border: 1px solid #808080">
<xsl:value-of select="@Username"/>
</td>
<td style="border: 1px solid #808080">
<xsl:value-of select="@City"/>
</td>
<td style="border: 1px solid #808080">
<xsl:value-of select="@Cool"/>
</td>
</tr>
</xsl:for-each>
</table>
</td>
</tr>
</table>
</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[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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值