質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Linux

Linuxは、Unixをベースにして開発されたオペレーティングシステムです。日本では「リナックス」と呼ばれています。 主にWebサーバやDNSサーバ、イントラネットなどのサーバ用OSとして利用されています。 上位500のスーパーコンピュータの90%以上はLinuxを使用しています。 携帯端末用のプラットフォームAndroidは、Linuxカーネル上に構築されています。

検索

検索は、あるデータの集まりの中から 目的のデータを見つけ出すことです。

Q&A

2回答

2359閲覧

キーワードを照合しながら、XMLファイルから特定の要素を抽出して出力する

kakuko

総合スコア2

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Linux

Linuxは、Unixをベースにして開発されたオペレーティングシステムです。日本では「リナックス」と呼ばれています。 主にWebサーバやDNSサーバ、イントラネットなどのサーバ用OSとして利用されています。 上位500のスーパーコンピュータの90%以上はLinuxを使用しています。 携帯端末用のプラットフォームAndroidは、Linuxカーネル上に構築されています。

検索

検索は、あるデータの集まりの中から 目的のデータを見つけ出すことです。

0グッド

0クリップ

投稿2020/06/20 06:11

編集2020/06/20 09:06

前提・実現したいこと

例えば、下方のXMLファイルがあり、私の手元には以下の「参照用のID listファイル」があります。inputファイルとしてこのID list を使い、xml ファイル内の<Ids>に必ず記載されているIDと照合し、もし一致した場合に、その子要素として必ず一緒に記述されている特定の要素(<Attribute attribute_name="isolation_source" harmonized_name="isolation_source" display_name="isolation source">surface water</Attribute>)の</Attribute>の前のキーワード(ここではsurface water)を抽出し、元のIDとともに出力するにはどのようにすればよろしいでしょうか?linux 初心者で、またxml ファイルを扱うのも初めてで、素人の質問で申し訳ございません。記載事項に不備がございましたら、ご質問いただけましたら幸いです。【追加1】XMLファイルを処理する場合、xmllint を有効活用すべきとコメントいただきまして試行錯誤中ですが、子要素内のIDやキーワードを抽出することもできておらず、引き続き勉強中です。【追加2】XMLファイルをソースからそのままコピーしました。

●参照するID のlist ファイル

SAMN03741962
SAMN03741963

●XMLファイルと照合後に出力したいlist ファイル

SAMN03741962^ surface water
SAMN03741963^ surface water

*ここで「^」はタブ区切りを示しています。

該当のXMLファイル

XML

