0

When i'm trying to process data out of an xml file I'm facing this problem: I cant find a way to append the data from the for- each statement.I would like to send them as parameters to the js function.

Here is sample of the xml:

 <events>
<event>
    <eventType>some type</eventType>
    <date>12/24/1999</date>
    <participants>
        <participant>
            <participant_name>Mike</participant_name>
        </participant>
        <participant>
            <participant_name>John</participant_name>
        </participant>
    </participants>

. . .

and here is the xsl file:

<xsl:for-each select="event">

<tr>                    

    <xsl:variable name="eventType"select="eventType"> </xsl:variable>

    <xsl:variable name="date" select="date"></xsl:variable>

    <td>    
        <xsl:value-of select="$eventType" />    
    </td>

    <td>
        <xsl:value-of select="$date" /> 
    </td>

    <xsl:for-each select="participants/participant">
    <td>
    <xsl:value-of select="participant_name"/>   
    </td>
    </xsl:for-each>

    <td>
        <button type="button" onClick="myFunc('{$date} {$eventType}')">

    </td>

    </tr>

My goal is to create a table when the last column will be a button which sends all the row parameters to js function.The code above is working fine, but I don't seem to find a way to append the participant_name because of its scope. I searched for a solution and didn't find it. hope to find help here, thanks.

shultz8
  • 11
  • 3

1 Answers1

0

You are very close - you are already in scope of participant, so you just need to navigate down to participant_name:

<xsl:for-each select="participants/participant">
  <td>
    <xsl:value-of select="participant_name" />
  </td>
</xsl:for-each>

Edit

I've just re-read your question and I believe I missed the part about sending to your javascript function? If so, you can build up the participants in another <xsl:variable>

<xsl:variable name="participants">
  <xsl:for-each select="participants/participant">
     <xsl:value-of select="participant_name" />
    <xsl:if test="not(position() = last())">
      <xsl:text>,</xsl:text>
    </xsl:if>
  </xsl:for-each>
</xsl:variable>

<button type="button" onClick="myFunc('{$date} {$eventType} {$participants}')" />

Last Comma removal courtesy of here

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • Sorry, now it is the correct code. the code is fine as it is. the issue is how do I transfer the particpant_name like I did in the other element's data to myFunc() – shultz8 Jan 05 '15 at 11:18
  • Thank you very much for the quick reply. Its working in terms of the js function. But do I have a way to display the prticipant_name in the columns? I mean to add inside that code block? – shultz8 Jan 05 '15 at 11:43
  • You might want to include the desired output HTML to your question. And *please*, indent your XSLT code properly. – Tomalak Jan 05 '15 at 11:49
  • There seem to be two distinct requirements here 1) Emit a list of td's and 2) Create a comma separated list for the js Function. Assuming you don't want the `` passed to javascript, you would need to repeat the `` bit and include a `td` wrapper around each participant which was omitted? One other thing worth mentioning is that we'll also need to [escape out single quotes](http://stackoverflow.com/questions/7711654/xslt-replace-single-quotes-by) in the participant's names for your js function (e.g. to accomodate names like `O'Hara`) – StuartLC Jan 05 '15 at 11:53