Xpath 使用无输出

问题描述 投票:0回答:2

我有一个 xml 的 API 输出,我正在尝试解析 下面是一个例子。

我正在使用 Power Automate 并尝试使用 xpath

<?xml version="1.0"?>
<requests>
 <request id="31998">
  <employee id="6505">Peter Parker</employee>
  <start>2025-07-11</start>
  <end>2025-07-13</end>
  <created>2025-07-16</created>
  <status lastChanged="2024-01-16" lastChangedByUserId="91210">approved</status>
  <type id="84" icon="time-off-calendar">Vacation</type>
  <amount unit="days">2</amount>
  <dates>
   <date ymd="2024-03-11" amount="1"/>
   <date ymd="2024-03-12" amount="1"/>

  </dates>
  <notes>
   <note from="employee">Work Sucks and i dont wont to be here</note>
  </notes>
 </request>

我的 Xpath 看起来像这样

xpath(xml(outputs('Compose')), '(//request/@id)')

我的输出是这样的

[
  "id=\"31998\"",
  "id=\"32214\"",
  "id=\"32339\""
]

我只想要这里的数字,所以我尝试了以下

xpath(xml(outputs('Compose')), 'string(//request/@id)')

这只返回

31998

这对我来说没有好处,我想要所有的结果。就像第一个例子一样。

所以我尝试了这个语法

xpath(xml(outputs('Compose')), 'string(//request/@id/text()')
xpath(xml(outputs('Compose')), 'string(//request/@id[text()]')

这两者都会产生 null

我现在很茫然,失去了所有的耐心 可爱的小伙伴们能帮忙吗

xml xpath xml-parsing power-automate
2个回答
0
投票

很好,我也无法让它发挥作用。最坏的情况是,您可能需要进行一些字符串操作。

或者,您可以使用 高级数据操作 连接器和

XmlToJson
操作...

结果

...另一种选择是将 XML 转换为 JSON 并使用

Select
操作对其进行处理...

表达式

来自 =

json(xml(variables('XML')))['requests']['request']

地图项目 =

item()[string('@id')]

结果


-1
投票

使用 Powershell 和 Xml Linq 将数据解析到表中,然后导出到 CSV

using assembly System.Xml.Linq 

$inputFilename = 'c:\temp\test.xml'
$outputFilename = 'c:\temp\test.csv'

$doc = [System.Xml.Linq.XDocument]::Load($inputFilename)

$table = [System.Collections.ArrayList]::new()
foreach($request in $doc.Descendants('request'))
{
   $requestId = $request.Attribute('id').Value
   $employee = $request.Element('employee')
   $employeeId = $employee.Attribute('id').Value
   $employeeName = $employee.Value

   $newRow = [psCustomObject]@{
      RequestId = $requestId
      EmployeeId = $employeeId
      EmployeeName = $employeeName
   }

   $table.Add($newRow) | out-null
}

$table | Export-CSV -path $outputFilename -NoTypeInformation

请求将如下所示

"RequestId","EmployeeId","EmployeeName"
"31998","6505","Peter Parker"
© www.soinside.com 2019 - 2024. All rights reserved.