RE: [xsl] Output multiple occurence into one and concatenate values

Subject: RE: [xsl] Output multiple occurence into one and concatenate values
From: "Shaikh, Parvez" <Parvez.Shaikh@xxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 29 Mar 2007 13:57:37 -0400
I am an XSL newbie. Can someone please respond?


I have given my XML and XSL below. The rest all is taken care of. All I
want to do is Combine all cost_allocation_product into one and append
all values for the attributes when doing thiss.

Expected Result:
<cost_allocation_product>
<dbfield name ="cost_allocation_product_project" value= "1725 -
Project1,1774 - Project2,1755 - Project3" />
<dbfield name="cost_allocation_product_percent" value="12,1,87" />
</cost_allocation_product>

This is my XML:
 <?xml version="1.0" encoding="utf-8"?>
<payload id="eRequest.Purchase Desktop or Laptop Inventory.Initiate">
  <erpayload>
    <basicinfo basicinfo_requestPriority="Normal"
basicinfo_requestExpectedDate="" />
    <genericrequestdetail genericrequestdetail_description="testing"
genericrequestdetail_include_section="true" />
    <purchasing purchasing_purchasing_status="   "
purchasing_acknowleged="" purchasing_btn_Add_CER_PO=""
purchasing_details="" purchasing_gl_account="12n (12233)"
purchasing_capital_sequence_number="82"
purchasing_gl_project="170System" purchasing_budget_year="26"
purchasing_item_description="testing" purchasing_amount="0.00"
purchasing_include_section="true">
      <add_CER_PO id="0" cer_number="" cer_created="" po_number=""
po_created="" Vendor="" />
      <cost_allocation_product id="0" project="170upport" percent="12"
/>
      <cost_allocation_product id="1" project="171stem" percent="1" />
      <cost_allocation_product id="2" project="170tem" percent="87" />
    </purchasing>
    <generalcomments generalcomments_comments=""
generalcomments_copyToChild="true"
generalcomments_include_section="true" />
    <attachments attachments_copyToChild="true"
attachments_include_section="true" />
    <promoted-property />
  </erpayload>
</payload>

MY XSL:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:output method="xml"/>
  <xsl:template match="/">
    <erpayload>
      <xsl:apply-templates select="//erpayload/* | //erpayload/*/*"/>
    </erpayload>
  </xsl:template>

  <xsl:template match="basicinfo">
    <xsl:copy-of select="."/>
    <basicinfocondensed>
      <xsl:apply-templates select="@basicinfo_initiatorFullName"/>
      <xsl:apply-templates select="@basicinfo_initiatorPhone"/>
      <xsl:apply-templates select="@basicinfo_requestedForFullName"/>
      <xsl:apply-templates select="@basicinfo_requestedForPhone"/>
      <xsl:apply-templates select="@basicinfo_department_name"/>
      <xsl:apply-templates select="@basicinfo_location"/>
    </basicinfocondensed>
  </xsl:template>

  <xsl:template match="generalcomments">
    <xsl:copy-of select="."/>
  </xsl:template>

  <xsl:template match="add_CER_PO">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="@cer_number"/>
      <xsl:apply-templates select="@cer_created"/>
      <xsl:apply-templates select="@po_number"/>
      <xsl:apply-templates select="@po_created"/>
      <xsl:apply-templates select="@Vendor"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="@cer_number | @cer_created | @po_number |
