根据ID值将Child元素与其父元素相关联

时间:2016-03-22 05:42:47

标签: xml xslt xslt-1.0 xslt-2.0

我必须转换输入xml,

<?xml version="1.0" encoding="UTF-8" ?>
<queryResponse xmlns="urn:partner.soap.sforce.com">
    <result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="QueryResult">
        <records xmlns:sf="urn:sobject.partner.soap.sforce.com" xsi:type="sf:sObject">
            <sf:type>Course__c</sf:type>
            <sf:Id>a0c20000002Yu</sf:Id>
            <sf:Id>a0c20000002Yu</sf:Id>
            <sf:CurrencyIsoCode>USD</sf:CurrencyIsoCode>
            <sf:Course_End_Time__c>5 pm</sf:Course_End_Time__c>
            <sf:Course_Start_Time__c>8 am</sf:Course_Start_Time__c>
            <sf:Course_Type__c>Face to Face</sf:Course_Type__c>
        </records>
        <records xmlns:sf="urn:sobject.partner.soap.sforce.com" xsi:type="sf:sObject">
            <sf:type>Course__c</sf:type>
            <sf:Id>a0c20000002ZI</sf:Id>
            <sf:Id>a0c20000002ZI</sf:Id>
            <sf:CurrencyIsoCode>AUD</sf:CurrencyIsoCode>
            <sf:Course_End_Time__c>5:00PM</sf:Course_End_Time__c>
            <sf:Course_Start_Time__c>9:00AM</sf:Course_Start_Time__c>
            <sf:Course_Type__c>Face to Face</sf:Course_Type__c>
        </records>
    </result>
    <Information xmlns="http://ws.apache.org/ns/synapse" xmlns:ns="www.abc.com">
        <queryResponse xmlns="urn:partner.soap.sforce.com">
            <result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="QueryResult">
                <done>true</done>
                <queryLocator xsi:nil="true"/>
                <records xsi:type="sf:sObject">
                    <sf:Class_Begin_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-04-09</sf:Class_Begin_Date__c>
                    <sf:Class_End_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-04-10</sf:Class_End_Date__c>
                    <sf:Class_Location__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Other</sf:Class_Location__c>
                    <sf:Class_Status__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Closed</sf:Class_Status__c>
                    <sf:Course_Name__c xmlns:sf="urn:sobject.partner.soap.sforce.com">a0c20000002Yu</sf:Course_Name__c>
                    <sf:Display_on_the_Website__c xmlns:sf="urn:sobject.partner.soap.sforce.com">false</sf:Display_on_the_Website__c>
                    <sf:Maximum_Attendee__c xmlns:sf="urn:sobject.partner.soap.sforce.com">10.0</sf:Maximum_Attendee__c>
                    <sf:Number_of_Confirmed_Attendees__c xmlns:sf="urn:sobject.partner.soap.sforce.com">8.0</sf:Number_of_Confirmed_Attendees__c>
                    <sf:Placements_Available__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2.0</sf:Placements_Available__c>
                </records>
                <size>1</size>
            </result>
        </queryResponse>
        <queryResponse xmlns="urn:partner.soap.sforce.com">
            <result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="QueryResult">
                <queryResponse xmlns="urn:partner.soap.sforce.com">
                    <result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="QueryResult">
                        <done>true</done>
                        <queryLocator xsi:nil="true"/>
                        <records xsi:type="sf:sObject">
                            <sf:Class_Begin_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-02-07</sf:Class_Begin_Date__c>
                            <sf:Class_End_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-02-08</sf:Class_End_Date__c>
                            <sf:Class_Location__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Other</sf:Class_Location__c>
                            <sf:Class_Status__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Open</sf:Class_Status__c>
                            <sf:Course_Name__c xmlns:sf="urn:sobject.partner.soap.sforce.com">a0c20000002ZI</sf:Course_Name__c>
                            <sf:Display_on_the_Website__c xmlns:sf="urn:sobject.partner.soap.sforce.com">false</sf:Display_on_the_Website__c>
                            <sf:Maximum_Attendee__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Maximum_Attendee__c>
                            <sf:Number_of_Confirmed_Attendees__c xmlns:sf="urn:sobject.partner.soap.sforce.com">0.0</sf:Number_of_Confirmed_Attendees__c>
                            <sf:Placements_Available__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Placements_Available__c>
                        </records>
                        <records xsi:type="sf:sObject">
                            <sf:Class_Begin_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-01-31</sf:Class_Begin_Date__c>
                            <sf:Class_End_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-01-31</sf:Class_End_Date__c>
                            <sf:Class_Location__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Canberra</sf:Class_Location__c>
                            <sf:Class_Status__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Open</sf:Class_Status__c>
                            <sf:Course_Name__c xmlns:sf="urn:sobject.partner.soap.sforce.com">a0c20000002ZI</sf:Course_Name__c>
                            <sf:Display_on_the_Website__c xmlns:sf="urn:sobject.partner.soap.sforce.com">false</sf:Display_on_the_Website__c>
                            <sf:Maximum_Attendee__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Maximum_Attendee__c>
                            <sf:Number_of_Confirmed_Attendees__c xmlns:sf="urn:sobject.partner.soap.sforce.com">0.0</sf:Number_of_Confirmed_Attendees__c>
                            <sf:Placements_Available__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Placements_Available__c>
                        </records>
                        <records xsi:type="sf:sObject">
                            <sf:Class_Begin_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-02-17</sf:Class_Begin_Date__c>
                            <sf:Class_End_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-02-17</sf:Class_End_Date__c>
                            <sf:Class_Location__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Other</sf:Class_Location__c>
                            <sf:Class_Status__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Open</sf:Class_Status__c>
                            <sf:Course_Name__c xmlns:sf="urn:sobject.partner.soap.sforce.com">a0c20000002ZI</sf:Course_Name__c>
                            <sf:Display_on_the_Website__c xmlns:sf="urn:sobject.partner.soap.sforce.com">false</sf:Display_on_the_Website__c>
                            <sf:Maximum_Attendee__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Maximum_Attendee__c>
                            <sf:Number_of_Confirmed_Attendees__c xmlns:sf="urn:sobject.partner.soap.sforce.com">0.0</sf:Number_of_Confirmed_Attendees__c>
                            <sf:Placements_Available__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Placements_Available__c>
                        </records>
                        <records xsi:type="sf:sObject">
                            <sf:Class_Begin_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-03-13</sf:Class_Begin_Date__c>
                            <sf:Class_End_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-03-15</sf:Class_End_Date__c>
                            <sf:Class_Location__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Other</sf:Class_Location__c>
                            <sf:Class_Status__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Open</sf:Class_Status__c>
                            <sf:Course_Name__c xmlns:sf="urn:sobject.partner.soap.sforce.com">a0c20000002ZI</sf:Course_Name__c>
                            <sf:Display_on_the_Website__c xmlns:sf="urn:sobject.partner.soap.sforce.com">false</sf:Display_on_the_Website__c>
                            <sf:Maximum_Attendee__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Maximum_Attendee__c>
                            <sf:Number_of_Confirmed_Attendees__c xmlns:sf="urn:sobject.partner.soap.sforce.com">0.0</sf:Number_of_Confirmed_Attendees__c>
                            <sf:Placements_Available__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Placements_Available__c>
                        </records>
                        <records xsi:type="sf:sObject">
                            <sf:Class_Begin_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-03-07</sf:Class_Begin_Date__c>
                            <sf:Class_End_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-03-11</sf:Class_End_Date__c>
                            <sf:Class_Location__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Other</sf:Class_Location__c>
                            <sf:Class_Status__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Open</sf:Class_Status__c>
                            <sf:Course_Name__c xmlns:sf="urn:sobject.partner.soap.sforce.com">a0c20000002ZI</sf:Course_Name__c>
                            <sf:Display_on_the_Website__c xmlns:sf="urn:sobject.partner.soap.sforce.com">false</sf:Display_on_the_Website__c>
                            <sf:Maximum_Attendee__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Maximum_Attendee__c>
                            <sf:Number_of_Confirmed_Attendees__c xmlns:sf="urn:sobject.partner.soap.sforce.com">0.0</sf:Number_of_Confirmed_Attendees__c>
                            <sf:Placements_Available__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Placements_Available__c>
                        </records>
                        <size>5</size>
                    </result>
                </queryResponse>
            </result>
        </queryResponse>
    </Information>
