XSLT嵌套列表重组:将子列表合并至对应父列表项
解决嵌套列表合并时XPath匹配子列表的问题
你的XPath之所以会把所有子列表关联到第一个父项,是因为following-sibling::li会选中当前节点所有后续的
基础场景(父项后紧跟一个子列表
如果每个父
following-sibling::li[1][not(normalize-space(text()))]/*[1][self::ul or self::ol]
following-sibling::li[1]:只取当前- 的第一个后续兄弟
- ,避免匹配其他父项的子列表
not(normalize-space(text())):确保这个- 没有有效文本内容(是用来包裹子列表的空项)
/*[1][self::ul or self::ol]:匹配该- 下的第一个ul/ol子节点
复杂场景(父项后跟着多个子列表
如果一个父项对应多个分散在空
following-sibling::li[not(normalize-space(text()))][preceding-sibling::li[normalize-space(text())][last()] = current()]/*[1][self::ul or self::ol]
preceding-sibling::li[normalize-space(text())][last()] = current():确保当前匹配的空- ,它最后一个有文本的前置兄弟就是当前父
- ,这样就能把父项后面所有属于它的子列表都捞出来
示例XSLT实现合并
如果用XSLT完成列表合并,可以参考这个模板:
<!-- 处理有文本的父列表项 --> <xsl:template match="li[normalize-space(text())]"> <li> <!-- 复制父项原有内容 --> <xsl:copy-of select="node()"/> <!-- 插入对应子列表 --> <xsl:copy-of select="following-sibling::li[1][not(normalize-space(text()))]/*[1][self::ul or self::ol]"/> </li> </xsl:template> <!-- 跳过空的子列表容器<li>,不输出到结果 --> <xsl:template match="li[not(normalize-space(text()))]"/> <!-- 默认复制模板 --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>
内容的提问来源于stack exchange,提问作者m4rc
相关产品推荐
相关产品推荐

