xsl-list
[Top] [All Lists]

RE: [xsl] Extracting element name and value as separate

2007-02-16 05:55:18
Srinivasan,

If you have any control over the XML format it may help to decide whether to
use attributes or elements.

As a quick fix you can use //@ to select all child attributes. It's a bit
blunt but it works.

Your XML is.

<state_results state_name="Atlantic Canada" state_id="AC">
        <game_results game_name="Lotto 6/49" game_id="201">
                <date>
                        <abbr_date>02/03/07</abbr_date>
                        <full_date>02/03/2007</full_date>
                </date>
                <number_str>10-13-24-27-37-39, Bonus: 14</number_str>
                <numbers>
                        <number>10</number>
                        <number>13</number>
                </numbers>
                <bonus>
                        <bonus_name>Bonus</bonus_name>
                        <bonus_number>14</bonus_number>
                </bonus>
                <next_date>
                        <next_abbr_date>02/07/07</next_abbr_date>
                        <next_full_date>02/07/2007</next_full_date>
                </next_date>
        </game_results>
        <requested_action>ADD</requested_action>
</state_results>

It have repeated it here because it is easier to see the format.
For test purposes I have:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
        <xsl:template match="state_results">
                <table border="1">
                        <tbody>
                                <tr>
                                        <th>name</th>
                                        <th>value</th>
                                </tr>
                                <xsl:for-each select="//@*">
                                        <tr>
                                                <td>
                                                        <xsl:value-of
select="name()"/>
                                                </td>
                                                <td>
                                                        <xsl:value-of
select="."/>
                                                </td>
                                        </tr>
                                </xsl:for-each>
                        </tbody>
                </table>
                Desired output
                
                        <table border="1">
                        <tbody>
                                <tr>
                                        <th>name</th>
                                        <th>value</th>
                                </tr>
                                <tr>
                                        <td>state_name</td>
                                        <td>Atlantic Canada</td>
                                </tr>
                                <tr>
                                        <td>state_id</td>
                                        <td>AC</td>
                                </tr>
                                <tr>
                                        <td>game_name</td>
                                        <td>Lotto 6/49</td>
                                </tr>
                                <tr>
                                        <td>game_id</td>
                                        <td>201</td>
                                </tr>
                                <tr>
                                        <td>abbr_date</td>
                                        <td>02/03/07</td>
                                </tr>
                                <tr>
                                        <td>full_date</td>
                                        <td>02/03/2007</td>
                                </tr>
                                <tr>
                                        <td>number_str</td>
                                        <td>10-13-24-27-37-39, Bonus:
14</td>
                                </tr>
                                <tr>
                                        <td>next_abbr_date</td>
                                        <td>02/07/07</td>
                                </tr>
                        </tbody>
                </table>
        </xsl:template>
</xsl:stylesheet>

As you can see the child of child elements are not matched.

If you can change the elements to attributes like:
<date abbr_date="02/03/07" full_date="02/03/2007"/>

Then you will find it works.


William Charlton
The yMonda team
yMonda Limited
w: www.ymonda.net


-----Original Message-----
From: Thangavelu Srinivasan [mailto:vasantry(_at_)gmail(_dot_)com] 
Sent: 2007 February 16 12:20
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Extracting element name and value as separate

Hi Folks

I am using xslt 1.0. I am facing the problem in extracting the element
name and value separately. Any suggestions are welcome

Sample XML:

<state_results state_name="Atlantic Canada" state_id="AC">
- <game_results game_name="Lotto 6/49" game_id="201">
- <date>
  <abbr_date>02/03/07</abbr_date>
  <full_date>02/03/2007</full_date>
  </date>
  <number_str>10-13-24-27-37-39, Bonus: 14</number_str>
- <numbers>
  <number>10</number>
  <number>13</number>
  </numbers>
- <bonus>
  <bonus_name>Bonus</bonus_name>
  <bonus_number>14</bonus_number>
  </bonus>
- <next_date>
  <next_abbr_date>02/07/07</next_abbr_date>
  <next_full_date>02/07/2007</next_full_date>
  </next_date>
  </game_results>
  <requested_action>ADD</requested_action>
  </state_results>

My XSL:
<xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:template match="state_results">
<xsl:for-each select="@*">
<Name><xsl:value-of select="name()"/></Name>
<Value><xsl:value-of select="."/></Value>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Required Output :

<Name>state_name</Name>
<Value>Atlantic Canada</Value>
<Name>state_id</Name>
<Value>AC</Value>
<Name>game_name</Name>
<Value>Lotto 6/49</Value>
<Name>game_id</Name>
<Value>201</Value>
<Name>abbr_date</Name>
<Value>02/03/07</Value>
<Name>full_date</Name>
<Value>02/03/2007</Value>
<Name>number_str</Name>
<Value>10-13-24-27-37-39, Bonus: 14</Value>
<Name>next_abbr_date</Name>
<Value>02/07/07</Value>

I am not able to get desired output.

Regards
Srinivas

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>