XSL Query

S

Sandy

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

=?ISO-8859-1?Q?J=FCrgen_Kahrs?=

Sandy said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top