Greg Blodgett
Greg Blodgett

Reputation: 11

Using XSL and need to concatenate multiple returned data elements from a for-each select

I have XML output like the below and there are multiple records for Locations, the ask is to have all of these locations concatenated together. XML looks like this:

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:getDocumentByKeyResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://www.taleo.com/ws/integration/toolkit/2005/07"><Document xmlns="http://www.taleo.com/ws/integration/toolkit/2005/07"><Attributes><Attribute name="count">1</Attribute><Attribute name="duration">0:00:00.407</Attribute><Attribute name="entity">SourcingRequest</Attribute><Attribute name="mode">T-XML</Attribute><Attribute name="version">http://www.taleo.com/ws/tee800/2009/01</Attribute></Attributes><Content>
<ExportTXML xmlns="http://www.taleo.com/ws/integration/toolkit/2005/07" xmlns:e="http://www.taleo.com/ws/tee800/2009/01">
    <e:SourcingRequest>
        <e:Requisition>
            <e:Requisition>
                <e:ContestNumber>1604860</e:ContestNumber>
                <e:JobInformation>
                    <e:JobInformation>
                        <e:OtherLocations>
                            <e:Location>
                                <e:NetworkLocation>
                                    <e:NetworkLocation>
                                        <e:CountryLocation>
                                            <e:NetworkLocation>
                                                <e:Abbreviation>US</e:Abbreviation>
                                            </e:NetworkLocation>
                                        </e:CountryLocation>
                                        <e:StateLocation>
                                            <e:NetworkLocation>
                                                <e:Abbreviation>VA</e:Abbreviation>
                                            </e:NetworkLocation>
                                        </e:StateLocation>
                                    </e:NetworkLocation>
                                </e:NetworkLocation>
                                <e:WorkLocation>
                                    <e:WorkLocation>
                                        <e:City>Fredericksburg</e:City>
                                    </e:WorkLocation>
                                </e:WorkLocation>
                            </e:Location>
                            <e:Location>
                                <e:NetworkLocation>
                                    <e:NetworkLocation>
                                        <e:CountryLocation>
                                            <e:NetworkLocation>
                                                <e:Abbreviation>US</e:Abbreviation>
                                            </e:NetworkLocation>
                                        </e:CountryLocation>
                                        <e:StateLocation>
                                            <e:NetworkLocation>
                                                <e:Abbreviation>VA</e:Abbreviation>
                                            </e:NetworkLocation>
                                        </e:StateLocation>
                                    </e:NetworkLocation>
                                </e:NetworkLocation>
                                <e:WorkLocation>
                                    <e:WorkLocation>
                                        <e:City>Manassas</e:City>
                                    </e:WorkLocation>
                                </e:WorkLocation>
                            </e:Location>
                        </e:OtherLocations>
                    </e:JobInformation>
                </e:JobInformation>
            </e:Requisition>
        </e:Requisition>
        <e:RuntimeFields>
            <e:RuntimeField name="Address1"/>
            <e:RuntimeField name="Category">Corporate</e:RuntimeField>
            <e:RuntimeField name="City"/>
            <e:RuntimeField name="Lat"/>
            <e:RuntimeField name="Long"/>
            <e:RuntimeField name="PostedDate">2016-06-30</e:RuntimeField>
            <e:RuntimeField name="State"/>
            <e:RuntimeField name="country">US</e:RuntimeField>
            <e:RuntimeField name="language">E</e:RuntimeField>
            <e:RuntimeField name="zip"/>
        </e:RuntimeFields>
    </e:SourcingRequest>
</ExportTXML></Content></Document></ns1:getDocumentByKeyResponse></soapenv:Body></soapenv:Envelope>

