我一直在尝试编写XSLT以合并两个不同的XML,并使用XSLT v2.0使用XSLT代码生成不同的XML输出。
以下是我的要求。
输入XML1是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<externalIdFields xmlns="urn:partner.soap.sforce.com/">
<object name="Account">
<ExternalID>
<name>ExtAccountID__c</name>
<type>string</type>
</ExternalID>
</object>
<object name="User"/>
<object name="Contact">
<ExternalID>
<name>ExtContactID__c</name>
<type>string</type>
</ExternalID>
</object>
<object name="Opportunity">
<ExternalID>
<name>ExtOpportunityID__c</name>
<type>decimal</type>
</ExternalID>
<ExternalID>
<name>UniqueAnotherOppID__c</name>
<type>boolean</type>
</ExternalID>
</object>
和XML2是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<relationshipNames>
<object name="Account">
<reletionshipName>Parent</reletionshipName>
</object>
<object name="User">
<reletionshipName>Owner</reletionshipName>
</object>
<object name="Contact">
<reletionshipName>LookUpContact__r</reletionshipName>
</object>
<object name="Opportunity">
<reletionshipName>LookUpOpportunity__r</reletionshipName>
</object>
<object name="Opportunity">
<reletionshipName>AnotherRelationship__r</reletionshipName>
</object>
</relationshipNames>
第一个XML包含每个XML的所有外部ID,第二个XML包含每个对象的所有关系名称。
输出XML应为
<?xml version="1.0" encoding="UTF-8"?>
<result xmlns:con = "http://www.approuter.com/schemas/cdk/config/">
<con:field name = "Parent.ExtAccountID__c" label = "Parent.ExtAccountID__c">
<con:type>string</con:type>
</con:field>
<con:field name = "LookUpContact__r.ExtContactID__c" label="LookUpContact__r.ExtContactID__c">
<con:type>string</con:type>
</con:field>
<con:field name = "LookUpOpportunity__r.ExtOpportunityID__c" label = "LookUpOpportunity__r.ExtOpportunityID__c">
<con:type>decimal</con:type>
</con:field>
<con:field name = "LookUpOpportunity__r.UniqueAnotherOppID__c." label = "LookUpOpportunity__r.UniqueAnotherOppID__c">
<con:type>boolean</con:type>
</con:field>
<con:field name = "AnotherRelationship__r.ExtOpportunityID__c." label = "AnotherRelationship__r.ExtOpportunityID__c">
<con:type>boolean</con:type>
</con:field>
<con:field name = "AnotherRelationship__r.UniqueAnotherOppID__c." label = "AnotherRelationship__r.UniqueAnotherOppID__c">
<con:type>boolean</con:type>
</con:field>
</result>
合并很简单。对于每个对象的每个externalId,创建一个con:field标记,这里name和label的值应该是该对象的对应relationshipName和externalId字段名。它将是relationshiName.externalIdFieldName。对象的relationshipName应该只与该对象的externalIds相关联。
我们怎样才能做到这一点?我看到一个XSLT来合并两个XML。但我不理解&#34; @ * |节点()&#34;部分不同的例子。在这里,我没有找到解析两个完全不同的XML的示例。
感谢您的帮助。
答案 0 :(得分:1)
这是我的建议:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sf="urn:partner.soap.sforce.com/"
xmlns:con="http://www.approuter.com/schemas/cdk/config/"
exclude-result-prefixes="sf">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="names-url" select="'test2013080105.xml'"/>
<xsl:variable name="names-doc" select="doc($names-url)"/>
<xsl:key name="name" match="relationshipNames/object" use="@name"/>
<xsl:template match="sf:externalIdFields">
<result>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="sf:object/sf:ExternalID">
<xsl:apply-templates select="key('name', ../@name, $names-doc)">
<xsl:with-param name="eid" select="current()"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="relationshipNames/object">
<xsl:param name="eid"/>
<con:field name="{reletionshipName}.{$eid/sf:name}" label="{reletionshipName}.{$eid/sf:name}">
<con:type><xsl:value-of select="$eid/sf:type"/></con:type>
</con:field>
</xsl:template>
</xsl:stylesheet>
样式表为第二个文件名/ URL采用参数names-url
。还要确保所有元素名称和命名空间URL都已发布(即reletionshipName
),或者如果真实输入具有正确拼写的元素名称,请确保编辑样式表以使其具有正确的拼写,如relationshipName
。< / p>