XSL 从平面树问题创建嵌套列表
我需要能够从平面树创建嵌套列表。例如,输入可能是这样的:
<root>
<h1>text</h1>
<list level="1">num1</list>
<list level="1">num2</list>
<list level="2">sub-num1</list>
<list level="2">sub-num2</list>
<list level="3">sub-sub-num1</list>
<list level="1">num3</list>
<p>text</p>
<list>num1</list>
<list>num2</list>
<h2>text</h2>
</root>
输出应该嵌套如下:
<root>
<h1>text</h1>
<ol>
<li>num1</li>
<li>num2
<ol>
<li>sub-num1</li>
<li>sub-num2
<ol>
<li>sub-sub-num1</li>
</ol>
</li>
</ol>
</li>
<li>num3</li>
</ol>
<p>text</p>
<ol>
<li>num1</li>
<li>num2</li>
</ol>
<h2>text</h2>
</root>
我已经尝试了几种方法,但似乎无法得到它。非常感谢任何帮助。 注意:我需要使用 XSLT 1.0 来完成此操作。
I need to be able to create nested lists from a flat tree. For example, the input might be something like this:
<root>
<h1>text</h1>
<list level="1">num1</list>
<list level="1">num2</list>
<list level="2">sub-num1</list>
<list level="2">sub-num2</list>
<list level="3">sub-sub-num1</list>
<list level="1">num3</list>
<p>text</p>
<list>num1</list>
<list>num2</list>
<h2>text</h2>
</root>
and the output should be nested as follows:
<root>
<h1>text</h1>
<ol>
<li>num1</li>
<li>num2
<ol>
<li>sub-num1</li>
<li>sub-num2
<ol>
<li>sub-sub-num1</li>
</ol>
</li>
</ol>
</li>
<li>num3</li>
</ol>
<p>text</p>
<ol>
<li>num1</li>
<li>num2</li>
</ol>
<h2>text</h2>
</root>
I've tried a few approaches but just can't seem to get it. Any help is greatly appreciated.
Note: I need to do this using XSLT 1.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这几乎让我发疯,但我还是完成了。我花了将近 2 个小时。
应用于稍微复杂的文档:
它产生这样的结果:
应用于您的示例,它也产生正确的结果:
It almost drove me mad, but I finished it. Took me almost 2 hours.
Applied to a slightly more complicated document:
It produces this result:
Applied to your sample it also produces the correct result:
此 XSLT 1.0 样式表:
输出:
注意:
xsl:key/@use
中current()
XSLT 函数的使用This XSLT 1.0 stylesheet:
Output:
Note: The use of
current()
XSLT function inxsl:key/@use
continue
This transformation:
when applied on this XML document (intentionally complicated to show that the solution works in many edge cases):
produces the wanted, correct result:
or as displayed by the browser:
text
text
text
text
text
您将在本文
http: //www.saxonica.com/papers/ideadb-1.1/mhk-paper.xml
注意:它是 XSLT 2.0。
You'll find a worked solution to a very similar problem in this paper
http://www.saxonica.com/papers/ideadb-1.1/mhk-paper.xml
Note: it's XSLT 2.0.