1<?xml version="1.0" encoding="UTF-8"?> 2<BioSampleSet> 3<BioSample access="public" publication_date="2015-11-08T00:00:00.000" last_update="2019-06-20T17:16:02.371" submission_date="2015-05-28T09:58:39.000" id="3741962" accession="SAMN03741962"> 4 <Ids> 5 <Id db="BioSample" is_primary="1">SAMN03741962</Id> 6 <Id db_label="Sample name">BACL4 MAG-120507-bin80</Id> 7 </Ids> 8 <Description> 9 <Title>Microbe sample from OM182 bacterium BACL3 MAG-120507-bin80</Title> 10 <Organism taxonomy_id="1655577" taxonomy_name="OM182 bacterium BACL3 MAG-120507-bin80"> 11 <OrganismName>OM182 bacterium BACL3 MAG-120507-bin80</OrganismName> 12 </Organism> 13 <Comment> 14 <Paragraph>Genome assembled from metagenome sample SAMN03351369</Paragraph> 15 </Comment> 16 </Description> 17 <Owner> 18 <Name>Kungliga Tekniska Hogskolan, Science for Life Laboratory</Name> 19 <Contacts> 20 <Contact email="luisa.hugerth@scilifelab.se"> 21 <Name> 22 <First>Luisa</First> 23 <Last>Hugerth</Last> 24 <Middle>W</Middle> 25 </Name> 26 </Contact> 27 </Contacts> 28 </Owner> 29 <Models> 30 <Model>Microbe, viral or environmental</Model> 31 </Models> 32 <Package display_name="Microbe; version 1.0">Microbe.1.0</Package> 33 <Attributes> 34 <Attribute attribute_name="strain" harmonized_name="strain" display_name="strain">not applicable</Attribute> 35 <Attribute attribute_name="Salinity (PSU)">6.6</Attribute> 36 <Attribute attribute_name="depth" harmonized_name="depth" display_name="depth">2</Attribute> 37 <Attribute attribute_name="env_biome" harmonized_name="env_broad_scale" display_name="broad-scale environmental context">Brackish water</Attribute> 38 <Attribute attribute_name="collection_date" harmonized_name="collection_date" display_name="collection date">07-May-2012</Attribute> 39 <Attribute attribute_name="temp" harmonized_name="temp" display_name="temperature">5.6</Attribute> 40 <Attribute attribute_name="environmental-sample">TRUE</Attribute> 41 <Attribute attribute_name="sample_type" harmonized_name="sample_type" display_name="sample type">Metagenomic Assembly</Attribute> 42 <Attribute attribute_name="isolation_source" harmonized_name="isolation_source" display_name="isolation source">surface water</Attribute> 43 <Attribute attribute_name="geo_loc_name" harmonized_name="geo_loc_name" display_name="geographic location">Baltic Sea</Attribute> 44 <Attribute attribute_name="lat_lon" harmonized_name="lat_lon" display_name="latitude and longitude">56.930850 N 17.060667 E</Attribute> 45 <Attribute attribute_name="metagenomic">TRUE</Attribute> 46 <Attribute attribute_name="metagenome-source">marine metagenome</Attribute> 47 <Attribute attribute_name="isolation-source" harmonized_name="isolation_source" display_name="isolation source">brackish water collected from the surface of the Baltic Sea</Attribute> 48 <Attribute attribute_name="isolate" harmonized_name="isolate" display_name="isolate">BACL4 MAG-120507-bin80</Attribute> 49 </Attributes> 50 <Links> 51 <Link target="bioproject" type="entrez" label="PRJNA273799">273799</Link> 52 </Links> 53 <Status status="live" when="2015-11-08T06:44:46.013"/> 54</BioSample> 55<BioSample access="public" publication_date="2015-11-08T06:44:46.060" last_update="2015-11-08T06:44:46.060" submission_date="2015-05-28T09:58:39.470" id="3741963" accession="SAMN03741963"> 56 <Ids> 57-- 58 <Ids> 59 <Id db="BioSample" is_primary="1">SAMN03741963</Id> 60 <Id db_label="Sample name">BACL4 MAG-120813-bin39</Id> 61 </Ids> 62 <Description> 63 <Title>Microbe sample from Actinobacteria bacterium BACL4 MAG-120813-bin39</Title> 64 <Organism taxonomy_id="1655578" taxonomy_name="Actinobacteria bacterium BACL4 MAG-120813-bin39"> 65 <OrganismName>Actinobacteria bacterium BACL4 MAG-120813-bin39</OrganismName> 66 </Organism> 67 <Comment> 68 <Paragraph>Genome assembled from metagenome sample SAMN03351393</Paragraph> 69 </Comment> 70 </Description> 71 <Owner> 72 <Name>Kungliga Tekniska Hogskolan, Science for Life Laboratory</Name> 73 <Contacts> 74 <Contact email="luisa.hugerth@scilifelab.se"> 75 <Name> 76 <First>Luisa</First> 77 <Last>Hugerth</Last> 78 <Middle>W</Middle> 79 </Name> 80 </Contact> 81 </Contacts> 82 </Owner> 83 <Models> 84 <Model>Microbe, viral or environmental</Model> 85 </Models> 86 <Package display_name="Microbe; version 1.0">Microbe.1.0</Package> 87 <Attributes> 88 <Attribute attribute_name="strain" harmonized_name="strain" display_name="strain">not applicable</Attribute> 89 <Attribute attribute_name="Salinity (PSU)">6.2</Attribute> 90 <Attribute attribute_name="depth" harmonized_name="depth" display_name="depth">2</Attribute> 91 <Attribute attribute_name="env_biome" harmonized_name="env_broad_scale" display_name="broad-scale environmental context">Brackish water</Attribute> 92 <Attribute attribute_name="collection_date" harmonized_name="collection_date" display_name="collection date">13-Aug-2012</Attribute> 93 <Attribute attribute_name="temp" harmonized_name="temp" display_name="temperature">18.2</Attribute> 94 <Attribute attribute_name="environmental-sample">TRUE</Attribute> 95 <Attribute attribute_name="sample_type" harmonized_name="sample_type" display_name="sample type">Metagenomic Assembly</Attribute> 96 <Attribute attribute_name="isolation_source" harmonized_name="isolation_source" display_name="isolation source">surface water</Attribute> 97 <Attribute attribute_name="geo_loc_name" harmonized_name="geo_loc_name" display_name="geographic location">Baltic Sea</Attribute> 98 <Attribute attribute_name="lat_lon" harmonized_name="lat_lon" display_name="latitude and longitude">56.930850 N 17.060667 E</Attribute> 99 <Attribute attribute_name="metagenomic">TRUE</Attribute> 100 <Attribute attribute_name="metagenome-source">marine metagenome</Attribute> 101 <Attribute attribute_name="isolation-source" harmonized_name="isolation_source" display_name="isolation source">brackish water collected from the surface of the Baltic Sea</Attribute> 102 <Attribute attribute_name="isolate" harmonized_name="isolate" display_name="isolate">BACL4 MAG-120813-bin39</Attribute> 103 </Attributes> 104 <Links> 105 <Link target="bioproject" type="entrez" label="PRJNA273799">273799</Link> 106 </Links> 107 <Status status="live" when="2015-11-08T06:44:46.060"/> 108</BioSample>

