问题描述
我有一些xml文件
我想搜索每个 mixed-citation
并检查 mixed-citation
中是否存在任何一个给定标签,其特定属性值为发布类型
并获取父 ref
以及匹配的不支持标记的名称。
这是一个
示例XML:
I've got some xml files like this
I want to search each mixed-citation
and check whether any one of the given tags exist in a mixed-citation
with specific attribute value of publication-type
and get the parent ref
and the name of the unsupported tag that is matched.
Here is a list of unsupported tags for a specific publication-type
SAMPLE XML:
<?xml version="1.0"?>
<ref-list>
<title>References</title>
<ref id="ref1"><label>[1]</label><mixed-citation publication-type="patent"><person-group person-group-type="author"><string-name><surname>Angel’skii</surname>, <given-names>O.,V.</given-names></string-name>, <string-name><surname>Ushenko</surname>, <given-names>A.,G.</given-names></string-name>, <string-name><surname>Arkhelyuk</surname>, <given-names>A.,D.</given-names></string-name>, <string-name><surname>Ermolenko</surname>, <given-names>S.,B.</given-names></string-name>, <string-name><surname>Burkovets</surname>, <given-names>D.,N.</given-names></string-name></person-group>, "<article-title>Scattering of laser radiation by multifractal biological structures</article-title>." <source>Optika ieee Spektroskopiya 88</source> (<issue>3</issue>), <fpage>495</fpage>–<lpage>498</lpage> (<year>2000</year>).</mixed-citation></ref>
<ref id="ref2"><label>[2]</label><mixed-citation publication-type="periodical"><person-group person-group-type="author"><string-name><surname>Angelsky</surname>, <given-names>O.,V.</given-names></string-name>, <string-name><surname>Hanson</surname>, <given-names>S., G.</given-names></string-name>, <string-name><surname>Zenkova</surname>, <given-names>C.,Yu.</given-names></string-name>, <string-name><surname>Gorsky</surname>, <given-names>M.,P.</given-names></string-name>, <string-name><surname>Gorodyns’ka</surname>, <given-names>N.,V.</given-names></string-name></person-group>, "<article-title>On polarization metrology (estimation) of the degree of coherence of optical waves</article-title>." <source>Optics Express</source> <volume>17</volume>(<issue>18</issue>), pp.<fpage>15623</fpage>–<lpage>15634</lpage> (<year>2009</year>).</mixed-citation></ref>
<ref id="ref3"><label>[3]</label><mixed-citation publication-type="periodical"><person-group person-group-type="author"><string-name><surname>Angelsky</surname>, <given-names>O.,V.</given-names></string-name>, <string-name><surname>Maksimyak</surname>, <given-names>P.,P.</given-names></string-name>, <string-name><surname>Hanson</surname>, <given-names>S.,G.</given-names></string-name>, <string-name><surname>Ryukhin</surname>, <given-names>V.,V.</given-names></string-name></person-group>, "<article-title>New Feasibilities for Characterizing Rough Surfaces by Optical-Correlation Techniques</article-title>" <source>Applied Optics</source> (<issue>40</issue>) , pp. <fpage>5693</fpage>–<lpage>5707</lpage> <conf-date>12-15-2007</conf-date> (<year>2001</year>).</mixed-citation></ref>
<ref id="ref4"><label>[4]</label><mixed-citation publication-type="book"><person-group person-group-type="author"><string-name><surname>Ushenko</surname>, <given-names>Yu.,O.</given-names></string-name>, <string-name><surname>Dubolazov</surname>, <given-names>O., V.</given-names></string-name>, <string-name><surname>Karachevtsev</surname>, <given-names>A.,O.</given-names></string-name>, <string-name><surname>Gorsky</surname>, <given-names>M., P.</given-names></string-name>, <string-name><surname>Marchuk</surname>, <given-names>Yu., F.</given-names></string-name></person-group>, "<article-title>Wavelet analysis of Fourier polarized images of the human bile</article-title>." <source specific-use="IEEE">Applied Optics</source> (<issue>51</issue>), P. <fpage>133</fpage>–<lpage>139</lpage> (<year>2012</year>).</mixed-citation></ref>
<ref id="ref5"><label>[5]</label><mixed-citation publication-type="periodical"><person-group person-group-type="author"><string-name><surname>Angelsky</surname>, <given-names>O.,V.</given-names></string-name>, <string-name><surname>Ushenko</surname>, <given-names>A.,G.</given-names></string-name>, <string-name><surname>Burkovets</surname>, <given-names>D.,N.</given-names></string-name>, <string-name><surname>Ushenko</surname>, <given-names>Y., A.</given-names></string-name></person-group>, "<article-title>Polarization visualization and selection of biotissue image two-layer scattering medium</article-title>." <source>Journal of biomedical optics</source> <volume>10</volume>(<issue>1</issue>), P.<fpage>14010</fpage> (<year>2005</year>).</mixed-citation></ref>
<ref id="ref6"><label>[6]</label><mixed-citation publication-type="periodical"><person-group person-group-type="author"><string-name><surname>Angelsky</surname>, <given-names>O.,V.</given-names></string-name>, <string-name><surname>Polyanskii</surname>, <given-names>P.,V.</given-names></string-name>, <string-name><surname>Felde</surname>, <given-names>C.,V.</given-names></string-name></person-group>, "<article-title>The emerging field of correlation optics</article-title>." <source>Optics and Photonics News</source> <volume>23</volume>(<issue>4</issue>), p.p.<fpage>25</fpage>–<lpage>29</lpage> (<year>2012</year>).</mixed-citation></ref>
<ref id="ref7"><label>[7]</label><mixed-citation publication-type="periodical"><person-group person-group-type="author"><string-name><surname>Angelsky</surname>, <given-names>O.,V.</given-names></string-name>, <string-name><surname>Bekshaev</surname>, <given-names>A.,Ya.</given-names></string-name>, <string-name><surname>Maksimyak</surname>, <given-names>P.,P.</given-names></string-name>, <string-name><surname>Maksimyak</surname>, <given-names>A.,P.</given-names></string-name>, Mokhun, <string-name><surname>Hanson</surname>, <given-names>S.,G.</given-names></string-name>, <string-name><surname>Zenkova</surname>, <given-names>C., Yu.</given-names></string-name>, <string-name><surname>Tyurin</surname>, <given-names>A.,V.</given-names></string-name></person-group>, "<article-title>Circular motion of particles suspended in a Gaussian beam with circular polarization validates the spin part of the internal energy flow</article-title>." <source>Optics Express</source> <volume>20</volume>(<issue>10</issue>), pp.<fpage>11351</fpage>–<lpage>11356</lpage> (<year>2012</year>).</mixed-citation></ref>
<ref id="ref8"><label>[8]</label><mixed-citation publication-type="periodical"><person-group person-group-type="author"><string-name><surname>Angelsky</surname>, <given-names>O.V.</given-names></string-name>, <string-name><surname>Besaha</surname>, <given-names>R.N.</given-names></string-name>, <string-name><surname>Mokhun</surname>, <given-names>I.I.</given-names></string-name></person-group> "<article-title>Appearance of wavefront dislocations under interference among beams with simple wavefronts</article-title>," <source>Optica Applicata</source> <volume>27</volume>(<issue>4</issue>), Pages <fpage>272</fpage>–<lpage>278</lpage> <edition>5</edition> (<year>1997</year>).</mixed-citation></ref>
<ref id="ref9"><label>[9]</label><mixed-citation publication-type="book"><person-group person-group-type="author"><string-name><surname>Angelsky</surname>, <given-names>P., O.</given-names></string-name>, <string-name><surname>Ushenko</surname>, <given-names>A., G.</given-names></string-name>, <string-name><surname>Dubolazov</surname>, <given-names>A., V.</given-names></string-name>, <string-name><surname>Sidor</surname>, <given-names>M., I.</given-names></string-name>, <string-name><surname>Bodnar</surname>, <given-names>G., B.</given-names></string-name>, <string-name><surname>Koval</surname>, <given-names>G.</given-names></string-name>, <string-name><surname>Trifonyuk</surname>, <given-names>L.</given-names></string-name></person-group>, "<article-title>The singular approach for processing polarization-inhomogeneous laser images of blood plasma layers</article-title>." <source source-type="IEEE">J. Opt.</source> (<issue>15</issue>), <fpage>044030</fpage> (<year>2013</year>).</mixed-citation></ref>
</ref-list>
What I have tried:
What I have tried:
XDocument doc=XDocument.Load(@"D:\ref.xml");
var invalid_citations1=doc.Descendants("mixed-citation").Where(q=>q.Attribute("publication-type").Value=="periodical")
.Where(a=>a.Descendants("edition").Any() || a.Descendants("chapter-title").Any()||a.Descendants("conf-date").Any()||a.Descendants("conf-loc").Any()||a.Descendants("conf-name").Any()||a.Descendants("conf-sponsor").Any())
.Select(x=>x.Parent.Attribute("id"));
var invalid_citations2=doc.Descendants("mixed-citation").Where(q=>q.Attribute("publication-type").Value=="book")
.Where(a=>a.Descendants("article-title").Any() || a.Descendants("conf-sponsor").Any()||a.Descendants("conf-date").Any()||a.Descendants("conf-loc").Any()||a.Descendants("conf-name").Any()||a.Descendants("conf-sponsor").Any()|| a.Descendants("institution").Any() || a.Descendants("ref-degree").Any() || a.Descendants("patent").Any() || a.Descendants("std").Any())
.Select(x=>x.Parent.Attribute("id"));
foreach (var element in invalid_citations1) {
Console.WriteLine("Check "+element+" ==> publication-type=\"periodical\": for unsupported tag/tags");
}
foreach (var element in invalid_citations2) {
Console.WriteLine("Check "+element+" ==> publication-type=\"book\": for unsupported tag/tags");
}
Console.ReadLine();
But I cannot get the name of the unsupported tag that is matched for each mixed-citation
...How do I get that?
Also, how can I do this for all different publication-type
’s in a single expression, rather than doing invalid_citations1, invalid_citations2 ... etc.(as I did in my code)?
But I cannot get the name of the unsupported tag that is matched for each mixed-citation
...How do I get that?
Also, how can I do this for all different publication-type
's in a single expression, rather than doing invalid_citations1, invalid_citations2 ... etc.(as I did in my code)?
推荐答案
var invalidTags = new List<string> { "edition", "chapter-title", "conf-date", "conf-loc", "conf-name", "conf-sponsor" };
var citationQuery = from item in doc.Descendants("mixed-citation")
where item.Attribute("publication-type").Value == "periodical"
&& item.Descendants().Any(x => invalidTags.Contains(x.Name.LocalName))
select new {
a = item.Parent.Attribute("id"),
b = item.Descendants().Where(x => invalidTags.Contains(x.Name.LocalName))
};
In the returned class, property a
contains the attribute while b
contains a list of matching tags
ADDITION
--------
To fetch the tags based on publication type, consider the following
Method to list tags
In the returned class, property a
contains the attribute while b
contains a list of matching tags
ADDITION
--------
To fetch the tags based on publication type, consider the following
Method to list tags
public static List<string> GetInvalidTags(string publicationType) {
List<string> invalidTags = new List<string>();
if (publicationType == "periodical") {
invalidTags = new List<string> { "edition", "chapter-title", "conf-date", "conf-loc", "conf-name", "conf-sponsor" };
}
return invalidTags;
}
And usage
And usage
var citationQuery = from item in doc.Descendants("mixed-citation")
where item.Descendants().Any(x => GetInvalidTags(item.Attribute("publication-type").Value).Contains(x.Name.LocalName))
select new {
a = item.Parent.Attribute("id"),
b = item.Descendants().Where(x => GetInvalidTags(item.Attribute("publication-type").Value).Contains(x.Name.LocalName)),
c = item.Descendants().Where(x => GetInvalidTags(item.Attribute("publication-type").Value).Contains(x.Name.LocalName)).First().Name
};
这篇关于如何检查特定父节点内是否存在特定的子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!