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

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

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

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

PowerShell

Windows PowerShellはコマンドラインインターフェースであり、システム管理を含むWindowsタスク自動化のためのスクリプト言語です。

Q&A

解決済

3回答

2811閲覧

PowerShellで、XMLファイルにタグを追加し、項目毎で囲いたい。

mie.8

総合スコア28

XML

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

PowerShell

Windows PowerShellはコマンドラインインターフェースであり、システム管理を含むWindowsタスク自動化のためのスクリプト言語です。

0グッド

0クリップ

投稿2017/06/26 01:44

編集2017/06/28 06:08

前提・実現したい事

①PowerShellを使用し、SharePoint Online上のリストを、「XMLファイル」でローカルに保存する。。
(*プログラム作成済)

【保存したXMLファイル】

XML

1<?xml version= "1.0"?> 2<catalog> 3 4 <book id= "bk101"> 5 <author>Gambardella, Matthew</author> 6 <title>XML Developer's Guide</title> 7 <genre>Computer</genre> 8 <price>44.95</price> 9 <publish_date>2000-10-01</publish_date> 10 <description>An in-depth look at creating applicationswith XML.</description> 11 </book> 12 13 <book id= "bk102"> 14 <author>Ralls, Kim</author> 15 <title>Midnight Rain</title> 16 <genre>Fantasy</genre> 17 <price>5.95</price> 18 <publish_date>2000-12-16</publish_date> 19 <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description> 20 </book> 21 22 <book id= "bk103"> 23 <author>Corets, Eva</author> 24 <title>Maeve Ascendant</title> 25 <genre>Fantasy</genre> 26 <price>5.95</price> 27 <publish_date>2000-11-17</publish_date> 28 <description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description> 29 </book> 30 31 <book id= "bk104"> 32 <author>Corets, Eva</author> 33 <title>Oberon's Legacy</title> 34 <genre>Fantasy</genre> 35 <price>5.95</price> 36 <publish_date>2001-03-10</publish_date> 37 <description>In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.</description> 38 </book> 39 40 <book id= "bk105"> 41 <author>Corets, Eva</author> 42 <title>The Sundered Grail</title> 43 <genre>Fantasy</genre> 44 <price>5.95</price> 45 <publish_date>2001-09-10</publish_date> 46 <description>The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.</description> 47 </book> 48 49 <book id= "bk106"> 50 <author>Randall, Cynthia</author> 51 <title>Lover Birds</title> 52 <genre>Romance</genre> 53 <price>4.95</price> 54 <publish_date>2000-09-02</publish_date> 55 <description>When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.</description> 56 </book> 57 58 <book id= "bk107"> 59 <author>Thurman, Paula</author> 60 <title>Splish Splash</title> 61 <genre>Romance</genre> 62 <price>4.95</price> 63 <publish_date>2000-11-02</publish_date> 64 <description>A deep sea diver finds true love twenty thousand leagues beneath the sea.</description> 65 </book> 66 67 <book id= "bk108"> 68 <author>Knorr, Stefan</author> 69 <title>Creepy Crawlies</title> 70 <genre>Horror</genre> 71 <price>4.95</price> 72 <publish_date>2000-12-06</publish_date> 73 <description>An anthology of horror stories about roaches, centipedes, scorpions and other insects.</description> 74 </book> 75 76 <book id= "bk109"> 77 <author>Kress, Peter</author> 78 <title>Paradox Lost</title> 79 <genre>Science Fiction</genre> 80 <price>6.95</price> 81 <publish_date>2000-11-02</publish_date> 82 <description>After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum.</description> 83 </book> 84 85 <book id= "bk110"> 86 <author>O'Brien, Tim</author> 87 <title>Microsoft .NET: The Programming Bible</title> 88 <genre>Computer</genre> 89 <price>36.95</price> 90 <publish_date>2000-12-09</publish_date> 91 <description>Microsoft's .NET initiative is explored in detail in this deep programmer's reference.</description> 92 </book> 93 94 <book id= "bk111"> 95 <author>O'Brien, Tim</author> 96 <title>MSXML3: A Comprehensive Guide</title> 97 <genre>Computer</genre> 98 <price>36.95</price> 99 <publish_date>2000-12-01</publish_date> 100 <description>The Microsoft MSXML3 parser is covered in detail, with attention to XML DOM interfaces, XSLT processing, SAX and more.</description> 101 </book> 102 103 <book id= "bk112"> 104 <author>Galos, Mike</author> 105 <title>Visual Studio 7: A Comprehensive Guide</title> 106 <genre>Computer</genre> 107 <price>49.95</price> 108 <publish_date>2001-04-16</publish_date> 109 <description>Microsoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C++, C#, and ASP+ are integrated into a comprehensive development environment.</description> 110 </book> 111</catalog>


②保存したXMLファイルの中には、不要な項目(タグ)があるので、PowerShellを使い必要なタグのみ抽出。(別名で保存)
(*プログラム作成済)

【作成したプログラム】

PowerShell

1# 変数宣言 2$XmlFilePath = "XMLファイル保存先パス" 3$XmlSavePath = "保存先パス" 4$TagName = "'title','genre','price'" # 抜き出したいタグ名 5 6# XMLファイル読み込み 7$xml = [xml](Get-Content $XmlFilePath) 8 9# XMLタグ抜き出し 10$CustomXML = $xml.catalog.book | 11 ForEach-Object { 12 $_[$TagName].OuterXml 13 } 14 15# ファイル出力(文字コード:UTF8) 16$CustomXML | Out-File $XmlSavePath -Encoding UTF8

