{"id":930,"date":"2021-03-05T19:01:07","date_gmt":"2021-03-05T10:01:07","guid":{"rendered":"http:\/\/www.opentone.co.jp\/ot-lab\/?p=930"},"modified":"2025-05-12T10:06:19","modified_gmt":"2025-05-12T01:06:19","slug":"shellscript-xml","status":"publish","type":"post","link":"https:\/\/www.opentone.co.jp\/ot-lab\/all\/web-system\/shellscript-xml","title":{"rendered":"ShellScript\u3067XML\u306e\u5185\u5bb9\u3092\u53d6\u308a\u51fa\u3059"},"content":{"rendered":"\n<p>\u3053\u3093\u306b\u3061\u306f\u3002itou\u3067\u3059\u3002<br>ShellScript\uff08\u30b7\u30a7\u30eb\u30b9\u30af\u30ea\u30d7\u30c8\uff09\u3067XML\u3092\u30d1\u30fc\u30b9\u3057\u3066\u5185\u5bb9\u3092\u53d6\u308a\u51fa\u3059TIPS\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u74b0\u5883\u306fLinux\uff08CentOS6.5\uff09\u3067\u691c\u8a3c\u3057\u307e\u3059\u3002<br>\u30c4\u30fc\u30eb\u306f\u300cxmllint\u300d\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<br>CentOS6.5\u3060\u3068\u300c\/usr\/bin\/xmllint\u300d\u306b\u30d7\u30ea\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u307e\u3059\u3002<br>xmllint\u306e\u672c\u5bb6\u30b5\u30a4\u30c8\u306f\u300c<a href=\"http:\/\/xmlsoft.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/xmlsoft.org\/<\/a>\u300d\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u307e\u305a\u3001\u30b5\u30f3\u30d7\u30eb\u306eXML\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u30fbsample.xml<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[code]\n&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;sample&gt;\n  &lt;food&gt;\n    &lt;name language=\"Japanese\"&gt;\u308a\u3093\u3054&lt;\/name&gt;\n    &lt;amount&gt;105&lt;\/amount&gt;\n  &lt;\/food&gt;\n  &lt;food&gt;\n    &lt;name language=\"English\"&gt;grape&lt;\/name&gt;\n    &lt;amount&gt;210&lt;\/amount&gt;\n  &lt;\/food&gt;\n&lt;\/sample&gt;\n[\/code]<\/pre>\n\n\n\n<p>xmllint\u3067\u30b5\u30f3\u30d7\u30ebXML\u30d5\u30a1\u30a4\u30eb\u3092\u53c2\u7167\u3057\u307e\u3059\u3002<br>&#8220;cat&#8221;\u3067XML\u306e\u5168\u5185\u5bb9\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[shell]\n$ xmllint --shell sample.xml\n\/ &gt; cat\n\n&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;sample&gt;\n  &lt;food&gt;\n    &lt;name language=\"Japanese\"&gt;\u308a\u3093\u3054&lt;\/name&gt;\n    &lt;amount&gt;105&lt;\/amount&gt;\n  &lt;\/food&gt;\n  &lt;food&gt;\n    &lt;name language=\"English\"&gt;grape&lt;\/name&gt;\n    &lt;amount&gt;210&lt;\/amount&gt;\n  &lt;\/food&gt;\n&lt;\/sample&gt;\n[\/shell]<\/pre>\n\n\n\n<p>\u300csample\u300d\u2192\u300cfood\u300d\u2192\u300cname\u300d\u8981\u7d20\u306e\u5185\u5bb9\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[shell]\n\/ &gt; cat \/sample\/food\/name\n -------\n&lt;name language=\"Japanese\"&gt;\u308a\u3093\u3054&lt;\/name&gt;\n -------\n&lt;name language=\"English\"&gt;grape&lt;\/name&gt;\n[\/shell]<\/pre>\n\n\n\n<p>2\u756a\u76ee\u306e\u300cfood\u300d\u8981\u7d20\u306b\u5b9a\u7fa9\u3057\u3066\u3044\u308b\u300cname\u300d\u8981\u7d20\u306e\u5185\u5bb9\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[shell]\n\/ &gt; cat \/sample\/food[2]\/name\n&lt;name language=\"English\"&gt;grape&lt;\/name&gt;\n[\/shell]<\/pre>\n\n\n\n<p>\u300clanguage\u300d\u5c5e\u6027\u306e\u5185\u5bb9\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[shell]\n\/ &gt; cat \/sample\/food\/name\/@language\n -------\n language=\"Japanese\"\n -------\n language=\"English\"\n[\/shell]<\/pre>\n\n\n\n<p>ShellScript\u3067\u53d6\u5f97\u3059\u308b\u5834\u5408\u306a\u3069\u3001\u975e\u5bfe\u8a71\u3067\u5b9f\u65bd\u3059\u308b\u5834\u5408\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[shell]\n$ echo \"cat \/sample\/food\/name\" | xmllint --shell sample.xml\n\/ &gt;  -------\n&lt;name language=\"Japanese\"&gt;\u308a\u3093\u3054&lt;\/name&gt;\n -------\n&lt;name language=\"English\"&gt;grape&lt;\/name&gt;\n[\/shell]<\/pre>\n\n\n\n<p>\u5404\u9805\u76ee\u3092xmllint\u306b\u3066\u53d6\u5f97\u3057\u6574\u5f62\u3057\u3066\u8868\u793a\u3059\u308bshell\u3092\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002<br>xmllint\u306b\u3088\u308aXML\u8981\u7d20\u306e\u5185\u5bb9\u3092\u53d6\u5f97\u3057\u3001sed\u3067\u6574\u5f62\u3057\u3066\u3044\u307e\u3059\u3002<br>\u30fbsample.sh<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[shell]\n#!\/bin\/sh\ncount=1\nwhile :\ndo\n  # \u98df\u3079\u7269\u306e\u540d\u79f0\u3092\u53d6\u5f97\n  name_xml=`echo \"cat \/sample\/food[${count}]\/name\" | xmllint --shell sample.xml`\n\n  # \u98df\u3079\u7269\u306e\u540d\u79f0\u306e\u8a00\u8a9e\u3092\u53d6\u5f97\n  language_xml=`echo \"cat \/sample\/food[${count}]\/name\/@language\" | xmllint --shell sample.xml`\n\n  # \u98df\u3079\u7269\u306e\u91d1\u984d\u3092\u53d6\u5f97\n  amount_xml=`echo \"cat \/sample\/food[${count}]\/amount\" | xmllint --shell sample.xml`\n\n  # \u5168\u3066\u53d6\u5f97\u3057\u305f\u5834\u5408\u3001\u30eb\u30fc\u30d7\u3092\u629c\u3051\u308b\n  if [  \"${name_xml}\" = \"\/ &gt; \/ &gt; \" ]; then\n    break\n  fi\n\n  # \u53d6\u5f97\u5185\u5bb9\u3092\u6574\u5f62\n  language=`echo ${language_xml} | sed -e \"s\/^.*language=\\\"\\(.*\\)\\\".*$\/\\1\/\"`\n  name=`echo ${name_xml} | sed -e \"s\/^.*&lt;name.*&gt;\\(.*\\)&lt;\\\/name&gt;.*$\/\\1\/\"`\n  amount=`echo ${amount_xml} | sed -e \"s\/^.*&lt;amount.*&gt;\\(.*\\)&lt;\\\/amount&gt;.*$\/\\1\/\"`\n\n  # \u53d6\u5f97\u7d50\u679c\u3092\u8868\u793a\n  echo ${name}\"(\"${language}\")\"\u306e\u91d1\u984d\u306f\"${amount}\u5186\"\n\n  count=${count}+1\ndone\n[\/shell]<\/pre>\n\n\n\n<p>\u5b9f\u884c\u7d50\u679c\u3067\u3059<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[code]\n\u308a\u3093\u3054(Japanese)\u306e\u91d1\u984d\u306f105\u5186\ngrape(English)\u306e\u91d1\u984d\u306f210\u5186\n[\/code]<\/pre>\n\n\n\n<p>ShellScript\u3067XML\u306e\u5185\u5bb9\u3092\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408<br>xmllint\u306f\u9078\u629e\u80a2\u306e\u4e00\u3064\u306b\u306a\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<div class=\"opent-_2\" style=\"margin-top: 10px;margin-right: 10px;margin-bottom: 10px;margin-left: 10px;\" id=\"opent-771136622\"><a href=\"https:\/\/www.opentone.co.jp\/backlog\/\" aria-label=\"twitter_backlogskt_new\"><img src=\"https:\/\/www.opentone.co.jp\/ot-lab\/wp-content\/uploads\/sites\/11\/2023\/02\/twitter_backlogskt_new.jpg\" alt=\"\"  srcset=\"https:\/\/www.opentone.co.jp\/ot-lab\/wp-content\/uploads\/sites\/11\/2023\/02\/twitter_backlogskt_new.jpg 1024w, https:\/\/www.opentone.co.jp\/ot-lab\/wp-content\/uploads\/sites\/11\/2023\/02\/twitter_backlogskt_new-300x157.jpg 300w, https:\/\/www.opentone.co.jp\/ot-lab\/wp-content\/uploads\/sites\/11\/2023\/02\/twitter_backlogskt_new-768x401.jpg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" width=\"384\" height=\"200\"   \/><\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3002itou\u3067\u3059\u3002ShellScript\uff08\u30b7\u30a7\u30eb\u30b9\u30af\u30ea\u30d7\u30c8\uff09\u3067XML\u3092\u30d1\u30fc\u30b9\u3057\u3066\u5185\u5bb9\u3092\u53d6\u308a\u51fa<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[28],"tags":[8],"class_list":["post-930","post","type-post","status-publish","format-standard","hentry","category-web-system","tag-blog",""],"acf":[],"aioseo_notices":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"engage-mag-carousel-img":false,"engage-mag-carousel-img-landscape":false,"engage-mag-carousel-large-img":false,"engage-mag-carousel-large-img-landscape":false,"engage-mag-large-thumb":false,"engage-mag-small-thumb":false},"uagb_author_info":{"display_name":"ah106rx4o4","author_link":"https:\/\/www.opentone.co.jp\/ot-lab\/author\/ah106rx4o4"},"uagb_comment_info":0,"uagb_excerpt":"\u3053\u3093\u306b\u3061\u306f\u3002itou\u3067\u3059\u3002ShellScript\uff08\u30b7\u30a7\u30eb\u30b9\u30af\u30ea\u30d7\u30c8\uff09\u3067XML\u3092\u30d1\u30fc\u30b9\u3057\u3066\u5185\u5bb9\u3092\u53d6\u308a\u51fa","_links":{"self":[{"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/posts\/930","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/comments?post=930"}],"version-history":[{"count":1,"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/posts\/930\/revisions"}],"predecessor-version":[{"id":932,"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/posts\/930\/revisions\/932"}],"wp:attachment":[{"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/media?parent=930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/categories?post=930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/tags?post=930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}