我一直在尝试编写一个带有2.0版本的XSLT,以将CSV数据(再次嵌入到XML元素中)转换为XML。
以下是我的样本CSV数据
<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>
这里第一行是标题字段 对于上面的数据,我的输出XML应该是
<results xmlns = "http://www.force.com/2009/06/asyncapi/dataload">
<result>
<id>001D000000ISUr3IAH</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>
我的转换应具有以下逻辑来执行转换。
正如您在第一条记录中看到的那样(不是标题行,实际上是csv数据中的第二行) 成功值为true,因此结果将按原样填充,包含id,success和created信息。
对于第二行,成功为false,因此结果中不会有id元素,但应填充错误。 要填充错误,应解析CSV数据中的错误字段,并将第一个令牌:(冒号)放入statusCode元素中,并将最后一个令牌:(冒号)放入字段中(通过解析空格分隔)和休息中间字符串应该放入错误的消息字段。
我在这里搜索了示例XSLT,但无法获得如何开始。 能否帮助我从示例XSLT开始,以便我可以实现这一目标?
答案 0 :(得分:2)
对于您的所有数据,这可能无法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>