2017-11-16 8 views
-1

でXMLノードを更新し、私は私が更新する必要があり、私のXML文書内の次のXMLノードがあります。以下では検索とPowerShellの

<ImageUrl>xxx.xxx.com/imageeditor/X.jpg</ImageUrl> 

を:

<ImageUrl>xxx.xxx.com/image/getdvimage/X/1/false/200/200/</ImageUrl> 

「X」で最初の文字列は数値(たとえば9808)であり、2番目のノードでは「X」の位置にある必要があります。これは、ドキュメント内のImageUrlという唯一のノードであり、この方法でドキュメント全体を更新する必要があります。だから私は、ノード内のXを見つけて、更新されたノードのXで置き換えてファイルを保存するのを助ける必要があります。私は新しい文字列に挿入するモデル番号を引くことができれば{0}私はないでしょう

<Product> 
      <ExternalId>2127</ExternalId> 
      <Name>test</Name> 
           <Description> 
       <![CDATA[ 
       desc 
       ]]></Description> 
      <BrandExternalId>xxx</BrandExternalId> 
      <CategoryExternalId>Foo</CategoryExternalId> 
           <ProductPageUrl><![CDATA[xxx.com/2127]]></ProductPageUrl> 
       <ImageUrl>xxx.com/imageeditor/2127.jpg</ImageUrl> 
      <ModelNumbers> 
       <ModelNumber>2127</ModelNumber> 
      </ModelNumbers> 
      <ManufacturerPartNumbers> 
       <ManufacturerPartNumber>2127</ManufacturerPartNumber> 
      </ManufacturerPartNumbers> 
      <UPCs> 
       <UPC>841101002711</UPC> 
      </UPCs> 
     </Product> 

:私は使用していますXMLをここで

[xml]$xml = Get-Content 'D:\temp\feed.xml' 
Foreach ($item in (Select-XML -Xml $xml -XPath '//ImageUrl')) 
{ 
    $oldValue = $item.node.innertext #need to get the value from the last "/" to the .jpg value 
    $newValue = [string]::Format('xxx.xxx.com/image/getdvimage/{0}/1/false/200/200/',$oldValue) #place the $oldValue into the new string here 

    $item.node.Name = ??? 
} 
$NewPath = 'D:\temp\FeedUpd.xml' 
$xml.Save($NewPath) 

さ:

私はこのコードをしようとしています古いImageUrlを解析する必要があります。モデルを文字列に挿入するだけです。どのようにしてループ内の<ExternalId><ImageUrl>を置き換えるのですか?

+3

? –

+0

Foreach(Select-XML -Xml $ xml -XPath '// ImageUrl')の$ item) { $ item.node.Name = ??? } $ NewPath = 'D:\ temp \ FeedUpd.xml' $ xml.Save($ NewPath) このコードを実行すると、テキストを取得して試してみるためのImageUrlタグが見つかりません交換する。 – john

+0

上記のコードを実行すると、Foreachループに入ることはなく、$ itemには値は決してありません – john

答えて

0

私はこれを行う必要があったので、私はちょうどデータをプルしてすぐに私のためにそれを解析するコンソールアプリケーションをC#で作成しました。私はXDocumentとSelectNodesを使ってデータを取り込みました。

XmlDocument doc = new XmlDocument(); 
     doc.Load(@"d:\temp\xxx.xml"); 
     XmlNode docElement = doc.DocumentElement; 
     XmlNamespaceManager nsman = new XmlNamespaceManager(doc.NameTable); 
     nsman.AddNamespace("ns", docElement.NamespaceURI); 

     foreach (XmlNode row in doc.SelectNodes("//ns:Product", nsman)) 
     { 
      string prodID = null; 
      string ImageUrl = null; 
      foreach (XmlNode childNode in row.ChildNodes) 
      { 
       //Process the node data here 
      } 
     } 
     doc.Save(@"d:\temp\xxx.xml"); 

私の530行を1秒未満で処理しました。 <ImageUrl>要素を更新するための

0

あなたの正確な要件は不明であるが、ここでは(.ForEach()収集オペレータの使用によるものPSV4 +、)必要なすべての技術を示して何か:あなたがこれまでに試してみました何

$inFile = 'D:\temp\feed.xml' 
$outFile = 'D:\temp\feed-updated.xml' 

# Read the input file into an in-memory XML document (object model). 
$xml = [xml] (Get-Content -Raw $inFile) 

# Loop over all <Product> elements (anywhere in the document) 
# Note: Since we already have an in-memory XML document, using method 
#  .SelectNodes() is more efficient than using the Select-Xml cmdlet. 
#  Similarly, the use of collection operator .ForEach() is more efficient 
#  than use of the ForEach-Object cmdlet. 
$xml.SelectNodes('//Product').ForEach({ 

    # $_ is the <Product> element at hand, and its child elements 
    # can simply be accessed with *dot notation*. 

    # Get the model number... 
    $modelNo = $_.ModelNumbers.ModelNumber 

    # ... and use it to update the <ImageUrl> child element 
    $_.ImageUrl = 'xxx.xxx.com/image/getdvimage/{0}/1/false/200/200/' -f $modelNo 

}) 

# Save the modified in-memory XML document back to disk. 
# Note: 
# * Creates a file with BOM-less UTF-8 encoding. 
# * Be sure to use an *absolute* file path, because the .NET framework's 
#  current directory typically differs from PowerShell's. 
$xml.Save($outFile) 
関連する問題