As the Title describes, I want to set the InnerText of a new ChildNode to equal a hashtable value if the hashtable key matches the innertext of another XML Node.
XML Example:
<?xml version="1.0" encoding="UTF-8"?>
<ns2:scaleType xmlns:ns2="http://www.someURL.com/">
<languages>
<language language="1">English</language>
</languages>
<scales>
<scale>
<id>10708</id>
<scaleType>10003</scaleType>
<names>
<name id="0">Some Text</name>
<name id="1">
</name>
<name id="2">
</name>
<alternativeExportValues>
<alternativeExportValue id="0">
</alternativeExportValue>
</alternativeExportValues>
</scale>
<scale>
<id>10709</id>
<scaleType>10003</scaleType>
<names>
<name id="0">Some Text 2</name>
<name id="1">
</name>
<name id="2">
</name>
<alternativeExportValues>
<alternativeExportValue id="0">
</alternativeExportValue>
</alternativeExportValues>
</scale>
</scales>
<additionalScales />
</ns2:scaleType>
.CSV Example:
navn;altValue
Some Text;0001
Some Text 2;0002
# Hashtable containing scale name & alt exp value (key : value)
$exportValue = @{ }
# Import .csv file holding the data
$scaleData = Import-Csv -Path '.\myCsvFile.csv' -Delimiter ';' -Encoding UTF8 |
% {
$exportValue[$_.navn] = $_.altValue
}
# scales & list XML
$xmlFile = '.\myXmlFile.xml'
[Xml]$xml = Get-Content $xmlFile -Encoding UTF8
# xPath
$scaleName = $xml.SelectNodes('.//name[@id="0"]')
# Create child node to alternativeExportValues
$nodes = $xml.SelectNodes('.//alternativeExportValues') | ForEach-Object {
$newNode = $xml.CreateElement('alternativeExportValue')
$newNode.SetAttribute("id", "0")
$newNode.InnerText = "{{key value}}"
$_.AppendChild($newNode)
}
$xml.Save('.\Result.xml')
As you see in the script, I'm creating a childnode for each $nodes = $xml.SelectNodes('.//alternativeExportValues')
.
Here I want the $newNode.InnerText
to be the hashtable value to the corresponding hashtable key if it matches the InnerText of $scaleName = $xml.SelectSingleNode('.//name[@id="0"]')
.
Been experimenting with foreach($key in $exportValue.Keys)
and foreach($kvp in $exportValue.GetEnumerator())
but without any luck, the script basically runs forever...
Any help or guidance in the right direction is appreciated.
I tried to help with a generic answer, but need more information.
When submitting a question about XML navigation, please include a sample of the xml, with at least 2 nodes of the type you are targeting. In this case, we probably need the context of //scales/scale and //names/name. Might help to include a few lines of your .csv data.
This statement probably won't work:
But maybe you only intended it as a placeholder for the logic you're asking for help with. Please clarify or maybe offset that text to show it's literal.
You are creating the element correctly, but it looks like you might need to tighten up the context. Try something like this:
But make sure you're at the right context node in your xml to compare it, and also be sure that they literally match. If so, you should be able to use that InnerText as your index for the $exportValue hashtable.
Lots of guessing. More info will help. :)