Home > other >  How to create 10 copies of the XML file, each with a different value of the td tag- PowerShell scrip
How to create 10 copies of the XML file, each with a different value of the td tag- PowerShell scrip

Time:10-29

I just started learning PowerShell (literally - just a week and completely from tutorials and materials online :)) so don`t be harsh on me)

I would appreciate help and how to do it because I am stuck at this point.. So I have this template file, I created 10 copies of it but I have no idea how to change both td tag values "." with different name for Title and Artist in each copy.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <tr>
        <td>.</td>
        <td>.</td>
      </tr>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

This is how far I got but I would appreciate some help/advice/explanation for correct further steps..

[xml]$File= get-content "\MyXMLFile.xml"
1..10 |% { Copy-Item "\MyXMLFile.xml" "MyXMLFile$_.xml"}

$copiedFiles =@(Get-ChildItem -Path "XmlFileFolder" | % Name)

$exampleNode = $File.SelectNodes("//td[1]")
                                      
$TitleNodeArray = @('aa', 'bb', 'cc', etc..) #but I have no idea how to go on from there ..

CodePudding user response:

hmm, I don't work much with xml but I know of one-way which uses Select-Xml.

(Select-Xml -Xml $File -XPath '//tr/td')[1].Node."#text" = 'title'

Where [1] is the indexed node, and #text being the value/text itself.

CodePudding user response:

Try this. Add a loop to do 10 times.

using assembly System 
using assembly System.Linq
using assembly System.Xml.Linq 

$inputFilename = "c:\temp\test.xml"
$outputFilename = "c:\temp\test1.xml"
$xDoc = [System.Xml.Linq.XDocument]::Load($inputFilename)
$tds = $xDoc.Descendants("td").Foreach([System.Xml.Linq.XElement])
$td0 = $tds[0]
$td0.SetValue("abc")
$td1 = $tds[1]
$td1.SetValue("efg")
Write-Host $xDoc
$xDoc.Save($outputFilename)
  • Related