0

I try to convert xml from this:

<test>
    <sub ID="126754">
        <name>test</name>
    </sub>
    <sub ID="126769">
        <name>test2</name>
    </sub>
</test>

to this :

<test>
    <sub>
        <ID>126754</ID>
        <name>test</name>
    </sub>
    <sub>
        <ID>126769</ID>
        <name>test2</name>
    </sub>
</test>

I can read and loop in my file but I don't find how to convert ID=nnnnnn to <ID>nnnnnn</ID>

Pang
  • 9,564
  • 146
  • 81
  • 122
freakbn
  • 27
  • 5
  • 1
    This can be accomplished with XSLT. See [here](https://stackoverflow.com/questions/10645359/convert-xml-attributes-to-elements-xslt) for an example style sheet transform that changes attributes into elements, and [here](https://stackoverflow.com/questions/37024568/applying-xsl-to-xml-with-powershell-exception-calling-transform) for an example of using PowerShell to apply a style sheet. – Bacon Bits Sep 12 '18 at 19:21

1 Answers1

1

Try This

$newContent = @()
$test=gc C:\temp\xmll.xml

ForEach($Regel In $Text) {
  if($Regel -match "ID=\d{6}") {
    $newContent += "    <sub>"
    $newContent += "        <ID>$($Regel.Substring(8, 10))</ID>"

  } else {
    $newContent += $Regel
  }
}

$newContent
Kemal K.
  • 301
  • 2
  • 7