-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSiftFeedXml.ps1
More file actions
69 lines (60 loc) · 2.19 KB
/
SiftFeedXml.ps1
File metadata and controls
69 lines (60 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
param (
[Parameter(Mandatory = $true)]
[string]$xmlUrl
)
# 🌐 Download XML
try {
Write-Host "🌐 Downloading feed from: $xmlUrl"
$response = Invoke-WebRequest -Uri $xmlUrl -UseBasicParsing
[xml]$originalXml = $response.Content
}
catch {
Write-Error "❌ Failed to download or parse XML from $xmlUrl"
exit 1
}
# 🧠 Structure definition (same as CSV script)
$feedStructure = @{
rootPath = "//item"
nestedRepeats = @{
selling_prices = @("currency", "base_price", "discount_price")
}
flatFields = @("ean", "availability", "availability_count")
}
# 🏗️ Create a new XML document to hold the stripped version
$strippedXml = New-Object System.Xml.XmlDocument
$itemsNode = $strippedXml.CreateElement("items")
$strippedXml.AppendChild($itemsNode) | Out-Null
# 🧹 Process each item
foreach ($item in $originalXml.SelectNodes($feedStructure.rootPath)) {
$newItem = $strippedXml.CreateElement("item")
# Copy flat fields
foreach ($field in $feedStructure.flatFields) {
$value = $item.SelectSingleNode($field)?.InnerText
if ($value) {
$element = $strippedXml.CreateElement($field)
$element.InnerText = $value
$newItem.AppendChild($element) | Out-Null
}
}
# Copy nested repeat fields
foreach ($nestedPath in $feedStructure.nestedRepeats.Keys) {
$subnodes = $item.SelectNodes($nestedPath)
foreach ($subnode in $subnodes) {
$subElement = $strippedXml.CreateElement($nestedPath)
foreach ($nestedField in $feedStructure.nestedRepeats[$nestedPath]) {
$val = $subnode.SelectSingleNode($nestedField)?.InnerText
if ($val) {
$fieldElement = $strippedXml.CreateElement($nestedField)
$fieldElement.InnerText = $val
$subElement.AppendChild($fieldElement) | Out-Null
}
}
$newItem.AppendChild($subElement) | Out-Null
}
}
$itemsNode.AppendChild($newItem) | Out-Null
}
# 💾 Save the stripped XML
$outputXmlPath = "stripped-output.xml"
$strippedXml.Save($outputXmlPath)
Write-Host "✅ Stripped XML saved to: $outputXmlPath"