XPath学习:following-sibling

XPath 是一门在 XML 文档中查找信息的语言。XPath 可用来在 XML 文档中对元素和属性进行遍历。

XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointer 同时被构建于 XPath 表达之上。

推荐一个挺不错的网站:http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html 里面有很不错的例子,下面的例子中红色字体表示使用对应语法获取的元素(或属性)。

XPath轴(XPath Axes)可定义某个相对于当前节点的节点集:

1、child 选取当前节点的所有子元素

2、parent 选取当前节点的父节点

3、descendant 选取当前节点的所有后代元素(子、孙等)

4、ancestor 选取当前节点的所有先辈(父、祖父等)

5、descendant-or-self 选取当前节点的所有后代元素(子、孙等)以及当前节点本身

6、ancestor-or-self 选取当前节点的所有先辈(父、祖父等)以及当前节点本身

7、preceding-sibling 选取当前节点之前的所有同级节点

8、following-sibling 选取当前节点之后的所有同级节点

9、preceding 选取文档中当前节点的开始标签之前的所有节点

10、following 选取文档中当前节点的结束标签之后的所有节点

11、self 选取当前节点

12、attribute 选取当前节点的所有属性

13、namespace 选取当前节点的所有命名空间节点



following-sibling 选取当前节点之后的所有同级节点,跟preceding-sibling一样都是选取同级同父的节点,只不过following是取对应节点之后的节点,preceding-sibling取的是该节点之前的节点。



转自:http://www.cnblogs.com/zhaozhan/archive/2009/09/10/1564332.html




<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:variable name="adjustedGridSpan"> <xsl:choose> <xsl:when test="$currentGridSpan"> <xsl:value-of select="$currentGridSpan"/> </xsl:when> <xsl:otherwise>1</xsl:otherwise> </xsl:choose> </xsl:variable> <!-- 动态计算前置列跨度 --> <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) + count(preceding-sibling::w:tc[not(w:tcPr/w:gridSpan)])"/> </xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </xsl:variable> <!-- 生成列合并属性 --> <xsl:if test="$currentGridSpan"> <xsl:attribute name="number-columns-spanned"> <xsl:value-of select="$currentGridSpan"/> </xsl:attribute> </xsl:if> <!-- 改进的行合并计算 --> <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) + sum(preceding-sibling::w:tc/w:tcPr/w:gridSpan/@w:val) - 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:10790) 注意这是word的xml转xls-fo的xlst样式表,且版本均为1.0,请直接给出修改结果
最新发布
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值