猿问

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

我有一些像下面这样的 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"。请帮忙。


回首忆惘然
浏览 141回答 1
1回答

白衣非少年

将您的查询更改为:var thesiss = doc.Descendants("mixed-citation")&nbsp; &nbsp; .Where(a => a.Attribute("publication-type").Value == "thesis")&nbsp; &nbsp; .Where(a => !a.Descendants("institution").All(d => d.Attribute("content-type") != null))&nbsp; &nbsp; .Select(a => a.Parent.Attribute("id"));这应该会产生想要的结果。
随时随地看视频慕课网APP
我要回答