c# - XSLT创建一个变量来计算for-each中的迭代次数
问题描述
我不知道如何创建变量和 assi。
我是使用 XSLT 的新手,我有一个 XML 文件,该文件有一些节点,节点有一些子节点我需要使用 for-each 来计算这些子节点(每个 for-each 我需要将该计数增加 1 以及我的计数器我想从1开始)
我不知道如何创建一个变量并将其分配给值 1。
这是我需要的示例:
<root>
<body>
<sec id="sec1">
<!--Parent also can contain no sub element or also can contain a free text-->
<p></p>
<p>some free text</p>
<p>
<!--Nodes I want to count it-->
<childNodes></childNodes>
<childNodes></childNodes>
<childNodes></childNodes>
<childNodes></childNodes>
<childNodes></childNodes>
<!--Nodes I want to count it-->
</p>
</sec>
<sec id="sec2">
<p>
<!--Nodes I want to count it-->
<childNodes></childNodes>
<childNodes></childNodes>
<childNodes></childNodes>
<childNodes></childNodes>
<childNodes></childNodes>
<!--Nodes I want to count it-->
</p>
<p>
<!--Nodes I want to count it-->
<childNodes></childNodes>
<childNodes></childNodes>
<childNodes></childNodes>
<childNodes></childNodes>
<childNodes></childNodes>
<!--Nodes I want to count it-->
</p>
</sec>
</body>
</root>
我需要这样的输出
<root>
<childNodes>
<count>
The count of all childNodes
</count>
</childNodes>
</root>
你能帮忙解决这个问题吗,在此先感谢
解决方案
使用 xml linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication58
{
class Program
{
static void Main(string[] args)
{
XElement root = new XElement("root");
XElement body = new XElement("body");
root.Add(body);
for (int id = 1; id <= 10; id++)
{
XElement newSec = new XElement("sec",
new XAttribute("id", "sec" + id.ToString()),
XElement.Parse("<!--Parent also can contain no sub element or also can contain a free text--><p></p>"),
new XElement("p", "some free text")
);
body.Add(newSec);
XElement nodes = new XElement("p");
newSec.Add(nodes);
for (int childCount = 1; childCount <= 10; childCount++)
{
XElement newChild = new XElement("childNods", new XAttribute("id", "node" + childCount.ToString()),
"Child Text"
);
nodes.Add(newChild);
}
}
}
}
}
推荐阅读
- java - Spring Data JPA OneTo Many POST 映射问题
- css - 如何将活动类添加到包装在 Navlink 中的 img 标签?
- node.js - 向外部服务器请求时如何在 Kubernetes 容器中代理 IP 地址
- regex - 如何结合 xslt 和正则表达式来查找特定字符串
- r - R:优化错误:“列表”对象不能被强制输入“双”
- php - 我怎样才能在 php 字符串中实现这一点到 bbbvvvvzzxxc => 3v4v2z2xc
- flutter - Flutter:SingleChildScrollView 不适用于列和网格视图。为什么?
- azure - AZ Azure 命令失败:错误:需要以下参数:--name/-n、--resource-group/-g
- mysql - Debezium Mysql 连接器因 IllegalStateException 而失败,历史主题具有无限保留
- macos - 如何使用 Sphinx 生成 PDF