0

This seems pretty simple, but I'm missing to generate the required output. Any help would be appreciated.

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<Event xmlns="urn:sobject.partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Id>00Uc0000001iioEEAQ</Id>
<Subject>TestMeeting2</Subject>
<StartDateTime xsi:nil="true"/>
<Owner>
    <Name>dev0044</Name>
    <Email>testuser0044@gmail.com</Email>
</Owner>
<Notes_Universal_ID__c xsi:nil="true"/>
<Location>San Jose</Location>
<IsPrivate>false</IsPrivate>
<IsDeleted>false</IsDeleted>
<EndDateTime xsi:nil="true"/>
<Description>test description</Description>
<EventRelations>
    <EventRelation>
        <EventId>00Uc0000001iioEEAQ</EventId>
        <RelationId>005c0000000T9YQAA0</RelationId>
        <Relation>
            <Email>testuser0045@gmail.com</Email>
        </Relation>
    </EventRelation>
    <EventRelation>
        <EventId>00Uc0000001iioEEAQ</EventId>
        <RelationId>005c0000000T9YaAAK</RelationId>
        <Relation>
            <Email>testuser0047@gmail.com</Email>
        </Relation>
    </EventRelation>
</EventRelations>

Required Output XML:

<?xml version="1.0" encoding="UTF-8"?>
<CREATEAPPOINTMENT>
<STRUSERNAME>testuser0044</STRUSERNAME>
<STRSTARTDATETIME/>
<STRENDDATETIME/>
<STRSUBJECT>TestMeeting2</STRSUBJECT>
<STRDESCRIPTION>Email : testuser0045@gmail.com, testuser0047@gmail.com. Description:    test description</STRDESCRIPTION>
<SRTLOCATION>San Jose</SRTLOCATION>
<STRPRIVATE>0</STRPRIVATE>
<STRAVAILABLE>0</STRAVAILABLE>
</CREATEAPPOINTMENT>

I'm able to get all except for the description field. The STRDESCRIPTION will contain a concatenation of EventRelations/EventRelation/Relation/Email (comma separated email addresses) and the actual Description field.

Here's the xsl that I'm trying with:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
xmlns:sf="urn:partner.soap.sforce.com" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/">
<xsl:output indent="yes" method="xml"/>

<xsl:template match="*:Event">
    <xsl:element name="CREATEAPPOINTMENT">
        <xsl:element name="STRUSERNAME"><xsl:value-of select="*:Owner/*:Name"/></xsl:element>
        <xsl:element name="STRSTARTDATETIME"><xsl:value-of select="*:StartDateTime"/></xsl:element>
        <xsl:element name="STRENDDATETIME"><xsl:value-of select="*:EndDateTime"/></xsl:element>
        <xsl:element name="STRSUBJECT"><xsl:value-of select="*:Subject"/></xsl:element>
        <xsl:variable name="description" select="*:Description"/>

        <xsl:element name="SRTDESCRIPTION">
        <xsl:for-each select="*:EventRelations/*:EventRelation">
            <xsl:variable name="email" select="*:Relation/*:Email"/>
                <!-- <xsl:value-of select="concat('Email : ', $email, ' : Description : ')"></xsl:value-of> -->
            <xsl:value-of select="$email"/>
            <xsl:text>, </xsl:text>
            <xsl:value-of select="$description"></xsl:value-of>
            <xsl:text>, </xsl:text>
        </xsl:for-each>
        </xsl:element>

        <xsl:element name="SRTLOCATION"><xsl:value-of select="*:Location"/></xsl:element>

        <xsl:choose>
            <xsl:when test="*:IsPrivate = 'false'">
                <xsl:element name="STRPRIVATE">0</xsl:element>
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="STRPRIVATE">1</xsl:element>
            </xsl:otherwise>
        </xsl:choose>

        <xsl:choose>
            <xsl:when test="*:IsPrivate = 'false'">
                <xsl:element name="STRAVAILABLE">0</xsl:element>
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="STRAVAILABLE">1</xsl:element>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:element>
</xsl:template>

4

1 回答 1

1

由于您使用的是 XSLT 2.0,因此您可以separator使用xsl:value-of. 您也可以使用xpath-default-namespace,因此您不必使用*作为前缀。

例子:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xpath-default-namespace="urn:sobject.partner.soap.sforce.com">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="text()"/>

    <xsl:template match="/*">
        <CREATEAPPOINTMENT>
            <xsl:apply-templates select="(Owner,StartDateTime,EndDateTime,
                Subject,Description,Location,IsPrivate)"/>
        </CREATEAPPOINTMENT>
    </xsl:template>

    <xsl:template match="Owner/Name">
        <STRUSERNAME><xsl:value-of select="."/></STRUSERNAME>
    </xsl:template>

    <xsl:template match="StartDateTime">
        <STRSTARTDATETIME><xsl:value-of select="."/></STRSTARTDATETIME>
    </xsl:template>

    <xsl:template match="EndDateTime">
        <STRENDDATETIME><xsl:value-of select="."/></STRENDDATETIME>
    </xsl:template>

    <xsl:template match="Subject">
        <STRSUBJECT><xsl:value-of select="."/></STRSUBJECT>
    </xsl:template>

    <xsl:template match="Description">
        <STRDESCRIPTION>
            <xsl:text>Email: </xsl:text>
            <xsl:value-of select="../EventRelations/*/Relation/Email" separator=", "/>
            <xsl:text> DESCRIPTION: </xsl:text>
            <xsl:value-of select="."/>
        </STRDESCRIPTION>
    </xsl:template>

    <xsl:template match="Location">
        <SRTLOCATION><xsl:value-of select="."/></SRTLOCATION>
    </xsl:template>

    <xsl:template match="IsPrivate">
        <STRPRIVATE><xsl:apply-templates/></STRPRIVATE>
        <STRAVAILABLE><xsl:apply-templates/></STRAVAILABLE>
    </xsl:template>

    <xsl:template match="IsPrivate/text()">
        <xsl:choose>
            <xsl:when test=".='false'"><xsl:text>0</xsl:text></xsl:when>
            <xsl:otherwise><xsl:text>1</xsl:text></xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

输出

<CREATEAPPOINTMENT>
   <STRUSERNAME>dev0044</STRUSERNAME>
   <STRSTARTDATETIME/>
   <STRENDDATETIME/>
   <STRSUBJECT>TestMeeting2</STRSUBJECT>
   <STRDESCRIPTION>Email: testuser0045@gmail.com, testuser0047@gmail.com DESCRIPTION: test description</STRDESCRIPTION>
   <SRTLOCATION>San Jose</SRTLOCATION>
   <STRPRIVATE>0</STRPRIVATE>
   <STRAVAILABLE>0</STRAVAILABLE>
</CREATEAPPOINTMENT>

注意:元素的顺序与您问题中的输出不同。如果顺序很重要,您可以在指定顺序的select模板中添加一个。(如果有帮助,我可以修改它。)xsl:apply-templates/*

此外,如果您通过在开头添加“STR”来创建新元素,则可以用以下模板替换StartDateTimeEndDateTimeSubject和的模板Location

<xsl:template match="StartDateTime|EndDateTime|Subject|Location">
    <xsl:element name="STR{upper-case(local-name())}">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>
于 2013-06-13T18:26:49.603 回答