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

如何使用DataWeave提取XML中的属性

使用DataWeave提取XML属性

你的XML包含多个命名空间,提取属性和元素内容时需要正确处理命名空间:

目标XML

<?xml version="1.0" encoding="UTF-8"?>
<platformCore:record xmlns="urn:messages.platform.webservices.netsuite.com" xmlns:listRel="urn:relationships.lists.webservices.netsuite.com" xmlns:platformCore="urn:core.platform.webservices.netsuite.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" externalId="680" internalId="1426" xsi:type="listRel:Customer">
    <listRel:entityId>CUST611 Comercial Vega</listRel:entityId>
    <listRel:isPerson>false</listRel:isPerson>
</platformCore:record>

DataWeave 2.0 脚本

%dw 2.0
output application/json
// 声明XML中用到的命名空间
ns platformCore urn:core.platform.webservices.netsuite.com
ns listRel urn:relationships.lists.webservices.netsuite.com
---
{
    // 提取record元素的externalId属性
    externalId: payload.platformCore#record.@externalId,
    // 提取record元素的internalId属性
    internalId: payload.platformCore#record.@internalId,
    // 提取子元素entityId的内容
    customerName: payload.platformCore#record.listRel#entityId,
    // 提取isPerson并转为布尔类型
    isPerson: payload.platformCore#record.listRel#isPerson as Boolean
}

执行结果

{
  "externalId": "680",
  "internalId": "1426",
  "customerName": "CUST611 Comercial Vega",
  "isPerson": false
}

关键要点

  • 用ns关键字绑定命名空间前缀和URI,避免因命名空间导致元素无法识别
  • 元素名前加命名空间前缀#来指定命名空间下的元素
  • 元素属性通过@属性名的方式获取

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 14:42:57