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>

1
  • 1
    This can be accomplished with XSLT. See here for an example style sheet transform that changes attributes into elements, and here for an example of using PowerShell to apply a style sheet. Commented Sep 12, 2018 at 19:21

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.