使用XPath的XML无法成功

时间:2019-07-02 06:56:29

标签: xml powershell xpath

我有这样的XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://.../nuget/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">Packages</title>
  <id>http://.../nuget/Packages</id>
  <updated>2019-06-24T08:30:28Z</updated>
  <link rel="self" title="Packages" href="Packages" />
<id>http://.../nuget/Packages(Id='7zip',Version='16.0.2')</id>
    <title type="text">programma</title>
    <summary type="text">Programma is a file archiver with a high compression ratio.</summary>
    <updated>2018-06-19T15:33:22Z</updated>
    <author>
      <name>Herman</name>
    </author>
    <link rel="edit-media" title="ODataPackage" href="Packages(Id='7zip',Version='16.0.2')/$value" />
    <link rel="edit" title="ODataPackage" href="Packages(Id='7zip',Version='16.0.2')" />
    <category term="NuGet.Server.DataServices.ODataPackage"scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/zip" src="http://.../api/v2/package/7zip/16.0.2" />
    <m:properties xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
      <d:Version>16.0.2</d:Version>
      <d:NormalizedVersion>16.0.2</d:NormalizedVersion>
      <d:IsPrerelease m:type="Edm.Boolean">false</d:IsPrerelease>
      <d:Title>7-Zip</d:Title>
      <d:Owners>Herman</d:Owners>
      <d:IconUrl m:null="true"></d:IconUrl>
      <d:LicenseUrl>http://www.7-zip.org/license.txt</d:LicenseUrl>
      <d:ProjectUrl>http://www.7-zip.org/</d:ProjectUrl>
      <d:DownloadCount m:type="Edm.Int32">-1</d:DownloadCount>
      <d:RequireLicenseAcceptance m:type="Edm.Boolean">false</d:RequireLicenseAcceptance>
      <d:DevelopmentDependency m:type="Edm.Boolean">false</d:DevelopmentDependency>
      <d:Description>7-Zip is a file archiver with a high compression ratio.

使用Xpath,Powershell中的以下代码为$ entry提供了一些值:

$xpath = "//atom:title[contains(text(),'$ChocoPackageName')]"
$entry = $xml | Select-Xml -XPath $xpath -Namespace @{atom="http://www.w3.org/2005/Atom"}
$entry | Should Not BeNullOrEmpty 

现在我必须创建一个xpath来在属性的“版本”中查找具有特定版本(16.0.2)的节点

我制作了:

$xpath = "//Version[contains(text(), '$Version')]"
$entry = $xml | Select-Xml -XPath $xpath -Namespace @{atom="http://www.w3.org/2005/Atom"}
$entry | Should Not BeNullOrEmpty 

我无法使用它。无论我如何定义XPath。如何定义供稿/条目/属性/版本的$ xpath

请帮助!

1 个答案:

答案 0 :(得分:0)

func userNotificationCenter元素位于您未在PS脚本中声明的不同名称空间中:

Version

类似于您说的代码已经可以使用:

xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
...
<d:Version>16.0.2</d:Version>

免责声明:我不是Powershell专家,所以我不能说这是否是PS中的最佳解决方案。