使用XSLT解析Salesforce Marketing Cloud API XML导入FileMaker遇难题
解决Salesforce Marketing Cloud XML导入FileMaker的XPath映射问题
问题背景
将Salesforce Marketing Cloud API返回的XML导入FileMaker数据库时遇到障碍,此前导入其他XML集合均正常,但本次XML采用<Property>标签嵌套字段名和值的结构,示例如下:
<Results xsi:type="DataExtensionObject"> <PartnerKey xsi:nil="true"/> <ObjectID xsi:nil="true"/> <Type>DataExtensionObject</Type> <Properties> <Property> <Name>Account</Name> <Value>000000</Value> </Property> <Property> <Name>LastName</Name> <Value>Doe</Value> </Property> <Property> <Name>FirstName</Name> <Value>John</Value> </Property> <Property> <Name>EmailAddress</Name> <Value>email@email.net</Value> </Property> <Property> <Name>JobName</Name> <Value>30029-Name</Value> </Property> <Property> <Name>Primary Key</Name> <Value>000000</Value> </Property> </Properties> </Results>
尝试编写的XSLT未得到预期结果,代码如下:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tmp="http://tempuri.org" exclude-result-prefixes="SOAP-ENV tmp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:template match="/"> <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult"> <!-- FIELDS --> <METADATA> <FIELD NAME="f1" TYPE="TEXT"/> </METADATA> <!-- DATA --> <RESULTSET> <xsl:for-each select="Results/Properties"> <ROW> <xsl:if test="Property/Name='Account'"> <COL> <DATA> <xsl:value-of select="Value"/> </DATA> </COL> </xsl:if> </ROW> </xsl:for-each> </RESULTSET> </FMPXMLRESULT> </xsl:template> </xsl:stylesheet>
问题分析
原XSLT存在两处核心问题:
- XPath路径错误:
select="Value"是在<Properties>节点下直接取值,但实际<Value>嵌套在<Property>节点内,需通过Property[Name='Account']/Value定位;同时遍历节点应为<Results>而非<Properties>,否则会生成多余行。 - 字段映射逻辑缺失:仅定义了单个占位字段,未将FileMaker字段与XML中的
<Name>值一一对应,无法完整导入数据。
修正后的XSLT方案
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:template match="/"> <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult"> <!-- 定义FileMaker字段元数据,与XML中的Name对应 --> <METADATA> <FIELD NAME="Account" TYPE="TEXT"/> <FIELD NAME="LastName" TYPE="TEXT"/> <FIELD NAME="FirstName" TYPE="TEXT"/> <FIELD NAME="EmailAddress" TYPE="TEXT"/> <FIELD NAME="JobName" TYPE="TEXT"/> <FIELD NAME="Primary Key" TYPE="TEXT"/> </METADATA> <!-- 处理数据行 --> <RESULTSET> <!-- 遍历每个Results节点(支持多条数据) --> <xsl:for-each select="Results"> <ROW> <!-- 匹配Account字段 --> <COL> <DATA> <xsl:value-of select="Properties/Property[Name='Account']/Value"/> </DATA> </COL> <!-- 匹配LastName字段 --> <COL> <DATA> <xsl:value-of select="Properties/Property[Name='LastName']/Value"/> </DATA> </COL> <!-- 匹配FirstName字段 --> <COL> <DATA> <xsl:value-of select="Properties/Property[Name='FirstName']/Value"/> </DATA> </COL> <!-- 匹配EmailAddress字段 --> <COL> <DATA> <xsl:value-of select="Properties/Property[Name='EmailAddress']/Value"/> </DATA> </COL> <!-- 匹配JobName字段 --> <COL> <DATA> <xsl:value-of select="Properties/Property[Name='JobName']/Value"/> </DATA> </COL> <!-- 匹配Primary Key字段 --> <COL> <DATA> <xsl:value-of select="Properties/Property[Name='Primary Key']/Value"/> </DATA> </COL> </ROW> </xsl:for-each> </RESULTSET> </FMPXMLRESULT> </xsl:template> </xsl:stylesheet>
关键调整说明
- 精准XPath定位:通过
Properties/Property[Name='字段名']/Value直接获取对应字段的值,避免逻辑判断的冗余。 - 字段元数据对齐:将
<METADATA>中的字段名与XML里的<Name>值完全对应,确保FileMaker导入时字段映射正确。 - 支持多数据行:遍历
<Results>节点,若API返回多条数据,可自动生成对应<ROW>。
额外注意事项
如果API返回的是完整SOAP格式XML(嵌套在<SOAP-ENV:Envelope>和<SOAP-ENV:Body>内),需调整遍历节点的XPath路径为:select="SOAP-ENV:Envelope/SOAP-ENV:Body/tmp:RetrieveResponseMsg/tmp:Results"
同时保留XSLT中的SOAP-ENV和tmp命名空间定义。
内容的提问来源于stack exchange,提问作者John Haughey
相关产品推荐
相关产品推荐

