XSL Query

Discussion in 'XML' started by Sandy, Dec 29, 2005.

  1. Sandy

    Sandy Guest

    I want to genereate a csv file form an XML as shown below, and the content
    of csv are as follows
    <DATE>,<first ATOM/NAME>,<second ATOM/NAME>

    I am able to select first and the second NAME element, but the problem is
    that when number of instances of <NAME> is<2 I want the output to be
    <DATE>,, (at least two comma)
    But since the elements are not present so the rule does not execute and i
    don't get any comma in the output.csv

    Please suggest what should be done.

    The files i used are as follows.

    *************** Input XML
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xml" href="xml.xsl"?>
    <PERIODIC_TABLE>
    <DATE>12/29/2005</DATE>

    <ATOMS>
    <NAME>Hydrogen</NAME>
    </ATOMS>

    <ATOMS>
    <NAME>Helium</NAME>
    <NAME>Neon</NAME>
    </ATOMS>

    <ATOMS>
    <NAME>Carbon</NAME>
    <NAME>Oxygen</NAME>
    <NAME>Copper</NAME>
    <NAME>Iron</NAME>
    <NAME>Argon</NAME>
    </ATOMS>

    <ATOMS></ATOMS>

    </PERIODIC_TABLE>

    ********************* The XSL

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

    <xsl:template match="PERIODIC_TABLE">
    <xsl:apply-templates select="ATOMS"/>
    </xsl:template>

    <xsl:template match="ATOMS">
    <xsl:apply-templates select="NAME"/><xsl:text>
    </xsl:text>
    </xsl:template>

    <xsl:template match="NAME">
    <xsl:if test="position()&lt;3"><xsl:value-of select="self::NAME"/>,</xsl:if>
    </xsl:template>

    </xsl:stylesheet>
     
    Sandy, Dec 29, 2005
    #1
    1. Advertising

  2. Sandy wrote:

    > But since the elements are not present so the rule does not execute and i
    > don't get any comma in the output.csv


    Are you sure that XSL is the right tool for this ?

    > Please suggest what should be done.


    Try other tools, like XMLgawk.
    I bet you can solve your problem in 10 lines of source code.
     
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Dec 29, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Flood
    Replies:
    0
    Views:
    1,034
    Kevin Flood
    Sep 8, 2004
  2. Kevin Flood
    Replies:
    1
    Views:
    2,771
    Kevin Flood
    Sep 13, 2004
  3. Klaus Friese
    Replies:
    0
    Views:
    481
    Klaus Friese
    Nov 22, 2004
  4. Ray Tayek
    Replies:
    3
    Views:
    527
    Ray Tayek
    Nov 30, 2003
  5. Replies:
    1
    Views:
    3,637
    A. Bolmarcich
    May 27, 2005
Loading...

Share This Page