我使用Powershell安装SSIS项目并为其创建环境。我正在从Project.Params XML文件中读取项目参数,以创建环境变量&他们的价值观我可以使用以下代码片段来读取参数&它们的值,但我想使用SelectSingleNode而不是第二个foreach循环。我的XPath技能不符合它。 这是工作代码:
# load the Params file into memory
[xml]$ParamsDoc = Get-Content -Path $ParamsFilePath
# for each parameter in the Params file, add an environment variable if it doesn't already exist
foreach ($Param in $ParamsDoc.Parameters.Parameter)
{
$ParamName = $Param.Name
# use the value in the Params file as the default value for the environment variable
foreach ($Prop in $Param.Properties.Property)
{
if ($Prop.Name -eq 'value')
{
$ParamValue = $Prop.InnerText
break
}
}
AddEnvironmentVariableIfMissing $ParamName $ParamValue
}
AddEnvironmentVariableIfMissing是我编写的一个单独的函数。现在,这里的代码不起作用:
# load the Params file into memory
[xml]$ParamsDoc = Get-Content $ParamsFilePath
$nsMgr = New-Object System.Xml.XmlNamespaceManager($ParamsDoc.NameTable)
$nsMgr.AddNamespace("SSIS", $ParamsDoc.DocumentElement.NamespaceURI)
foreach ($Param in $ParamsDoc.Parameters.Parameter)
{
$ParamName = $Param.Name
# NONE of these work!
$ParamValue = $Param.Properties.SelectSingleNode("SSIS:Property[@Name='Value']",$nsMgr)
$ParamValue = $Param.Properties.SelectSingleNode("Name[.='Value']",$nsMgr)
$ParamValue = $Param.Properties.SelectSingleNode("SSIS:Property/Name[.='Value']",$nsMgr)
$ParamValue = $Param.Properties.Property.SelectSingleNode("Name[.='Value']",$nsMgr)
$ParamValue = $Param.SelectSingleNode("SSIS:Properties/SSIS:Property[@Name='Value']",$nsMgr)
$ParamValue = $Param.SelectSingleNode("SSIS:Properties/SSIS:Property/Name[.='Value']",$nsMgr)
$ParamValue = $Param.SelectSingleNode("SSIS:Property[@Name='Value']",$nsMgr)
$ParamValue = $Param.SelectSingleNode("SSIS:Property/Name[.='Value']",$nsMgr)
AddEnvironmentVariableIfMissing $ParamName $ParamValue
}
使用Name =' Value'来获取子节点的正确XPath语法是什么?
这是SSIS Project.params文件的样本
<?xml version="1.0"?>
<SSIS:Parameters xmlns:SSIS="www.microsoft.com/SqlServer/SSIS">
<SSIS:Parameter
SSIS:Name="Client">
<SSIS:Properties>
<SSIS:Property
SSIS:Name="ID">{144b0266-9e94-4821-bb68-f020436d5df5}</SSIS:Property>
<SSIS:Property
SSIS:Name="CreationName"></SSIS:Property>
<SSIS:Property
SSIS:Name="Description"></SSIS:Property>
<SSIS:Property
SSIS:Name="IncludeInDebugDump">0</SSIS:Property>
<SSIS:Property
SSIS:Name="Required">0</SSIS:Property>
<SSIS:Property
SSIS:Name="Sensitive">0</SSIS:Property>
<SSIS:Property
SSIS:Name="Value">sampleclient</SSIS:Property>
<SSIS:Property
SSIS:Name="DataType">18</SSIS:Property>
</SSIS:Properties>
</SSIS:Parameter>
<SSIS:Parameter
SSIS:Name="MSSQLServer">
<SSIS:Properties>
<SSIS:Property
SSIS:Name="ID">{b4e6c5e1-ef85-4df3-b2dc-db37971d081d}</SSIS:Property>
<SSIS:Property
SSIS:Name="CreationName"></SSIS:Property>
<SSIS:Property
SSIS:Name="Description"></SSIS:Property>
<SSIS:Property
SSIS:Name="IncludeInDebugDump">0</SSIS:Property>
<SSIS:Property
SSIS:Name="Required">0</SSIS:Property>
<SSIS:Property
SSIS:Name="Sensitive">0</SSIS:Property>
<SSIS:Property
SSIS:Name="Value">MSSQLSERVER</SSIS:Property>
<SSIS:Property
SSIS:Name="DataType">18</SSIS:Property>
</SSIS:Properties>
</SSIS:Parameter>
</SSIS:Parameters>
答案 0 :(得分:1)
所以PetSerAI有正确的想法。命名空间需要采用XPath语法,包括Name元素。有效的两个语法是:
$ParamValue = $Param.Properties.SelectSingleNode("SSIS:Property[@SSIS:Name='Value']",$nsMgr)
$ParamValue = $Param.SelectSingleNode("SSIS:Properties/SSIS:Property[@SSIS:Name='Value']",$nsMgr)
谢谢你,PetSerAI!