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

时间:2018-05-24 15:12:35

标签: c# linq-to-xml

我有一些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>
<ref id="ref3"><label>(3)</label> <mixed-citation publication-type="online" publication-format="online"><person-group person-group-type="author"><string-name><given-names>J.J.</given-names> <surname>O&#x0027;Conner</surname></string-name> and <string-name><given-names>E.F.</given-names> <surname>Robertson</surname></string-name>, <string-name><surname>Baudhayana</surname></string-name></person-group>. <source>History of Mathematics Project</source>. <uri>http://wwwhistory.mcs.st-and.ac.uk/&#x007E;history/Biographies/Baudhayana.html</uri>.</mixed-citation></ref>
<ref id="ref4"><label>(4)</label> <mixed-citation publication-type="periodical" publication-format="print"><person-group person-group-type="author"><string-name><given-names>A.</given-names> <surname>Seidenberg</surname></string-name></person-group>, <article-title>The origin of mathematics</article-title>. <source>Archive for History of Exact Sciences</source> <volume>18</volume>, <fpage>301</fpage>&#x2013;<lpage>42</lpage>, <year>1978</year>.</mixed-citation></ref>
<ref id="ref5"><label>(5)</label> <mixed-citation publication-type="report" publication-format="online"><person-group person-group-type="author"><string-name><given-names>S.</given-names> <surname>Kak</surname></string-name></person-group>, <source>Early record of divisibility and primality</source>. arXiv:<pub-id pub-id-type="arxiv">0904.1154</pub-id>.</mixed-citation></ref>
<ref id="ref6"><label>(6)</label> <mixed-citation publication-type="thesis" publication-format="print"><person-group person-group-type="author"><string-name><given-names>S.</given-names> <surname>Kak</surname></string-name></person-group>, <source>The Asvamedha: The Rite and its Logic</source>. <publisher-loc>Delhi</publisher-loc>: <publisher-name>Motilal Banarsidass</publisher-name> <institution>NSF</institution> <institution content-type="ad">Caltech</institution>, <year>2002</year>.</mixed-citation></ref>
<ref id="ref7"><label>(7)</label> <mixed-citation publication-type="periodical" publication-format="print"><person-group person-group-type="author"><string-name><given-names>D.</given-names> <surname>McCullough</surname></string-name></person-group>, <article-title>Height and excess of Pythagorean triples</article-title>. <source>Mathematics Magazine</source> <volume>78</volume>, <fpage>26</fpage>&#x2013;<fpage>44</fpage>, <year>2005</year> <volume>Vi</volume>.</mixed-citation></ref>
<ref id="ref8"><label>(8)</label> <mixed-citation publication-type="thesis" publication-format="print"><person-group person-group-type="author"><string-name><surname>Roberts</surname>, <given-names>F.S.</given-names></string-name></person-group> (<year>1991</year>) <chapter-title>From Garbage to Rainbows: Generalizations of Graph Coloring and their Applications</chapter-title>. In <person-group person-group-type="editor"><string-name><surname>Alavi</surname>, <given-names>Y.</given-names></string-name>, <string-name><surname>Chartrand</surname>, <given-names>G.</given-names></string-name>, <string-name><surname>Oellermann</surname>, <given-names>O.R.</given-names></string-name>, and <string-name><surname>Schwenk</surname>, <given-names>A.J.</given-names></string-name></person-group> (eds.), <source>Graph Theory, Combinatorics, and Applications</source>. <institution>Yale University</institution> <publisher-loc>New York</publisher-loc>: <publisher-name>Wiley</publisher-name>.</mixed-citation></ref>
<ref id="ref9"><label>(9)</label> <mixed-citation publication-type="thesis" publication-format="print"><person-group person-group-type="author"><string-name><surname>Roberts</surname>, <given-names>F.S.</given-names></string-name></person-group> (<year>1993</year>) <chapter-title>From Garbage to Rainbows: Generalizations of Graph Coloring and their Applications</chapter-title>. In <person-group person-group-type="editor"><string-name><surname>Alavi</surname>, <given-names>Y.</given-names></string-name>, <string-name><surname>Chartrand</surname>, <given-names>G.</given-names></string-name>, <string-name><surname>Oellermann</surname>, <given-names>O.R.</given-names></string-name>, and <string-name><surname>Schwenk</surname>, <given-names>A.J.</given-names></string-name></person-group> (eds.), <source>Graph Theory, Combinatorics, and Applications</source>. <publisher-loc>New York</publisher-loc>: <publisher-name>Wiley</publisher-name>.</mixed-citation></ref>
</ref-list>

我正在尝试检查具有属性 publication-type =“thesis”的节点 mixed-citation ,并检查它是否有任何名为机构,其中没有任何名为 content-type 的属性。基本上, mixed-citation publication-type =“thesis”中不能有像<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 个答案:

答案 0 :(得分:1)

将您的查询更改为:

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"));

这应该产生想要的结果。