XSLT:如何对位于同一级别的各个元素进行分类
亲爱的社区, 我想将具有以下格式的初始 xml 转换
<h2>title1</h2>
<div>sometext1</div>
<div>sometext2</div>
<h2>title2</h2>
<div>sometext3</div>
<div>sometext4</div>
<h2>title3</h2>
<div>sometext5</div>
<div>sometext6</div>
为
<cat name="title1">
<div>sometext1</div>
<div>sometext2</div>
</cat>
<cat name="title2">
<div>sometext3</div>
<div>sometext4</div>
</cat>
<cat name="title3">
<div>sometext5</div>
<div>sometext6</div>
</cat>
:我尝试执行双 for-each 并创建一个变量来保存“select”选项来执行内部 for-each,但似乎需要使用节点集()函数。即使我尝试包含它,它也不起作用。您对如何使用 XSLT 1.0 并且最好不使用任何其他名称空间来解决此问题有什么想法吗?
Dear community,
I would like to transform an initial xml which has this format:
<h2>title1</h2>
<div>sometext1</div>
<div>sometext2</div>
<h2>title2</h2>
<div>sometext3</div>
<div>sometext4</div>
<h2>title3</h2>
<div>sometext5</div>
<div>sometext6</div>
into
<cat name="title1">
<div>sometext1</div>
<div>sometext2</div>
</cat>
<cat name="title2">
<div>sometext3</div>
<div>sometext4</div>
</cat>
<cat name="title3">
<div>sometext5</div>
<div>sometext6</div>
</cat>
I have tried to execute a double for-each and create a variable to hold the "select" option to execute the inner for-each, but seems like it is required to use the node-set() function. Even if I try to include it, it does not work. Do you have any thoughts on how to resolve this issue, using XSLT 1.0 and preferrably without using any other namespaces?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种不依赖于嵌套循环的方法。
它首先构建一个索引 (xsl:key),将每个 div 映射到其前面的 h2。然后我们有一个简单的身份转换来跳过 div 条目。对于遇到的每个 h2,我们生成
,然后输出从该 h2 索引的
标签。Here's one way of doing this that does not depend on nested loops.
It first builds an index (xsl:key) that maps each div to its immediately preceding h2. Then we have a simple identity transform that skips the div entries. For each h2 encountered we generate the
<cat>
and then output the<div...>
tags indexed from that h2.