XPATH 轴 - 问题
XML 结构:
<units>
<unit>
<lesson>
<name>Sample 1</name>
<sections>
<section type="IN">
<title> Sample Title 1 </title>
</section>
</sections>
</lesson>
<lesson>
<name>Sample 2</name>
<sections>
<section type="OF">
<title> Sample Title 2 </title>
</section>
</sections>
</lesson>
<lesson>
<name>Sample 3</name>
<sections>
<section type="IN">
<title> Sample Title 3</title>
</section>
</sections>
</lesson>
<lesson>
<name>Sample 4</name>
<sections>
<section type="AS">
<title> Sample Title 4</title>
</section>
</sections>
</lesson>
<lesson>
<name>Sample 5</name>
<sections>
<section type="IN">
<title> Sample Title 5</title>
</section>
</sections>
</lesson>
</unit>
</units>
我的要求是获取 title 元素的值并显示如下(对相似的数据进行分组并显示)
IN:
Sample Title 1
Sample Title 3
Sample Title 5
OF:
Sample Title 2
AS:
Sample Title 5
我已使用 follow-sibling 选项来获取预期的输出。由于 XML 结构很大(我只粘贴了代码片段),因此我无法使用 ../../ 以及 XSLT 中的所有内容对路径进行硬编码。请帮助我获得预期的输出。
XML structure:
<units>
<unit>
<lesson>
<name>Sample 1</name>
<sections>
<section type="IN">
<title> Sample Title 1 </title>
</section>
</sections>
</lesson>
<lesson>
<name>Sample 2</name>
<sections>
<section type="OF">
<title> Sample Title 2 </title>
</section>
</sections>
</lesson>
<lesson>
<name>Sample 3</name>
<sections>
<section type="IN">
<title> Sample Title 3</title>
</section>
</sections>
</lesson>
<lesson>
<name>Sample 4</name>
<sections>
<section type="AS">
<title> Sample Title 4</title>
</section>
</sections>
</lesson>
<lesson>
<name>Sample 5</name>
<sections>
<section type="IN">
<title> Sample Title 5</title>
</section>
</sections>
</lesson>
</unit>
</units>
My requirement is to get the values of title element and display as follows (Grouping similar data and display)
IN:
Sample Title 1
Sample Title 3
Sample Title 5
OF:
Sample Title 2
AS:
Sample Title 5
I have used following-sibling option to get the expected output. Since the XML structure is huge(I have pasted only the snippet), I cannot hard-code the path using ../../ and all in XSLT. Please help me in getting the expected output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用分组比任一同级轴更好地解决此问题:
输出:
为了完整起见,以下样式表使用
前面
和以下
轴实现了相同的结果:这是一个效率低得多的解决方案。解决这个问题的正常方法是使用 Muenchian 方法进行分组,如上所示。
It's better to solve this using grouping than either of the sibling axes:
Output:
For completeness, the following stylesheet achieves the same result using the
preceding
andfollowing
axes:This is a far less efficient solution. The normal way to solve this is with the Muenchian Method for grouping, as shown above.
这是 XSLT 2.0 中的解决方案:
Here is the solution in XSLT 2.0: