标签: select-xml

PowerShell select-xml xpath似乎不起作用

我正在尝试使用select-xmlSharePoint解决方案中的一些东西.我有一个包含许多功能的解决方案目录,而不是打开每个feature.xml功能并手动选择功能名称并将它们放在一个列表中,我希望使用powershell和等效select-xml.

我的尝试是这样的:

ls -recurse -filter feature.xml | select-xml "/Feature"
Run Code Online (Sandbox Code Playgroud)

我什么都没得到,所以我尝试了这个:

ls -recurse -filter feature.xml | select-xml "//*"
Run Code Online (Sandbox Code Playgroud)

这似乎做了应该做的事情.我得到了解决方案中所有feature.xml文件中每个XML节点的列表.

我尝试了像"//功能"和"功能"这样的XPath表达式,但都没有得到任何结果.

在我看来,我的XPath表达式是正确的,但select-xml的行为有点令人困惑.任何人都知道为什么会发生这种情况?

xml powershell sharepoint select-xml

8
推荐指数
2
解决办法
3538
查看次数

使用Powershell进行外科XML编辑

我正在使用Powershell处理csproj文件,以执行项目引用的大规模编辑.到目前为止,我已经设法使用以下行编辑ProjectReferences上的Include属性:

    $projectXml = [xml](Get-Content $_.Project.FullName)
    Add-TfsPendingChange -edit $_.Project.FullName -ErrorAction Stop
    $projectXml | Select-Xml -namespace @{msb = "http://schemas.microsoft.com/developer/msbuild/2003"} -xpath "//msb:ProjectReference[msb:Project='$projectGuid']" | Select-Object -ExpandProperty Node | foreach { $_.Include = "$newPath" }
    $projectXml.Save($_.Project.FullName)
Run Code Online (Sandbox Code Playgroud)

这可以工作,并按照我的预期替换相应ProjectReferences上的Include属性.但是,还有很多额外的"无害"更改,例如在自己的行上格式化所有标记,例如
<FileUpgradeFlags></FileUpgradeFlags>

<FileUpgradeFlags>
</FileUpgradeFlags>

有没有办法执行这样的编辑,没有这些副作用?

编辑:为了清楚任何因其他原因发现此帖子的人,Select-MsBuildXml只是我在Select-Xml周围编写的包装函数,它使用msbuild命名空间预加载namespace参数,然后扩展node属性.

xml powershell formatting side-effects select-xml

7
推荐指数
1
解决办法
2098
查看次数

通过Powershell在Select-Xml中使用Xpath not运算符

我正在使用csproj文件,我正在尝试编写一个cmdlet来提取项目文件引用的所有文件.这将包括所有Compile,EmbeddedResource,Resource,Content和None元素(更重要的是,它们的@Include值),但我特别想要将引用元素排除在它们引用的dll之外,这些对我来说并不重要.

我没有太多的经验,但我认为我想要的xpath表达式看起来像这样

$projectFile | Select-Xml -namespace @{msb="http://schemas.microsoft.com/developer/msbuild/2003"} -xpath "//msb:ItemGroup/*[not(self::node() = msb:Reference) and @Include]"
Run Code Online (Sandbox Code Playgroud)

但是,只要我尝试引入self :: node(),我的表达式就不会返回任何节点.我不是100%确定self :: node()是正确的方法.知道我会改变什么以使其返回,概念上,"所有包含属性的属性值都不是作为ItemGroup元素的子元素的Reference元素吗?"

powershell xpath operators csproj select-xml

2
推荐指数
1
解决办法
1035
查看次数

如何使用 ConvertTo-Xml 和 Select-Xml 加载或读取 XML 文件?

我怎样才能完成这样的事情:

PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> $date=(Get-Date | ConvertTo-Xml)                                         
PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> $date

xml                            Objects
---                            -------
version="1.0" encoding="utf-8" Objects

PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> $date.OuterXml
<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.DateTime">12/12/2020 2:43:46 AM</Object></Objects>
PS /home/nicholas/powershell> 
Run Code Online (Sandbox Code Playgroud)

但是,相反,读取文件?


如何加载/导入/读取/转换用于解析的xml文件using ?ConvertTo-XmlSelect-XmlXpath

PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> $xml=ConvertTo-Xml ./bookstore.xml
PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> $xml                              

xml                            Objects
---                            -------
version="1.0" encoding="utf-8" Objects

PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> $xml.InnerXml                     
<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.String">./bookstore.xml</Object></Objects>
PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> $xml.OuterXml                     
<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.String">./bookstore.xml</Object></Objects>
PS /home/nicholas/powershell> 
PS …
Run Code Online (Sandbox Code Playgroud)

xml powershell xpath xquery select-xml

1
推荐指数
1
解决办法
1039
查看次数