An XSLT to turn XML into CSV

By Confusion on Friday 29 August 2008 21:23 - Comments (3)
Categories: Software engineering, XML, Views: 10.867

Today I needed to turn an XML file into a CSV file. I was sure someone would have solved this problem before, but I could not find an appropriate XSLT. The problem can be seperated into two subproblems: one is 'flattening' the XML, by which I mean turning it from

XML:
1
2
3
4
5
6
7
8
9
10
11
12
<root>
  <element>
    <foo>1</foo>
    <bar>
      <baz>2</baz>
      <fooz>3</fooz>
    </bar>
  </element>
  <element>
    <foo>1</foo>
  </element>
</root>


into

XML:
1
2
3
4
5
6
7
8
9
10
<root>
  <element>
    <foo>1</foo>
    <bar.baz>2</bar.baz>
    <bar.fooz>3</bar.fooz>
  </element>
  <element>
    <foo>1</foo>
  </element>
</root>


considering I am interested in converting each 'element' into a CSV line.
The 'namespaced' element names are required, because they serve as the CSV column headers and they are required to be unique (which, for our case, is guaranteed by this approach).
The other subproblem is converting XML to CSV, of which the main challenge was making sure the last element is not followed by a comma.

In the end, I came up with the templates below to print the values of the 'childless' elements.

XML:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<xsl:template match="//element">
        <xsl:apply-templates select="*" />
        <xsl:text>&#x0A;</xsl:text>
    </xsl:template>

    <xsl:template match="//element//*">
        <xsl:choose>
            <xsl:when test="count(child::*) > 0">
                <xsl:apply-templates select="*" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>"</xsl:text>
                <xsl:value-of select="."/>
                <xsl:text>"</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:if test="position() != last()">
            <xsl:text>,</xsl:text>
        </xsl:if>
    </xsl:template>


The upper template applies the lower template to the child nodes of element nodes called 'element', one 'element' at a time. The lower template determines whether the node has any child elements. If it doesn't, it prints the node. Otherwise, it recursively applies this template to the child nodes that were present. Finally, a comma is placed for each element that isn't the last in the node-set. What's a bit tricky here is that no comma is placed for the last grandchild of an 'element', so you might expect those to be missing, but that one is provided by the comma after the element itself.

What you could be

By Confusion on Thursday 21 August 2008 21:37 - Comments (13)
Category: Philosophy, Views: 5.319

If events conspired to make you the victim of cosmic bad luck, you could be notoriously absentminded. You could be distracted while bringing your daughter to the day-care center. You could be worried about someone and, hurrying over to meet him, get out of the car without a second glance. You could work hard all day, get back in the car, drive to the day-care center, be told there that you never dropped your daughter off, hurry back to your car and discover that you forgot so thoroughly that she is now dead, baked by the sun.

This could be you. You could be known as a good friend, a well-liked colleague, a devoted husband and a loving father and still this could be you. Remember that, next time you feel like flaming someone for their stupidity or their evilness. They may be just like you, only less lucky.

Inspired by Daniel Dennetts account of the same incident in his book Freedom Evolves

Theories without predictive power should still be funded

By Confusion on Wednesday 13 August 2008 21:29 - Comments (3)
Category: Science, Views: 6.676

In this /. article about "Solar Systems Like Ours Are Likely To Be Rare " someone responded with:
Whatever... this is naval gazing and conjecture, no more credible than Intelligent Design. These guys have a few data points, they create a highly convoluted system that seems to account for their data points, then the moment they get more data, they start over. Again and again.

A good critical thinker should know when to say "We don't have a fucking clue" if they want to be taken seriously. But then, it's all about money, isn't it?
and he's partially right. Some research targets of science, even in physics, are subject to too many independent variables. This results in theories that are very vulnerable to changes: if you change only a small thing, the predictions the theory make will vary wildly. Theories explaining the formation of solar systems are such research targets and it may well be said that "we don't have a fucking clue".

However, I don't agree with his implicit conclusion that such research targets should not receive any funding. We are still interested in knowing the answers these researchers are looking for. Even a bad theory is better than no theory: they need some starting point.

Of course, alternate theories should be encouraged. If someone wishes to explain the formation of solar systems by starting from Genesis: go ahead. However, it turns out that the current scientific theories, despite lacking predictive power, are considered to be more satisfactory to most than competing theories. Theories that are based on Genesis that explain the formation of solar systems, need for more adaptations to change their predictions from "solar systems are like ours" to "solar systems are unlike ours". So we fund th scientific ones, as they seem the most promising.

Ending with a funny /. comment:
I'm no expert, but as I recall the major problem with probes into the gas giants is that the immense pressure inside of them would crush anything we're capable of making, and electromagnetic interference from the constant storms would make it impossible to transmit any data out.

Plus, every time anyone mentions sending probes into Uranus over at NASA, nobody can stop giggling long enough to seriously work on the problem.
Though I don't think this is only funny: perhaps the connotations of 'Uranus' so often result in the culturally imprinted response of embarassment, that you cannot get a research program to start.

Hysteria over a Eucharist

By Confusion on Friday 1 August 2008 19:24 - Comments (6)
Category: Religion, Views: 3.384

PZ Meyers on the case of someone facing expulsion and death threats over taking a Eucharist from a Christian Mass.

And for the conclusion: http://scienceblogs.com/p...reat_desecration.php#more