【必要なタグのみ抽出したXMLファイル】抽出するタグ名 → title, genre, price

XML

1<title>XML Developer's Guide</title> 2<genre>Computer</genre> 3<price>44.95</price> 4<title>Midnight Rain</title> 5<genre>Fantasy</genre> 6<price>5.95</price> 7<title>Maeve Ascendant</title> 8<genre>Fantasy</genre> 9<price>5.95</price> 10<title>Oberon's Legacy</title> 11<genre>Fantasy</genre> 12<price>5.95</price> 13<title>The Sundered Grail</title> 14<genre>Fantasy</genre> 15<price>5.95</price> 16<title>Lover Birds</title> 17<genre>Romance</genre> 18<price>4.95</price> 19<title>Splish Splash</title> 20<genre>Romance</genre> 21<price>4.95</price> 22<title>Creepy Crawlies</title> 23<genre>Horror</genre> 24<price>4.95</price> 25<title>Paradox Lost</title> 26<genre>Science Fiction</genre> 27<price>6.95</price> 28<title>Microsoft .NET: The Programming Bible</title> 29<genre>Computer</genre> 30<price>36.95</price> 31<title>MSXML3: A Comprehensive Guide</title> 32<genre>Computer</genre> 33<price>36.95</price> 34<title>Visual Studio 7: A Comprehensive Guide</title> 35<genre>Computer</genre> 36<price>49.95</price>


③必要なタグのみ抽出したXMLファイルを開くと、必要な項目(タグ)のみ抽出されてはいるが、見づらいので
1アイテム毎にタグでくくり、見やすくしたい。
*(プログラム作成中。不明点)

【この様なXMLファイルにしたい】

XML

1 2<data> 3 <title>XML Developer's Guide</title> 4 <genre>Computer</genre> 5 <price>44.95</price> 6</data> 7<data> 8 <title>Midnight Rain</title> 9 <genre>Fantasy</genre> 10 <price>5.95</price> 11</data> 12<data> 13 <title>Maeve Ascendant</title> 14 <genre>Fantasy</genre> 15 <price>5.95</price> 16</data> 17<data> 18 <title>Oberon's Legacy</title> 19 <genre>Fantasy</genre> 20 <price>5.95</price> 21</data> 22<data> 23 <title>The Sundered Grail</title> 24 <genre>Fantasy</genre> 25 <price>5.95</price> 26</data> 27<data> 28 <title>Lover Birds</title> 29 <genre>Romance</genre> 30 <price>4.95</price> 31</data> 32<data> 33 <title>Splish Splash</title> 34 <genre>Romance</genre> 35 <price>4.95</price> 36</data> 37<data> 38 <title>Creepy Crawlies</title> 39 <genre>Horror</genre> 40 <price>4.95</price> 41</data> 42<data> 43 <title>Paradox Lost</title> 44 <genre>Science Fiction</genre> 45 <price>6.95</price> 46</data> 47<data> 48 <title>Microsoft .NET: The Programming Bible</title> 49 <genre>Computer</genre> 50 <price>36.95</price> 51</data> 52<data> 53 <title>MSXML3: A Comprehensive Guide</title> 54 <genre>Computer</genre> 55 <price>36.95</price> 56</data> 57<data> 58 <title>Visual Studio 7: A Comprehensive Guide</title> 59 <genre>Computer</genre> 60 <price>49.95</price> 61</data>

発生している問題・Error Message

③のコード記述が分からず困っております。
もし、分かる方がいれば、よろしくお願い致します。。

実際のソースコード

試した事

補足情報(言語/FW/ツール等のVersion)

・PowerShell ver5.0
・SharePoint Online
・Windows 7

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

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

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

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

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

guest

回答3

0

自己解決

もう1度、XMLファイルを作成する所から考案し直したいと思います。
皆様、ありがとうございました。

投稿2017/07/12 00:15

mie.8

総合スコア28

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

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

0

Powershell のプログラムにこだわらなければ、xmllint の windows 版を利用するという手もあります。
使い方はこんな感じです。

cmd

1 2> xmllint --format ${inp_xml_path} > ${out_xml_path} 3

インストール方法は下記 URL を参考にしてください。
http://saj.co.jp/techinfo/htmls/xml-libxml2-install_windows_precompile.html

投稿2017/07/06 02:18

tukuroku

総合スコア234

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

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

mie.8

2017/07/07 01:18

ご回答ありがとうございます。 検討させて頂きます。
guest

0

②のプログラムは自作されていて、title, genre, priceは常にこの順番で規則正しく出力されるという前提で良いのでしょうか?
であれば、titleを出力する際は

"<data>`r`n"

を先頭にくっつけ、priceを出力する際は

"</data>`r`n"

をくっつけて出力するように、②のプログラムを変更するというのではいかがでしょうか。

投稿2017/06/28 03:15

KojiDoi

総合スコア13669

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

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

mie.8

2017/06/28 06:07

ご回答ありがとうございます。 ②のプログラムは作成済です。追記致しました。 ただ、「ForEach-Object」で抜き出したいタグを処理しており 自分もその様な考えになりましたが、書き方が分かりません。 よろしければ、アドバイスを頂ければ助かります。 申し訳ございませんが、よろしくお願い致します。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問