Как проверить наличие определенных узлов, которые не имеют определенного атрибута? - PullRequest
0 голосов
/ 24 мая 2018

У меня есть некоторый 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>

Я пытаюсь проверить узлы смешанное цитирование , которые имеют атрибут publishing-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 Ответ

0 голосов
/ 24 мая 2018

Измените ваш запрос следующим образом:

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

Это должно привести к желаемым результатам.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...