user2917823
user2917823

Reputation: 199

XML to CSV using XSL template

I got this simple XML document:

<?xml version="1.0"?>
<document>
<category>
    <id>20504       </id>
    <title>ADSL iranga</title>
    <parent>Kompiuterinio tinklo</parent>
</category>
<category>
    <id>20902</id>
    <title>Akumuliatoriai</title>
    <parent>Baterijos akumuliatoriai</parent>
</category>
</document>

And this xsl template:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8"/>
<xsl:strip-space elements="*" />

<xsl:template match="/category">
  <xsl:value-of select="id" /><xsl:text>,</xsl:text>
  <xsl:value-of select="title" /><xsl:text>,</xsl:text>
</xsl:template>


</xsl:stylesheet>

I would like to get this simple output:

id,title
id,title
id,title
<...>

However, I don't get my expected delimiter "," Am I using the xsl:text in some wrong manner?

I am using xsltproc for the conversion.

Upvotes: 1

Views: 64

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

category is not the root element of your input XML - so your template matches nothing, and the output you see is produced by the built-in template rules.

You need to change:

<xsl:template match="/category">

to:

<xsl:template match="category">

I think you also want to change the second:

<xsl:text>,</xsl:text>

to:

<xsl:text>&#10;</xsl:text>

Upvotes: 2

Related Questions