XSL 嵌入式 for-each
我知道这里对此有所介绍,但我是 XSLT 的新手,我正在尝试在输入 XML 中位于不同位置但处于同一节点级别的节点上执行嵌套的 for-each 循环。
所以...例如:
<Level1>
<Level2>
<Level3a>
<Item1>Clothes Washed</Item1>
<Item2>08/02/2011 06:54</Item2>
<DoneBy>Ingrid</DoneBy>
</Level3a>
<Level3a>
<Item1>Car Washed</Item1>
<Item2>08/02/2011 08:25</Item2>
<DoneBy>Jeanette</DoneBy>
</Level3a>
<Level3a>
<Item1>Dog Walked</Item1>
<Item2>08/02/2011 10:30</Item2>
<DoneBy>Ingrid</DoneBy>
</Level3a>
<Level3b>
<DoneWho>Ingrid</DoneWho>
<JobTitle>Main Asst</JobTitle>
</Level3b>
<Level3b>
<DoneWho>Jeanette</DoneWho>
<JonTitle>2nd Asst</JobTitle>
</Level3b>
</Level2>
</Level1>
我需要编辑输出
<Jobs>
<CompletedJob>
<JobTitle>Main Asst</JobTitle>
<Job>Clothes Washed</Job>
<CompOn>08/02/2011</CompOn>
<CompAt>06:54<CompAt>
</CompletedJob>
<CompletedJob>
<JobTitle>Main Asst</JobTitle>
<Job>Dog Walked</Job>
<CompOn>08/02/2011</CompOn>
<CompAt>10:30</CompAt>
</CompletedJob>
<CompletedJob>
<JobTitle>2nd Asst</JobTitle>
<Job>Car Washed</Job>
<CompOn>08/02/2011</CompOn>
<CompAt>08:25</CompAt>
</CompletedJob>
</Jobs>
:请查看上面输出中的更改...我想这确实是我想要做的。 将输出替换为 ... 再次感谢。
我尝试过嵌套 for-each 循环,但无法从主 for-each 循环内部引用不同的节点。
非常感谢任何帮助。
I know this is covered somewhat on here, but I'm new to XSLT and I'm trying to do a nested for-each loop on nodes that are in different places in the input XML but at the same node level.
So...for example:
<Level1>
<Level2>
<Level3a>
<Item1>Clothes Washed</Item1>
<Item2>08/02/2011 06:54</Item2>
<DoneBy>Ingrid</DoneBy>
</Level3a>
<Level3a>
<Item1>Car Washed</Item1>
<Item2>08/02/2011 08:25</Item2>
<DoneBy>Jeanette</DoneBy>
</Level3a>
<Level3a>
<Item1>Dog Walked</Item1>
<Item2>08/02/2011 10:30</Item2>
<DoneBy>Ingrid</DoneBy>
</Level3a>
<Level3b>
<DoneWho>Ingrid</DoneWho>
<JobTitle>Main Asst</JobTitle>
</Level3b>
<Level3b>
<DoneWho>Jeanette</DoneWho>
<JonTitle>2nd Asst</JobTitle>
</Level3b>
</Level2>
</Level1>
I need the output to be
<Jobs>
<CompletedJob>
<JobTitle>Main Asst</JobTitle>
<Job>Clothes Washed</Job>
<CompOn>08/02/2011</CompOn>
<CompAt>06:54<CompAt>
</CompletedJob>
<CompletedJob>
<JobTitle>Main Asst</JobTitle>
<Job>Dog Walked</Job>
<CompOn>08/02/2011</CompOn>
<CompAt>10:30</CompAt>
</CompletedJob>
<CompletedJob>
<JobTitle>2nd Asst</JobTitle>
<Job>Car Washed</Job>
<CompOn>08/02/2011</CompOn>
<CompAt>08:25</CompAt>
</CompletedJob>
</Jobs>
EDIT: please see the change in the above output...I guess this is really what I'm trying to do.
Replacing in output with ...thanks again.
I've tried a neste for-each loop, but I can't refer to a different node from inside the main for-each loop.
Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不必使用嵌套循环(或根本不使用循环)。
使用输入 XML 的固定版本:
以及此样式表:
生成所需的输出:
注意:到目前为止,我的答案是唯一对输出进行排序并生成准确结果(减去格式良好错误)的答案。通缉。
You shouldn't have to use nested loops (or loops at all).
Using a fixed version of the input XML:
and this stylesheet:
produces the wanted output:
NOTE: So far, my answer is the only one that sorts the output and produces the exact result (minus the well-formedness error) you wanted.
不需要嵌套循环 - XSLT 本质上具有足够的递归性。
以下样式表完成这项工作:
Level3b 元素现在用于将 DoneBy 解析为 JobTitle。
No need for nested loops - XSLT is resursive enough by nature.
Following stylesheet does the job:
Level3b elements are now used for resolving DoneBy to JobTitle.
该死,打败我了。哦,好吧,不妨也给出我的解决方案:
Darn, beat me to it. Oh well, might as well give my solution too:
你的xml输入真的很糟糕。
这会生成你的输出
your xml input is realy terrible.
this generates your output