如何通过Select-Xml获取属性值?

时间:2019-01-02 21:52:26

标签: xml powershell xpath

如何使用Select-Xml获取属性值?

这是SSIS .dtsx文件的片段。

<?xml version="1.0"?>
<DTS:Executable xmlns:DTS="www.microsoft.com/SqlServer/Dts"
  DTS:refId="Package"
  ...
  <DTS:ConnectionManagers>
    <DTS:ConnectionManager
      DTS:refId="Package.ConnectionManagers[DW02.EDW_Source]"
      DTS:CreationName="OLEDB"
      DTS:DTSID="{12F8E4D7-B122-40AF-A3BD-2B283F9EB3A0}"
      DTS:ObjectName="DW02.EDW_Source">

以下代码无法产生预期的结果。如何获取属性值?

$x = Get-Content -Path .\ECW_SPECIALITY.dtsx
$namespace = @{DTS='www.microsoft.com/SqlServer/Dts'}
$x | Select-Xml -XPath '//@ConnectionManagers/@ConnectionManager[@DTS:ObjectName]' -Namespace $namespace

我遇到以下错误:

Select-Xml : Cannot convert value "<DTS:Executable xmlns:DTS="www.microsoft.com/
SqlServer/Dts"" to type "System.Xml.XmlDocument". Error: "Unexpected end of file
has occurred. The following elements are not closed:  Line 1, position 60."
At line:1 char:6
+ $x | Select-Xml -XPath '//@ConnectionManagers/@ConnectionManager[@DTS ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-Xml], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvalidCastToXmlDocument,Microsoft.PowerShell.Commands.SelectXmlCommand

2 个答案:

答案 0 :(得分:1)

首先,您没有将XML数据作为单个字符串传递。这就是引起您所观察到的错误的原因,因为行522628b8d3db01ac330240b28935933b0448649c:ReAccelerator/Snapshots/RFQ-JENSA_Setpoints.snp:REA_BTS19:BEAM:A_BOOK,0,1545240215.74320185 5,NO_ALARM,NONE,double,"58.0",,"---",,true 2557c599d2dc67d80ffc5b9be3f79899e0c15a10:ReAccelerator/Snapshots/RFQ-JENSA_Setpoints.snp:REA_BTS19:BEAM:A_BOOK,0,1545240215.74320185 5,NO_ALARM,NONE,double,"58.0",,"---",,true 7fc97ec2aa76f32265196c42dbcd289c49f0ad93:ReAccelerator/Snapshots/RFQ-JENSA_Setpoints.snp:REA_BTS19:BEAM:A_BOOK,0,1545240215.74320185 5,NO_ALARM,NONE,double,"58.0",,"---",,true本身不是有效的XML。

将文件读入单个字符串:

...

或将文件直接传递到<DTS:Executable ...

$x = Get-Content '.\ECW_SPECIALITY.dtsx' -Raw          # PowerShell v3 or later
$x = Get-Content '.\ECW_SPECIALITY.dtsx' | Out-String  # PowerShell v2 or earlier

但是,由于您的XPath表达式不正确,仅解决该问题仍然无法获得预期的结果。

  • 要选择命名空间节点或属性,必须在XPath表达式中使用命名空间哈希表中定义的前缀。
  • Select-Xml表示属性。不得用于节点
  • 方括号定义了用来过滤选定节点/属性(而不是要选择的节点/属性)的标准。您需要使用它来选择具有特定属性的节点,但是当您要选择属性本身时则不需要。
  • Select-Xml -Path '.\ECW_SPECIALITY.dtsx' -XPath ... 的输出为您提供选定的节点或属性(属性@)以及输入项(属性Select-Xml)和XPath表达式(属性Node )。要获取节点/属性的值,必须扩展属性Path。两次。

这应该做您想要的:

Pattern

有关更多详细信息,请检查XPath reference

答案 1 :(得分:0)

此解决方案可能有用:

$rootpath  = 'C:\Users\EricBellet\Desktop\Test\'
$filename = 'connections.txt'

$ns = @{DTS='www.microsoft.com/SqlServer/Dts'}
$objectname = '//DTS:ConnectionManagers/DTS:ConnectionManager/@DTS:ObjectName'
$connectionstring = '//DTS:ConnectionManagers/DTS:ConnectionManager/DTS:ObjectData/DTS:ConnectionManager/@DTS:ConnectionString'


Get-ChildItem $rootpath -Include *.dtsx -Recurse | % { $_.FullName >> $filename; Select-Xml -Path $_.FullName -XPath $objectname -Namespace $ns | Select-Object -Expand Node | Select-Object -Expand '#text' >> $filename; Select-Xml -Path $_.FullName-XPath $connectionstring -Namespace $ns | Select-Object -Expand Node | Select-Object -Expand '#text' >> $filename; '' >> $filename; }