My XSL looks like so:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:e="http://www.taleo.com/ws/tee800/2009/01" xmlns:fct="http://www.taleo.com/xsl_functions" exclude-result-prefixes="e fct">
  <xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="no"/>
  <xsl:template match="/">
      <Client>
      <xsl:apply-templates select="//e:SourcingRequest"/>
      </Client>
  </xsl:template>
  <xsl:template match="e:SourcingRequest">
    <xsl:variable name="Wegmans" select="e:RuntimeFields/e:RuntimeField[@name='client']"/>
    <xsl:variable name="Title" select="e:Requisition/e:Requisition/e:JobInformation/e:JobInformation/e:Title"/>
    <xsl:variable name="Req_Number" select="e:Requisition/e:Requisition/e:ContestNumber"/>
    <xsl:variable name="Posted_Date" select="e:RuntimeFields/e:RuntimeField[@name='PostedDate']"/>
    <xsl:variable name="Language" select="e:RuntimeFields/e:RuntimeField[@name='language']"/>
    <xsl:variable name="State_Province" select="e:RuntimeFields/e:RuntimeField[@name='State']"/>
    <xsl:variable name="AddressLine1" select="e:RuntimeFields/e:RuntimeField[@name='Address1']"/>
    <xsl:variable name="City" select="e:RuntimeFields/e:RuntimeField[@name='City']"/>
    <xsl:variable name="Zip" select="e:RuntimeFields/e:RuntimeField[@name='zip']"/>
    <xsl:variable name="Country" select="e:RuntimeFields/e:RuntimeField[@name='country']"/>
    <xsl:variable name="Lat" select="e:RuntimeFields/e:RuntimeField[@name='Lat']"/>
    <xsl:variable name="Long" select="e:RuntimeFields/e:RuntimeField[@name='Long']"/>
    <xsl:variable name="Category" select="e:RuntimeFields/e:RuntimeField[@name='Category']"/>
    <xsl:variable name="JobQualifications" select="e:Requisition/e:Requisition/e:JobInformation/e:JobInformation/e:ExternalQualificationHTML"/>
    <xsl:variable name="JobText" select="e:Requisition/e:Requisition/e:JobInformation/e:JobInformation/e:DescriptionExternalHTML"/>
    <xsl:variable name="AgeRequirement" select="e:Requisition/e:Requisition/e:JobInformation/e:JobInformation/e:UDFs/e:UDF[@name='Age_5fRequirement_5fW']/e:UDSElement/e:Description"/>
    <xsl:variable name="DaysHoursReq" select="e:Requisition/e:Requisition/e:JobInformation/e:JobInformation/e:UDFs/e:UDF[@name='Days_5fHours_20Requirement_5fW']"/>
    <xsl:variable name="Schedule" select="e:Requisition/e:Requisition/e:JobInformation/e:JobInformation/e:JobSchedule/e:JobSchedule/e:Description"/>
    Wegmans
    <job>
      <Title>
        <xsl:value-of select="$Title"/>
      </Title>
      <Req_Number>
        <xsl:value-of select="$Req_Number"/>
      </Req_Number>
      <Posted_date>
        <xsl:value-of select="$Posted_Date"/>
      </Posted_date>
      <Language>
        <xsl:value-of select="$Language"/>
      </Language>
      <AddressLine1>
        <xsl:value-of select="$AddressLine1"/>
      </AddressLine1>
      <City>
        <xsl:value-of select="$City"/>
      </City>
      <State_Province>
        <xsl:value-of select="$State_Province"/>
      </State_Province>
      <Zip>
        <xsl:value-of select="$Zip"/>
      </Zip>
      <Country>
        <xsl:value-of select="$Country"/>
      </Country>
      <xsl:for-each select="e:Requisition/e:Requisition/e:JobInformation/e:JobInformation/e:OtherLocations/e:Location">
                <xsl:variable name="OtherCity" select="e:WorkLocation/e:WorkLocation/e:City"/>
                <xsl:variable name="OtherState" select="e:NetworkLocation/e:NetworkLocation/e:StateLocation/e:NetworkLocation/e:Abbreviation"/>
                <xsl:variable name="OtherCountry" select="e:NetworkLocation/e:NetworkLocation/e:CountryLocation/e:NetworkLocation/e:Abbreviation"/>
            <OtherLocations>
                <xsl:value-of select="concat($OtherCity,'-',$OtherState,'-',$OtherCountry)"/>
            </OtherLocations>
        </xsl:for-each>
        <Lat>
        <xsl:value-of select="$Lat"/>
      </Lat>
      <Long>
        <xsl:value-of select="$Long"/>
      </Long>
      <Category>
        <xsl:value-of select="$Category"/>
      </Category>
      <JobQualifications>
        <xsl:value-of select="$JobQualifications"/>
      </JobQualifications>
      <JobText>
        <xsl:value-of select="$JobText"/>
      </JobText>
      <AgeRequirement>
        <xsl:value-of select="$AgeRequirement"/>
      </AgeRequirement>
      <DaysHoursReq>
        <xsl:value-of select="$DaysHoursReq"/>
      </DaysHoursReq>
      <Schedule>
        <xsl:value-of select="$Schedule"/>
      </Schedule>
      <ApplyUrl>
        <xsl:value-of select="concat('https://wegmans.taleo.net/careersection/2/jobapply.ftl?job=',$Req_Number)"/>
      </ApplyUrl>
</job>        
      </xsl:template>
      </xsl:stylesheet>

Current output based on the XSL above gives this:

<AddressLine1/>
<City/>
<State_Province/>
<Zip/>
<Country>US</Country>
<OtherLocations>Fredericksburg-VA-US</OtherLocations>
<OtherLocations>Manassas-VA-US</OtherLocations>
<OtherLocations>Leesburg-VA-US</OtherLocations>
<OtherLocations>Alexandria--US</OtherLocations>
<OtherLocations>Leesburg-VA-US</OtherLocations>
<OtherLocations>Hanover-VA-US</OtherLocations>
<OtherLocations>Fairfax-VA-US</OtherLocations>
<Lat/>

I need it to look like so:

<AddressLine1/>
<City/>
<State_Province/>
<Zip/>
<Country>US</Country>
<OtherLocations>Fredericksburg-VA-US,Manassas-VA-US,Leesburg-VA-US,Alexandria--US,Leesburg-VA-US,Hanover-VA-US,Fairfax-VA-US</OtherLocations>
<Lat/>

Upvotes: 0

Views: 69

Answers (1)

Tim C
Tim C

Reputation: 70638

Simply move the creation of the <OtherLocations> tag outside the xsl:for-each construct, and have a check inside to output a comma before all items except the first.

Try this XSLT fragment...

<OtherLocations>
    <xsl:for-each select="e:Requisition/e:Requisition/e:JobInformation/e:JobInformation/e:OtherLocations/e:Location">
        <xsl:variable name="OtherCity" select="e:WorkLocation/e:WorkLocation/e:City"/>
        <xsl:variable name="OtherState" select="e:NetworkLocation/e:NetworkLocation/e:StateLocation/e:NetworkLocation/e:Abbreviation"/>
        <xsl:variable name="OtherCountry" select="e:NetworkLocation/e:NetworkLocation/e:CountryLocation/e:NetworkLocation/e:Abbreviation"/>
        <xsl:if test="position() > 1">,</xsl:if>
        <xsl:value-of select="concat($OtherCity,'-',$OtherState,'-',$OtherCountry)"/>
    </xsl:for-each>
</OtherLocations>

Do note your current XML sample only has two locations though.....

Upvotes: 1

Related Questions