</queryResponse>

进入以下输出格式。

<queryResponse xmlns="urn:partner.soap.sforce.com">
    <result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="QueryResult">
        <done>true</done>
        <queryLocator xsi:nil="true"/>
        <records xmlns:sf="urn:sobject.partner.soap.sforce.com" xsi:type="sf:sObject">
            <sf:type>Course__c</sf:type>
            <sf:Id>a0c20000002Yu</sf:Id>
            <sf:Id>a0c20000002Yu</sf:Id>
            <sf:CurrencyIsoCode>USD</sf:CurrencyIsoCode>
            <sf:Course_End_Time__c>5 pm</sf:Course_End_Time__c>
            <sf:Course_Start_Time__c>8 am</sf:Course_Start_Time__c>
            <sf:Course_Type__c>Face to Face</sf:Course_Type__c>
            <classes>
                <class>
                    <sf:Class_Begin_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-04-09</sf:Class_Begin_Date__c>
                    <sf:Class_End_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-04-10</sf:Class_End_Date__c>
                    <sf:Class_Location__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Other</sf:Class_Location__c>
                    <sf:Class_Status__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Closed</sf:Class_Status__c>
                    <sf:Course_Name__c xmlns:sf="urn:sobject.partner.soap.sforce.com">a0c20000002Yu</sf:Course_Name__c>
                    <sf:Display_on_the_Website__c xmlns:sf="urn:sobject.partner.soap.sforce.com">false</sf:Display_on_the_Website__c>
                    <sf:Maximum_Attendee__c xmlns:sf="urn:sobject.partner.soap.sforce.com">10.0</sf:Maximum_Attendee__c>
                    <sf:Number_of_Confirmed_Attendees__c xmlns:sf="urn:sobject.partner.soap.sforce.com">8.0</sf:Number_of_Confirmed_Attendees__c>
                    <sf:Placements_Available__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2.0</sf:Placements_Available__c>
                </class>
            </classes>
        </records>
        <records xsi:type="sf:sObject" xmlns:sf="urn:sobject.partner.soap.sforce.com">
            <sf:type>Course__c</sf:type>
            <sf:Id>a0c20000002ZI</sf:Id>
            <sf:Id>a0c20000002ZI</sf:Id>
            <sf:CurrencyIsoCode>AUD</sf:CurrencyIsoCode>
            <sf:Course_End_Time__c>5:00PM</sf:Course_End_Time__c>
            <sf:Course_Start_Time__c>9:00AM</sf:Course_Start_Time__c>
            <sf:Course_Type__c>Face to Face</sf:Course_Type__c>
            <classes>
                <class>
                    <sf:Class_Begin_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-02-07</sf:Class_Begin_Date__c>
                    <sf:Class_End_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-02-08</sf:Class_End_Date__c>
                    <sf:Class_Location__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Other</sf:Class_Location__c>
                    <sf:Class_Status__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Open</sf:Class_Status__c>
                    <sf:Course_Name__c xmlns:sf="urn:sobject.partner.soap.sforce.com">a0c20000002ZI</sf:Course_Name__c>
                    <sf:Display_on_the_Website__c xmlns:sf="urn:sobject.partner.soap.sforce.com">false</sf:Display_on_the_Website__c>
                    <sf:Maximum_Attendee__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Maximum_Attendee__c>
                    <sf:Number_of_Confirmed_Attendees__c xmlns:sf="urn:sobject.partner.soap.sforce.com">0.0</sf:Number_of_Confirmed_Attendees__c>
                    <sf:Placements_Available__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Placements_Available__c>
                </class>
                <class>
                    <sf:Class_Begin_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-01-31</sf:Class_Begin_Date__c>
                    <sf:Class_End_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-01-31</sf:Class_End_Date__c>
                    <sf:Class_Location__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Canberra</sf:Class_Location__c>
                    <sf:Class_Status__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Open</sf:Class_Status__c>
                    <sf:Course_Name__c xmlns:sf="urn:sobject.partner.soap.sforce.com">a0c20000002ZI</sf:Course_Name__c>
                    <sf:Display_on_the_Website__c xmlns:sf="urn:sobject.partner.soap.sforce.com">false</sf:Display_on_the_Website__c>
                    <sf:Maximum_Attendee__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Maximum_Attendee__c>
                    <sf:Number_of_Confirmed_Attendees__c xmlns:sf="urn:sobject.partner.soap.sforce.com">0.0</sf:Number_of_Confirmed_Attendees__c>
                    <sf:Placements_Available__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Placements_Available__c>
                </class>
                <class>
                    <sf:Class_Begin_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-02-17</sf:Class_Begin_Date__c>
                    <sf:Class_End_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-02-17</sf:Class_End_Date__c>
                    <sf:Class_Location__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Other</sf:Class_Location__c>
                    <sf:Class_Status__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Open</sf:Class_Status__c>
                    <sf:Course_Name__c xmlns:sf="urn:sobject.partner.soap.sforce.com">a0c20000002ZI</sf:Course_Name__c>
                    <sf:Display_on_the_Website__c xmlns:sf="urn:sobject.partner.soap.sforce.com">false</sf:Display_on_the_Website__c>
                    <sf:Maximum_Attendee__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Maximum_Attendee__c>
                    <sf:Number_of_Confirmed_Attendees__c xmlns:sf="urn:sobject.partner.soap.sforce.com">0.0</sf:Number_of_Confirmed_Attendees__c>
                    <sf:Placements_Available__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Placements_Available__c>
                </class>
                <class>
                    <sf:Class_Begin_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-03-13</sf:Class_Begin_Date__c>
                    <sf:Class_End_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-03-15</sf:Class_End_Date__c>
                    <sf:Class_Location__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Other</sf:Class_Location__c>
                    <sf:Class_Status__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Open</sf:Class_Status__c>
                    <sf:Course_Name__c xmlns:sf="urn:sobject.partner.soap.sforce.com">a0c20000002ZI</sf:Course_Name__c>
                    <sf:Display_on_the_Website__c xmlns:sf="urn:sobject.partner.soap.sforce.com">false</sf:Display_on_the_Website__c>
                    <sf:Maximum_Attendee__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Maximum_Attendee__c>
                    <sf:Number_of_Confirmed_Attendees__c xmlns:sf="urn:sobject.partner.soap.sforce.com">0.0</sf:Number_of_Confirmed_Attendees__c>
                    <sf:Placements_Available__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Placements_Available__c>
                </class>
                <class>
                    <sf:Class_Begin_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-03-07</sf:Class_Begin_Date__c>
                    <sf:Class_End_Date__c xmlns:sf="urn:sobject.partner.soap.sforce.com">2012-03-11</sf:Class_End_Date__c>
                    <sf:Class_Location__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Other</sf:Class_Location__c>
                    <sf:Class_Status__c xmlns:sf="urn:sobject.partner.soap.sforce.com">Open</sf:Class_Status__c>
                    <sf:Course_Name__c xmlns:sf="urn:sobject.partner.soap.sforce.com">a0c20000002ZI</sf:Course_Name__c>
                    <sf:Display_on_the_Website__c xmlns:sf="urn:sobject.partner.soap.sforce.com">false</sf:Display_on_the_Website__c>
                    <sf:Maximum_Attendee__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Maximum_Attendee__c>
                    <sf:Number_of_Confirmed_Attendees__c xmlns:sf="urn:sobject.partner.soap.sforce.com">0.0</sf:Number_of_Confirmed_Attendees__c>
                    <sf:Placements_Available__c xmlns:sf="urn:sobject.partner.soap.sforce.com">40.0</sf:Placements_Available__c>
                </class>
            </classes>
        </records>
    </result>
