这可能无法 100% 地适用于您的所有数据,但它适用于给出的示例,并且应该给您一个不错的开始。
XML 输入
<csv>
"Id","Success","Created","Error"
"001P000000aXgRAIA0","true","true",""
"","false","false","REQUIRED_FIELD_MISSING:Required fields are missing: [Name, Man1__c, man2__c]:Name Man1__c man2__c --"
</csv>
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="csv">
<results xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<xsl:analyze-string select="." regex="(.*)\n">
<xsl:matching-substring>
<xsl:if test="not(regex-group(1)='' or contains(regex-group(1),'Id'))">
<result>
<xsl:variable name="fields" as="element()">
<fields>
<xsl:analyze-string select="regex-group(1)" regex=""([^"]*)",?">
<xsl:matching-substring>
<field>
<xsl:value-of select="regex-group(1)"/>
</field>
</xsl:matching-substring>
</xsl:analyze-string>
</fields>
</xsl:variable>
<xsl:choose>
<xsl:when test="$fields/*[2]='true'">
<id><xsl:value-of select="$fields/*[1]"/></id>
<success><xsl:value-of select="$fields/*[2]"/></success>
<created><xsl:value-of select="$fields/*[3]"/></created>
</xsl:when>
<xsl:otherwise>
<errors>
<xsl:variable name="errorTokens" select="tokenize($fields/*[4],':')"/>
<xsl:for-each select="tokenize(substring-before(substring-after($errorTokens[3],'['),']'),',')">
<fields><xsl:value-of select="normalize-space(.)"/></fields>
</xsl:for-each>
<message><xsl:value-of select="$errorTokens[not(position()=1) and not(position()=4)]" separator=":"/></message>
<statusCode><xsl:value-of select="$errorTokens[1]"/></statusCode>
</errors>
<success><xsl:value-of select="$fields/*[2]"/></success>
<created><xsl:value-of select="$fields/*[3]"/></created>
</xsl:otherwise>
</xsl:choose>
</result>
</xsl:if>
</xsl:matching-substring>
</xsl:analyze-string>
</results>
</xsl:template>
</xsl:stylesheet>
XML 输出
<results xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<result>
<id>001P000000aXgRAIA0</id>
<success>true</success>
<created>true</created>
</result>
<result>
<errors>
<fields>Name</fields>
<fields>Man1__c</fields>
<fields>man2__c</fields>
<message>Required fields are missing: [Name, Man1__c, man2__c]</message>
<statusCode>REQUIRED_FIELD_MISSING</statusCode>
</errors>
<success>false</success>
<created>false</created>
</result>
</results>