为了账号安全,请及时绑定邮箱和手机立即绑定

如何检查某些没有特定属性的节点?

如何检查某些没有特定属性的节点?

C#
回首忆惘然 2021-06-30 18:11:35
我有一些像下面这样的 xml 文件<ref-list><ref id="ref1"><label>(1)</label> <mixed-citation publication-type="book" publication-format="print"><person-group person-group-type="author"><string-name><given-names>C.N.</given-names> <surname>Srinivasiengar</surname></string-name></person-group>, <source>The History of Ancient Indian Mathematics</source>. <publisher-loc>Calcutta</publisher-loc>: <publisher-name>The World Press</publisher-name>, <year>1967</year>.</mixed-citation></ref><ref id="ref2"><label>(2)</label> <mixed-citation publication-type="periodical" publication-format="print"><person-group person-group-type="author"><string-name><given-names>F.J.M.</given-names> <surname>Barning</surname></string-name></person-group>, <article-title>On Pythagorean and quasi-Pythagorean triangles and a generation process with the help of unimodular matrices</article-title>, <source type="IEEE">(Dutch) Math. Centrum Amsterdam Afd. Zuivere Wisk, ZW-011</source>, <year>1963</year>.</mixed-citation></ref>我正在尝试检查具有属性Publication-type="thesis"的节点混合引用,并检查它是否有任何名为机构的子元素,其中没有任何名为content-type 的属性。基本上有不能像任何节点在混合引出版物型=“论文”但可以有。<institution>...</institution><institution content-type="...">...</institution>这是我试过的XDocument doc=XDocument.Load(xmlfile);var thesiss=doc.Descendants("mixed-citation")    .Where(a=>a.Attribute("publication-type").Value=="thesis")    .Where(a=>a.Descendants("institution").Any() && !a.Descendants("institution").Attributes("content-type").Any())    .Select(a=>a.Parent.Attribute("id"));foreach (var element in thesiss) {    Console.WriteLine("Check "+element);}但它并不完全按照我想要的方式工作。上面给出的样本应该给出输出Check id="ref6"Check id="ref8"但它只显示Check id="ref8"。请帮忙。
查看完整描述

1 回答

?
白衣非少年

TA贡献1155条经验 获得超0个赞

将您的查询更改为:


var thesiss = doc.Descendants("mixed-citation")

    .Where(a => a.Attribute("publication-type").Value == "thesis")

    .Where(a => !a.Descendants("institution").All(d => d.Attribute("content-type") != null))

    .Select(a => a.Parent.Attribute("id"));

这应该会产生想要的结果。


查看完整回答
反对 回复 2021-07-03
  • 1 回答
  • 0 关注
  • 121 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信