I'm trying to read data from an XML into Powershell.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="tmp.xslt"?>
<Book>
<Subbook id="1848">
<variable Name="Metadata_Code" Value="A-"/>
<variable Name="Metadata_Installation" Value="pod - A"/>
<Topic...>
<Topic....>
</Subbook>
<Subbook id="1849">
<variable Name="Metadata_Code" Value="B-"/>
<variable Name="Metadata_Installation" Value="pod - B"/>
<Topic...>
<Topic....>
</Subbook>
</Book>
I want to loop through all Subbook elements. Each Subbook element contains a few variable elements. In addition it contains other elements I have to process. I can set up the loop.
[xml]$xmlfile = Get-Content -Path $xmlpath
$subbooks = Select-Xml -Path $xmlpath -XPath '//Subbook'
$subbooks | ForEach-Object {
$MDC = $_.Node.SelectNodes('variable[@Name="Metadata_Code"]/@Value')
}
But then I'm stuck. I need to select the value of the variable node that has name 'Metadata_Code'. In XSLT this is variable[@Name='Metadata_Code']/@Value
$MDC remains empty. How can I get the value I need into this variable?