使用 XSLT 样式表解析 XML 时遇到问题

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

我在将 Salesforce Marketing Cloud API 返回的 XML 导入 FileMaker 数据库时遇到问题。我已经对其他 XML 集执行了此操作,没有出现任何问题,但此 XML 与我之前看到的不同。这是返回的示例:

<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 protected]</Value>
                    </Property>
                                        <Property>
                        <Name>JobName</Name>
                        <Value>30029-Name</Value>
                    </Property>
                    <Property>
                        <Name>Primary Key</Name>
                        <Value>000000</Value>
                    </Property>
                    </Properties>
            </Results>
The multiple occurrences of the <Property> tag has me a little stumped on mapping the xpath. Any thoughts are appreciated.   

这是我尝试制作的一些样式表,首先尝试解析一个字段。这没有结果。

<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>
xml xslt import filemaker
1个回答
0
投票

导入示例*的正确方法如下:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/Results">
    <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
        <!-- FIELDS -->
        <METADATA>
            <FIELD NAME="Account"/>              
            <FIELD NAME="LastName"/>              
            <FIELD NAME="FirstName"/>              
            <FIELD NAME="EmailAddress"/>              
            <FIELD NAME="JobName"/>              
            <FIELD NAME="Primary Key"/>              
        </METADATA>        
        <!-- DATA -->
        <RESULTSET>
            <xsl:for-each select="Properties">
                <ROW>
                    <COL>
                        <DATA>
                            <xsl:value-of select="Property[Name='Account']/Value"/>
                        </DATA>
                    </COL>
                    <COL>
                        <DATA>
                            <xsl:value-of select="Property[Name='LastName']/Value"/>
                        </DATA>
                    </COL>
                    <COL>
                        <DATA>
                            <xsl:value-of select="Property[Name='FirstName']/Value"/>
                        </DATA>
                    </COL>
                    <COL>
                        <DATA>
                            <xsl:value-of select="Property[Name='EmailAddress']/Value"/>
                        </DATA>
                    </COL>
                    <COL>
                        <DATA>
                            <xsl:value-of select="Property[Name='JobName']/Value"/>
                        </DATA>
                    </COL>
                    <COL>
                        <DATA>
                            <xsl:value-of select="Property[Name='Primary Key']/Value"/>
                        </DATA>
                    </COL>
                </ROW>
            </xsl:for-each>
        </RESULTSET>
    </FMPXMLRESULT>
</xsl:template>

</xsl:stylesheet>

正如我在对您的问题的评论中提到的,这将创建一条包含 6 个字段的记录。如果可以有多个记录,并且每组字段都位于其自己的

Property
元素中,那么这也将正确导入它们。

如果每条记录始终具有相同的字段集,并以相同的顺序列出,那么您可以将其缩短为:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/Results">
    <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
        <!-- FIELDS -->
        <METADATA>
            <xsl:for-each select="Properties[1]/Property">
                <FIELD NAME="{Name}"/>              
            </xsl:for-each>
        </METADATA>        
        <!-- DATA -->
        <RESULTSET>
            <xsl:for-each select="Properties">
                <ROW>
                    <xsl:for-each select="Property">
                        <COL>
                            <DATA>
                                <xsl:value-of select="Value"/>
                            </DATA>
                        </COL>
                    </xsl:for-each>
                </ROW>
            </xsl:for-each>
        </RESULTSET>
    </FMPXMLRESULT>
</xsl:template>

</xsl:stylesheet>

(*) 前提是添加了

xsi
前缀缺少的命名空间声明。

© www.soinside.com 2019 - 2024. All rights reserved.