如何通过 Select-Xml 获取属性值? [英] How can I get the attribute value through Select-Xml?

查看:33
本文介绍了如何通过 Select-Xml 获取属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是一个 SSIS .dtsx 文件的片段.

<DTS:Executable xmlns:DTS="www.microsoft.com/SqlServer/Dts"DTS:refId="包"...<DTS:ConnectionManagers><DTS:连接管理器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 : 无法转换值 "<DTS:Executable xmlns:DTS="www.microsoft.com/SqlServer/Dts"" 以键入 "System.Xml.XmlDocument".错误:意外的文件结尾已经发生了.以下元素未关闭:第 1 行,位置 60."在行:1 字符:6+ $x |Select-Xml -XPath '//@ConnectionManagers/@ConnectionManager[@DTS ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : InvalidArgument: (:) [Select-Xml], ParentContainsErrorRecordException+ FullQualifiedErrorId : InvalidCastToXmlDocument,Microsoft.PowerShell.Commands.SelectXmlCommand

解决方案

首先,您没有将 XML 数据作为单个字符串传递.这就是导致您观察到的错误的原因,因为行 <DTS:Executable ... 本身不是有效的 XML.

要么将文件读入单个字符串:

$x = Get-Content '.\ECW_SPECIALITY.dtsx' -Raw # PowerShell v3 或更高版本$x = Get-Content '.\ECW_SPECIALITY.dtsx' |Out-String # PowerShell v2 或更早版本

或者直接将文件传递给Select-Xml:

Select-Xml -Path '.\ECW_SPECIALITY.dtsx' -XPath ...

只是修复它仍然不会给您想要的结果,因为您的 XPath 表达式不正确.

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

这应该可以满足您的要求:

$ns = @{DTS='www.microsoft.com/SqlServer/Dts'}$path = '.\ECW_SPECIALITY.dtsx'$xpath = '//DTS:ConnectionManagers/DTS:ConnectionManager/@DTS:ObjectName'# ^节点 ^节点 ^属性Select-Xml -Path $path -XPath $xpath -Namespace $ns |选择对象 - 展开节点 |Select-Object -Expand '#text'

有关更多详细信息,请查看 XPath 参考.>

How can I get an attribute value using Select-Xml?

Here is a fragment of an SSIS .dtsx file.

<?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">

The following code does not produce the desired result. How can I get the attribute value?

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

I'm getting the following error:

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

解决方案

First and foremost, you're not passing the XML data as a single string. That's what's causing the error you've observed, because the line <DTS:Executable ... is not valid XML by itself.

Either read the file into a single string:

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

or pass the file directly to Select-Xml:

Select-Xml -Path '.\ECW_SPECIALITY.dtsx' -XPath ...

Just fixing that still won't give you the desired result, though, since your XPath expression is incorrect.

  • For selecting namespaced nodes or attributes the prefix defined in the namespace hashtable must be used in the XPath expression.
  • @ indicates an attribute. It must not be used for nodes.
  • Square brackets define criteria by which the selected nodes/attributes are filtered, not nodes/attributes to be selected. You need this for selecting a node that has a particular attribute, but not when you want to select the attribute itself.
  • The output of Select-Xml gives you the selected node or attribute (property Node) along with the input item (property Path) and the XPath expression (property Pattern). To get the value of the node/attribute you must expand the property Node. Twice.

This should do what you want:

$ns    = @{DTS='www.microsoft.com/SqlServer/Dts'}
$path  = '.\ECW_SPECIALITY.dtsx'
$xpath = '//DTS:ConnectionManagers/DTS:ConnectionManager/@DTS:ObjectName'
#           ^node                  ^node                 ^attribute

Select-Xml -Path $path -XPath $xpath -Namespace $ns |
    Select-Object -Expand Node |
    Select-Object -Expand '#text'

For further details please check the XPath reference.

这篇关于如何通过 Select-Xml 获取属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