具有嵌套元素的 XML 使用 XSLT v1.0 C# 进行过滤和计算
我有一个示例 xml 文件,其中 root 具有名称为“element”的元素。 该元素可以嵌套。
我想排除名称为“position”的元素,其中 “位置”值 x = (“数字” + “另一个”) * 计数大于 所有“position”元素的 sum(("number" + "another") * "count") 的平均值。
如何使用 xslt v 1 处理此 xml 文件。
<?xml version="1.0" encoding="utf-8" ?>
<root>
<element>
<position>
<number>
1
</number>
<another>
2
</another>
<count>
3
</count>
</position>
<position>
<number>
3
</number>
<another>
1
</another>
<count>
5
</count>
</position>
<element>
<position>
<number>
3
</number>
<another>
3
</another>
<count>
5
</count>
</position>
<position>
<number>
3
</number>
<another>
6
</another>
<count>
5
</count>
</position>
<element>
<position>
<number>
3
</number>
<another>
3
</another>
<count>
5
</count>
</position>
<position>
<number>
3
</number>
<another>
7
</another>
<count>
5
</count>
</position>
<element>
<position>
<number>
33
</number>
<another>
4
</another>
<count>
5
</count>
</position>
<position>
<number>
34
</number>
<another>
3
</another>
<count>
5
</count>
</position>
</element>
</element>
</element>
</element>
<element>
<position>
<number>
5
</number>
<another>
1
</another>
<count>
2
</count>
</position>
<position>
<number>
3
</number>
<another>
3
</another>
<count>
9
</count>
</position>
<element>
<position>
<number>
5
</number>
<another>
3
</another>
<count>
2
</count>
</position>
<position>
<number>
3
</number>
<another>
3
</another>
<count>
5
</count>
</position>
</element>
</element>
</root>
I have got a sample xml file where root has element with name "element".
This elements can be nested.
I would like to exclude elements with name "position" where
"position" value x = ("number" + "another") * count is greater than
average of sum(("number" + "another") * "count") from all "position" elements.
How to process this xml file with xslt v 1.
<?xml version="1.0" encoding="utf-8" ?>
<root>
<element>
<position>
<number>
1
</number>
<another>
2
</another>
<count>
3
</count>
</position>
<position>
<number>
3
</number>
<another>
1
</another>
<count>
5
</count>
</position>
<element>
<position>
<number>
3
</number>
<another>
3
</another>
<count>
5
</count>
</position>
<position>
<number>
3
</number>
<another>
6
</another>
<count>
5
</count>
</position>
<element>
<position>
<number>
3
</number>
<another>
3
</another>
<count>
5
</count>
</position>
<position>
<number>
3
</number>
<another>
7
</another>
<count>
5
</count>
</position>
<element>
<position>
<number>
33
</number>
<another>
4
</another>
<count>
5
</count>
</position>
<position>
<number>
34
</number>
<another>
3
</another>
<count>
5
</count>
</position>
</element>
</element>
</element>
</element>
<element>
<position>
<number>
5
</number>
<another>
1
</another>
<count>
2
</count>
</position>
<position>
<number>
3
</number>
<another>
3
</another>
<count>
9
</count>
</position>
<element>
<position>
<number>
5
</number>
<another>
3
</another>
<count>
2
</count>
</position>
<position>
<number>
3
</number>
<another>
3
</another>
<count>
5
</count>
</position>
</element>
</element>
</root>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将使用以下两遍方法:
应用于提供的 XML 文档时:
生成所需的正确结果:
I would use the following two-pass approach:
when applied on the provided XML document:
the wanted, correct result is produced:
只是为了好玩,这个没有扩展的样式表:
输出:
看看 XSLT 2.0 解决方案是多么紧凑:
Just for fun, this stylesheet without extensions:
Output:
Look how compact an XSLT 2.0 solution is: