我有一个 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
我现在很茫然,失去了所有的耐心 可爱的小伙伴们能帮忙吗
使用 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"