我使用PowerShell传递XML文件。某些值必须是整数类型。 我已经构建(或Visual Studio)一个XSD架构文件来支持我的XML以尝试指定数据类型。在XSD中,我指定了数据类型,VS intellisense建议XSD是正确的,就像我将int更改为它所抱怨的字符串一样。但是,当它通过PowerShell传递时,返回的数据类型是一个字符串。
有没有办法让PowerShell使用XSD中指定的数据类型?
<?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>
<?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>
$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
返回对象:
PS C:\> $Return.database
Name Value
---- -----
title a new database
vmQty 1
检查vmQty的数据类型:
PS C:\> $Return.database.vmQty.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
在XML中,我已将xs:type="integer"
添加到<vmQty>1</vmQty>
,但这会导致智能感知报告This is an invalid xsi:type 'integer'.
只是为了检查它不是我在工作中编码不好,就像我能得到它一样简单;
$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
它仍会返回一个字符串。