How to iterate XML values with Powershell and filter by key name?

huangapple go评论125阅读模式
英文:

How to iterate XML values with Powershell and filter by key name?

问题

我有一些看起来像这样的 XML:

  1. <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
  2. <FlowInstance xmlns="http://www.oeplatform.org/version/2.0/schemas/flowinstance"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oeplatform.org/version/2.0/schemas/flowinstance schema-1161.xsd">
  4. <Values>
  5. <donotoutput>
  6. <Name><![CDATA[donotoutput]]></Name>
  7. <Value><![CDATA[donotoutput value]]></Value>
  8. </donotoutput>
  9. <specifics_test1>
  10. <Name><![CDATA[Test1]]></Name>
  11. <Value>Val1</Value>
  12. <Value>Val2</Value>
  13. <Value>Val3</Value>
  14. </specifics_test1>
  15. <specifics_test2>
  16. <Name><![CDATA[Test2]]></Name>
  17. <Value><![CDATA[Test2 value]]></Value>
  18. </specifics_test2>
  19. <specifics_test3>
  20. <Name><![CDATA[Test3]]></Name>
  21. <Value><![CDATA[Test3 value]]></Value>
  22. </specifics_test3>
  23. </Values>
  24. </FlowInstance>

我想要遍历其中的 <Values> 部分,但只想处理键以 "specifics_" 开头的部分,我该如何做到?

我尝试了以下 PowerShell 代码:

  1. [xml]$xml = Get-Content -Path "C:\Temp\xml\xml_cleaned.xml"
  2. $xml.FlowInstance.Values
  3. $xml.FlowInstance.Values.specifics_test1

这样可以给我键和值,但我无法像这样使用循环:

  1. foreach ($val in $xml.FlowInstance.Values){
  2. $val
  3. }

我还想获取键,只解析以 "specifics_" 开头的键,但我无法像在 PowerShell 中使用哈希表那样使用 .Keys,在这个 XML 中我该如何做到?

英文:

I have som XML that looks like this

  1. <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
  2. <FlowInstance xmlns="http://www.oeplatform.org/version/2.0/schemas/flowinstance"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oeplatform.org/version/2.0/schemas/flowinstance schema-1161.xsd">
  4. <Values>
  5. <donotoutput>
  6. <Name><![CDATA[donotoutput]]></Name>
  7. <Value><![CDATA[donotoutput value]]></Value>
  8. </donotoutput>
  9. <specifics_test1>
  10. <Name><![CDATA[Test1]]></Name>
  11. <Value>Val1</Value>
  12. <Value>Val2</Value>
  13. <Value>Val3</Value>
  14. </specifics_test1>
  15. <specifics_test2>
  16. <Name><![CDATA[Test2]]></Name>
  17. <Value><![CDATA[Test2 value]]></Value>
  18. </specifics_test2>
  19. <specifics_test3>
  20. <Name><![CDATA[Test3]]></Name>
  21. <Value><![CDATA[Test3 value]]></Value>
  22. </specifics_test3>
  23. </Values>
  24. </FlowInstance>

and i want to iterate over the <Values> part of it but only the ones that the key start with "specifics_", how can i do that?

Ive tried this Powershell code

  1. [xml]$xml = Get-Content -Path "C:\Temp\xml\xml_cleaned.xml"
  2. $xml.FlowInstance.Values
  3. $xml.FlowInstance.Values.specifics_test1

gives me the keys and values, but i cant do a loop like this

  1. foreach ($val in $xml.FlowInstance.Values){
  2. $val
  3. }

and i would also like to get the keys to only parse the ones with "specifics_*" but i cant to that like an hashtable in Powershell with .Keys, how would i do that with this XML?

答案1

得分: 3