</queryResponse>

为了这个目的,我试图调整一个简单例子[1]中给出的xslt。我的xslt看起来像这样。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:syn="http://ws.apache.org/ns/synapse"
    exclude-result-prefixes="syn" xmlns:sf="urn:sobject.partner.soap.sforce.com"
    xmlns:sfrsp="urn:partner.soap.sforce.com">
    <xsl:output method="xml" version="1.0" encoding="UTF-8"
        indent="yes" />
    <xsl:strip-space elements="*" />

    <xsl:key name="child"
        match="//sfrsp:queryResponse/syn:Information/sfrsp:queryResponse[1]/sfrsp:result/sfrsp:records[1]"
        use="//sfrsp:queryResponse/syn:Information/sfrsp:queryResponse[1]/sfrsp:result/sfrsp:records[1]/sf:Course_Name__c" />

    <xsl:template match="@*|node()">
        <xsl:copy copy-namespaces="no">
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="//sfrsp:queryResponse/sfrsp:result/sfrsp:records[1]">
        <xsl:copy>
            <xsl:apply-templates />
            <Children>
                <xsl:apply-templates
                    select="key('child', //sfrsp:queryResponse/sfrsp:result/sfrsp:records[1]/sf:Id[1])" />
            </Children>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="syn:Information" />

</xsl:stylesheet>

但它没有按预期转换xml。任何帮助,将不胜感激。 Course_Name__c与父母的Id值相似的所有孩子都需要在父元素下。

[1] Correlating related items using XSLT

1 个答案:

答案 0 :(得分:1)

对于密钥使用,匹配元素是上下文节点。因此,请尝试将密钥更改为:

<xsl:key name="child"
    match="//sfrsp:queryResponse/syn:Information/sfrsp:queryResponse[1]/sfrsp:result/sfrsp:records[1]"
    use="sf:Course_Name__c" />

而不是用它:

 <xsl:apply-templates
                select="key('child', sf:Id[1])" />