Powershell & XML: how to count specific elements for each node
Here is another one for my server for PC Game - Space Engineers. The answer to this question seems simple, but it got stuck as I cannot find the correct way to call this information.
What I would like to do is count the number of times a certain element appears in each node. This partially works for me, but not exactly what I want.
Here's what I have so far:
Excerpt from XML (nodes stacked except for target node)
<MyObjectBuilder_Sector xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Position>
<SectorEvents>
<AppVersion>
<SectorObjects>
<MyObjectBuilder_EntityBase xsi:type="MyObjectBuilder_CubeGrid">
<CubeBlocks>
<MyObjectBuilder_CubeBlock xsi:type="MyObjectBuilder_Reactor">
<MyObjectBuilder_CubeBlock xsi:type="MyObjectBuilder_Thrust">
<MyObjectBuilder_CubeBlock xsi:type="MyObjectBuilder_Drill">
<MyObjectBuilder_CubeBlock xsi:type="MyObjectBuilder_Drill">
<SubtypeName>SmallBlockDrill</SubtypeName>
<EntityId>72280681079646079</EntityId>
<Min x="1" y="1" z="-7" />
<BlockOrientation Forward="Forward" Up="Left" />
<ColorMaskHSV x="0" y="-1" z="0" />
<Owner>144256542526969420</Owner>
<ShareMode>None</ShareMode>
<ShowOnHUD>false</ShowOnHUD>
<Enabled>false</Enabled>
<Inventory>
<Items />
<nextItemId>0</nextItemId>
</Inventory>
</MyObjectBuilder_CubeBlock>
and my powershell code that returns me the number of cube blocks by
<MyObjectBuilder_EntityBase xsi:type="MyObjectBuilder_CubeGrid">
cube mesh.
$filePath = 'F:\DedicatedServer\DataDir\SE Survival 2\Saves\VPS RC 1\SANDBOX_0_0_0_.sbs'
[xml]$myXML = Get-Content $filePath
$ns = New-Object System.Xml.XmlNamespaceManager($myXML.NameTable)
$ns.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
$infos = $myXML.SelectNodes("//SectorObjects/MyObjectBuilder_EntityBase[(@xsi:type='MyObjectBuilder_CubeGrid')]" ,$ns)
foreach ($info in $infos ){
$info.CubeBlocks.MyObjectBuilder_CubeBlock.count
}
So I'm trying to get this to get the number of small drills back to a cubic grid. here in XML
<MyObjectBuilder_CubeBlock xsi:type="MyObjectBuilder_Drill">
<SubtypeName>SmallBlockDrill</SubtypeName>
It seems to me like this is close to a solution, but it returns the same number for each grid of the cube, so it may not be correct.
$info = $info.SelectNodes("//CubeBlocks/MyObjectBuilder_CubeBlock[(@xsi:type='MyObjectBuilder_Drill')]/SubtypeName['SmallBlockDrill']" ,$ns).InnerText
$info.count
The results are below. I believe this is the total number of both large and small exercises in the world that are repeated only for each mesh found. feels close, but I bet that choosing a subtype value does not work the way I want.
490 490 490 490 490 490 490 490
source to share