gpt4 book ai didi

xslt - 输出层次结构中当前节点的深度

转载 作者:行者123 更新时间:2023-12-03 15:23:57 25 4
gpt4 key购买 nike

使用 XSLT/XPATH 1.0,我想创建 HTML,其中 class span 的属性元素指示原始 XML 层次结构中的深度。

例如,使用此 XML 片段:

<text>
<div type="Book" n="3">
<div type="Chapter" n="6">
<div type="Verse" n="12">
</div>
</div>
</div>
</text>

我想要这个 HTML:
<span class="level1">Book 3</span>
<span class="level2">Chapter 6</span>
<span class="level3">Verse 12</span>

这些有多深 div元素可以去是不知道的先验。 div s 可以是 Book -> Chapter。它们可以是 Volume -> Book -> Chapter -> Paragraph -> Line。

我不能依赖@type 的值。部分或全部可能是 NULL。

最佳答案

这有一个非常简单和简短的解决方案——没有递归,没有参数,没有 xsl:element , 否 xsl:attribute :

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="div">
<span class="level{count(ancestor::*)}">
<xsl:value-of select="concat(@type, ' ', @n)"/>
</span>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时 :
<text>
<div type="Book" n="3">
<div type="Chapter" n="6">
<div type="Verse" n="12"></div></div></div>
</text>

产生了想要的、正确的结果 :
<span class="level1">Book 3</span>
<span class="level2">Chapter 6</span>
<span class="level3">Verse 12</span>

说明 :正确使用模板、AVT 和 count()功能。

关于xslt - 输出层次结构中当前节点的深度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9145391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com