使用XSL并需要从for-each选中连接多个返回的数据元素

时间:2016-09-01 20:56:04

标签: xml xslt

我有如下所示的XML输出,并且有多个Locations记录,要求是将所有这些位置连接在一起。 XML看起来像这样:

<?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>

我的XSL看起来像这样:

<?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>

基于上面的XSL的当前输出给出了:

<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/>

我需要它看起来像这样:

<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/>

1 个答案:

答案 0 :(得分:1)

只需在<OtherLocations>构造之外移动xsl:for-each标记的创建,并在里面检查以在除第一个之外的所有项目之前输出逗号。

试试这个XSLT片段......

<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>

请注意,您当前的XML示例只有两个位置,但是......