I'm using PowerShell to pass an XML file. Certain values need be of type integer. I have built (or Visual Studio has) an XSD schema file to support my XML in an attempt to specify data types. In the XSD, I specify the data type, and VS intellisense suggests the XSD is correct as when I change an int to a string it complains. However, when it is passed through PowerShell, the data type returned is a string.
Is there a way to get PowerShell to use the data type specified in the XSD?
XML:
<?xml version="1.0" encoding="utf-8"?>
<test xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="test.xsd">
<phase value="build">
<component role="database">
<title value="a new database" />
<vmQty>1</vmQty>
</component>
<component role="application">
<title value="an app" />
<vmQty>1</vmQty>
</component>
</phase>
<phase value="configure">
<component role="database">
<databaseName value ="new world DB" />
</component>
<component role="application">
<appName value="new world app" />
</component>
</phase>
</test>
XSD:
<?xml version="1.0" encoding="utf-8"?>
<schema xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema">
<element name="test">
<complexType>
<sequence>
<element maxOccurs="unbounded" name="phase">
<complexType>
<sequence>
<element maxOccurs="unbounded" name="component">
<complexType>
<sequence>
<element minOccurs="0" name="appName">
<complexType>
<attribute name="value" type="string" use="required" />
</complexType>
</element>
<element minOccurs="0" name="databaseName">
<complexType>
<attribute name="value" type="string" use="required" />
</complexType>
</element>
<element minOccurs="0" name="title">
<complexType>
<attribute name="value" type="string" use="required" />
</complexType>
</element>
<element minOccurs="0" name="vmQty" type="integer" />
</sequence>
<attribute name="role" type="string" use="required" />
</complexType>
</element>
</sequence>
<attribute name="value" type="string" use="required" />
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
PowerShell
$path = "D:\Temp\XML\test.xml"
$xpath = $xPath = "//phase[@value='build']/component"
$oXml = [xml](Get-Content -Path $Path)
$xPathObject = $oXml | Select-Xml -xpath $xPath
$Return = [ordered]@{}
$xPathObject.Node | ForEach-Object{
$Role = $_.Role
$childNodes = $_.ChildNodes
$childNodes | ForEach-Object{
if($_.InnerXml)
{
#e.g <number>1</vmQty>
$Return.$Role += [ordered]@{$_.name = $_.InnerXml}
}
else
{
#e.g <number value="1" />
$Return.$Role += [ordered]@{$_.name = $_.value}
}
}
}
$Return
Return object:
PS C:\> $Return.database
Name Value
---- -----
title a new database
vmQty 1
Check data type of vmQty:
PS C:\> $Return.database.vmQty.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
In the XML, I have added xs:type="integer"
to <vmQty>1</vmQty>
, however this results in Intellisense reporting This is an invalid xsi:type 'integer'.
And just to check it's not my poor coding at work, as simple as I can get it;
$path = "D:\Temp\XML\test.xml"
$oXml = [xml](Get-Content -Path $Path)
$oxml.test.phase[0].component[0].vmQty.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
It still return a string.