You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用XPath获取IntelliJ IDEA首个build节点的version属性?

问题描述

我编写了以下PowerShell代码片段:

[xml]$doc = (New-Object System.Net.WebClient).DownloadString("https://www.jetbrains.com/updates/updates.xml")

$xPath = "//product[@name='IntelliJ IDEA']//channel[@id='IC-IU-RELEASE-licensing-RELEASE']//[1]"
$node = $doc | Select-XML -XPath $xPath 

需求是通过XPath获取IntelliJ IDEA的最新构建版本,目标是提取如下build节点的version属性:

<build number="222.4345" version="2022.2.3" releaseDate="20220726" fullNumber="222.4345.14">

尝试过程中遇到的问题:

  • 使用XPath //product[@name='IntelliJ IDEA']//channel[@id='IC-IU-RELEASE-licensing-RELEASE']//[1]时,返回了所有build节点;
  • 改用//product[@name='IntelliJ IDEA']//channel[@id='IC-IU-RELEASE-licensing-RELEASE']//build[1]时,虽能定位首个build,但无法获取包含version属性的节点;
  • 尝试用$doc | Select-XML -XPath $xPath | ForEach-Object { $_.Node.version }遍历所有版本,结果为空。

请问获取首个build节点的正确XPath语法是什么?

解决方案

正确的XPath语法如下:

//product[@name='IntelliJ IDEA']//channel[@id='IC-IU-RELEASE-licensing-RELEASE']/build[1]

说明

  • 之前使用//build[1]时,//会递归匹配所有层级下的build元素,[1]作用是取每个父节点下的第一个build,而非目标channel节点下的第一个build;
  • 改用单个斜杠/build[1]后,会直接定位到目标channel节点的直接子节点build,并取第一个匹配项,这样就能准确拿到包含version属性的目标节点。

完整PowerShell代码示例

[xml]$doc = (New-Object System.Net.WebClient).DownloadString("https://www.jetbrains.com/updates/updates.xml")

# 正确的XPath
$xPath = "//product[@name='IntelliJ IDEA']//channel[@id='IC-IU-RELEASE-licensing-RELEASE']/build[1]"
$node = $doc | Select-Xml -XPath $xPath

# 提取version属性
$latestVersion = $node.Node.version
Write-Host "IntelliJ IDEA最新版本:$latestVersion"

内容的提问来源于stack exchange,提问作者Marco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 02:05:25