試したこと

grep、sed、xpath による抽出

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

xpathの書き方なら、下記の記事をご参照ください。
お役に立てれば幸いです。
XPathとは?基本概念や書き方をわかりやすく解説!

投稿2020/06/23 09:03

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

こういう要件を満たす最も合理的な方法の一つは、Xpathというものを利用することです。

perlやgrepやsedのみでこれをやろうとするのはかなり大変です。改行の入れ方などに厳密な法則性があることがわかっているとき、狙ったキーワードが特定のタグの下以外には絶対に現れないことがわかっているなど、何らかの前提条件があれば別ですが、一般論としては実用的とは言えません。

投稿2020/06/20 06:41

KojiDoi

総合スコア13671

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kakuko

2020/06/20 06:45

ありがとうございます。まずは教えていただいたリンク先の Xpath を使って試してみようと思います。
kakuko

2020/06/20 07:04

度々申し訳ございません。先ほど xmllint などをダウンロードしましたが、これを使った場合、上記のようにキーワードと照合しながら属性を抽出して出力する場合、コマンドをどのように組み合わせればよろしいでしょうか?(もし新たな質問を立てるべきでしたら、そのようにいたします)
KojiDoi

2020/06/20 07:12

ツールの使い方とxpathの文法をそれぞれチェックする必要があります。 > コマンドをどのように組み合わせればよろしいでしょうか? 最初に示したQiitaの記事の最初に例が書いてあります。これを参考に、「必ず含まれているはずの要素」(たとえばIdなど)をまずコマンドラインで抽出してみてください。複雑な条件判定や親子要素の探索などはそのあとです。
kakuko

2020/06/20 07:17

早速にご返事を有難うございます。まずは要素の抽出を試みます。
kakuko

2020/06/20 09:10

申し訳ありませんが、子要素の関係をまだ理解できていないためにIDなどを抽出することさえもできておらず、引き続き資料を見ながら勉強します。また、質問そのものが分かりにくい内容でしたので、今一度修正しました。他の方からコメントいただけましたら、あわせて確認してまいります。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問