#From XML to object

1 messages ยท Page 1 of 1 (latest)

tribal niche
#

I would like to convert variable $1 to variable $2. see image.
I don't get it. To split "ID" and keep the "Name" in the same object
(I can not chance the XML file, it is close to 40k lines.)

[xml]$1='
<Blades>
<Blade ID="1_3;1_10;1_27;1_34" Name="Blade Type A" />
<Blade ID="1_{16-17};1_40" Name="Blade Type B" />
</Blades>
'

chilly lynx
#

you mean like this?

$2=([xml]'
<Blades>
<Blade ID="1_3;1_10;1_27;1_34" Name="Blade Type A" />
<Blade ID="1_{16-17};1_40" Name="Blade Type B" />
</Blades>
').Blades.Blade
#

oh wait no you dont

chilly lynx
#
[xml]$1 = '
<Blades>
<Blade ID="1_3;1_10;1_27;1_34" Name="Blade Type A" />
<Blade ID="1_{16-17};1_40" Name="Blade Type B" />
</Blades>
'
foreach ($blade in $1.Blades.Blade) {
    $blade.ID -replace '\{|\}' -split ';' | ForEach-Object {
        $prefix, $numbers = $_ -split '_'
        $numbers -split '-' | ForEach-Object {
            [PSCustomObject]@{
                ID   = "$($prefix)_$($_)"
                Name = $blade.Name
            }
        }
    }
}

i think that's what you want

tribal niche
#

Yes thanks you. @chilly lynx
{16-17} is a range and there is not always a prefix.
But I have the solution.

[xml]$1 = '
<Blades>
<Blade ID="1_3;1_10;1_27;1_34" Name="Blade Type A" />
<Blade ID="1_{16-17};1_40" Name="Blade Type B" />
</Blades>
'
foreach ($blade in $1.Blades.Blade) {
    $blade.ID -split ';' | ForEach-Object {
        $numbers = $_
        if ($numbers -match '_' ){
            $prefix , $numbers = $numbers -split '_'
            $prefix+="_"
        }
        if ($numbers -match '{|}' ){
            $start, $end = $numbers -split '-' -replace '{|}'
            $numbers = $start..$end
        }
        $numbers | ForEach-Object { 
            [PSCustomObject]@{
                ID   = $prefix+$_
                Name = $blade.Name
            }
        }
    }
}
chilly lynx
#

ah yea, i missed that it seems ๐Ÿ™‚
I should probably look over the code a bit more when writing at 2am ๐Ÿ˜›

uhm you dont really need the '+' in the $prefix assignment.

        $prefix+="_"

also you need to escape the { with \{
[regex]::Escape('{}')