@po_created | @Vendor">
    <dbField>
      <xsl:attribute name="name">
        <xsl:value-of select="concat('add_CER_PO_',node())"/>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </dbField>
  </xsl:template>

  <xsl:template match="cost_allocation_factor" >
    <xsl:value-of select="." />
    <xsl:text>,</xsl:text>
  </xsl:template>



  <xsl:template match="*">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="@*"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="@*">
    <dbField>
      <xsl:attribute name="name">
        <xsl:value-of select="name()"/>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </dbField>
  </xsl:template>



  <xsl:template match="@basicinfo_initiatorFullName">
    <dbField>
      <xsl:attribute name="name">
        <xsl:value-of select="name()"/>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </dbField>
  </xsl:template>

  <xsl:template match="@basicinfo_initiatorPhone">
    <dbField>
      <xsl:attribute name="name">
        <xsl:value-of select="name()"/>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </dbField>
  </xsl:template>

  <xsl:template match="@basicinfo_requestedForFullName">
    <dbField>
      <xsl:attribute name="name">
        <xsl:value-of select="name()"/>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </dbField>
  </xsl:template>

  <xsl:template match="@basicinfo_requestedForPhone">
    <dbField>
      <xsl:attribute name="name">
        <xsl:value-of select="name()"/>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </dbField>
  </xsl:template>

  <xsl:template match="@basicinfo_initiatorFullName">
    <dbField>
      <xsl:attribute name="name">
        <xsl:value-of select="name()"/>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </dbField>
  </xsl:template>

  <xsl:template match="@basicinfo_initiatorFullName">
    <dbField>
      <xsl:attribute name="name">
        <xsl:value-of select="name()"/>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </dbField>
  </xsl:template>

  <xsl:template match="@basicinfo_department_name">
    <dbField>
      <xsl:attribute name="name">
        <xsl:value-of select="name()"/>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </dbField>
  </xsl:template>

  <xsl:template match="@basicinfo_location">
    <dbField>
      <xsl:attribute name="name">
        <xsl:value-of select="name()"/>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </dbField>
  </xsl:template>

  <xsl:template match="@subrequest">
    <dbField>
      <xsl:attribute name="name">
        <xsl:value-of select="concat(name(parent::node()),
'_subrequest')"/>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </dbField>
  </xsl:template>
  <xsl:template match="@subrequestguid">
    <dbField>
      <xsl:attribute name="name">
        <xsl:value-of select="concat(name(parent::node()),
'_subrequestguid')"/>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </dbField>
  </xsl:template>
</xsl:stylesheet>


-----Original Message-----
From: Abel Braaksma [mailto:abel.online@xxxxxxxxx]
Sent: Thursday, March 29, 2007 12:41 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Output multiple occurence into one and concatenate
values

Shaikh, Parvez wrote:
>
> I have an XML that has 2 or more occurences of data. I want to comma
>
> delimit the attribute values and output it only once. How do you do
that

You did not include your XSLT, so I have no clues as to how you go wrong

or what you tried. Do you use XSLT 1 or 2? In XSLT 2 you can do this:

<xsl:value-of select="//@project" separator="," />

make sure you place it in the right context. But to help you with that,
we need to have a look at your current XSLT and what it currently
(wrongly) produces.

In XSLT 1, btw, you can simply use apply-templates on the selection of
the attribute nodes and do this in the matching template:

<xsl:template match="@project" >
    <xsl:value-of select="." />
    <xsl:text>,</xsl:text>
</xsl:template>


Cheers,
-- Abel Braaksma



-----------------------------------------
The information contained in this transmission may be privileged and
confidential and is intended only for the use of the person(s) named
above. If you are not the intended recipient, or an employee or agent
responsible
for delivering this message to the intended recipient, any review,
dissemination,
distribution or duplication of this communication is strictly
prohibited. If you are
not the intended recipient, please contact the sender immediately by
reply e-mail
and destroy all copies of the original message. Please note that we do
not accept
account orders and/or instructions by e-mail, and therefore will not be
responsible
for carrying out such orders and/or instructions.  If you, as the
intended recipient
of this message, the purpose of which is to inform and update our
clients, prospects
and consultants of developments relating to our services and products,
would not
like to receive further e-mail correspondence from the sender, please
"reply" to the
sender indicating your wishes.  In the U.S.: 1345 Avenue of the
Americas, New York,
NY 10105.

Current Thread