由于你正在处理 XML,一种方法是使用 XML 解析器:

  1. # 首先,声明命名空间
  2. $ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
  3. $ns.AddNamespace("xx", "http://www.oeplatform.org/version/2.0/schemas/flowinstance")
  4. # 注意,这里的 "xx" 只是默认命名空间的别名
  5. # 然后,遍历它们:
  6. $values = $xml.SelectNodes('//xx:*[starts-with(name(),"specifics_")]/xx:Value',$ns);
  7. foreach ($value in $values){
  8. echo $value
  9. }

以上代码使用 PowerShell 编写,它首先声明了命名空间,并使用 XML 解析器遍历了 XML 中以 "specifics_" 开头的元素的值。

英文:

Since you are dealing with xml, one way to do it is use an xml parser:

  1. #first, declare namespaces
  2. $ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
  3. $ns.AddNamespace("xx", "http://www.oeplatform.org/version/2.0/schemas/flowinstance")
  4. #note "xx" here is just a nickname for the default namespace
  5. #and now just iterate over them:
  6. $values = $xml.SelectNodes('//xx:*[starts-with(name(),"specifics_")]/xx:Value',$ns);
  7. foreach ($value in $values){
  8. echo $value
  9. }

答案2

得分: 0

Jack Fleeting的回答帮助了我,指导了我正确的方向并解决了我的问题。我希望在循环中也包含键,所以最终得到了以下代码:

  1. # 加载XML内容
  2. [xml]$xml = Get-Content -Path "C:\Temp\xml\xml_cleaned.xml"
  3. # 定义XML中使用的命名空间
  4. $ns = New-Object Xml.XmlNamespaceManager($xml.NameTable)
  5. $ns.AddNamespace("ns", "http://www.oeplatform.org/version/2.0/schemas/flowinstance")
  6. # 遍历Values元素
  7. $valuesElements = $xml.SelectNodes("//ns:FlowInstance/ns:Values/ns:*[starts-with(name(),'specifics_')]", $ns)
  8. foreach ($element in $valuesElements) {
  9. $keyName = $element.LocalName
  10. $name = $element.SelectSingleNode("ns:Name", $ns).InnerText
  11. $value = if ($element.SelectNodes("ns:Value", $ns).Count -eq 1) {
  12. $element.SelectNodes("ns:Value", $ns).InnerText
  13. } else {
  14. $element.SelectNodes("ns:Value", $ns) | ForEach-Object { $_.InnerText }
  15. }
  16. Write-Host "键名: $keyName"
  17. Write-Host "名称: $name"
  18. Write-Host "值: $value"
  19. Write-Host "-----------"
  20. }

希望对你有帮助!

英文:

Jack Fleeting's answer above helped me in the right direction and answered my problem. I wanted the key in my loop aswell so i ended up with the following code

  1. # Load the XML content
  2. [xml]$xml = Get-Content -Path "C:\Temp\xml\xml_cleaned.xml"
  3. # Define the namespace used in the XML
  4. $ns = New-Object Xml.XmlNamespaceManager($xml.NameTable)
  5. $ns.AddNamespace("ns", "http://www.oeplatform.org/version/2.0/schemas/flowinstance")
  6. # Iterate over the Values elements
  7. $valuesElements = $xml.SelectNodes("//ns:FlowInstance/ns:Values/ns:*[starts-with(name(),'specifics_')]", $ns)
  8. foreach ($element in $valuesElements) {
  9. $keyName = $element.LocalName
  10. $name = $element.SelectSingleNode("ns:Name", $ns).InnerText
  11. $value = if ($element.SelectNodes("ns:Value", $ns).Count -eq 1) {
  12. $element.SelectNodes("ns:Value", $ns).InnerText
  13. } else {
  14. $element.SelectNodes("ns:Value", $ns) | ForEach-Object { $_.InnerText }
  15. }
  16. Write-Host "Key Name: $keyName"
  17. Write-Host "Name: $name"
  18. Write-Host "Value(s): $value"
  19. Write-Host "-----------"
  20. }

huangapple
  • 本文由 发表于 2023年7月27日 17:39:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定