1

I am working with Apache FOP. I am using XSLT to convert XML to PDF (which works fine). I need to create a journal file (basically a .txt file). The journal file should consist of values from tag values from XML while processing it in XSLT. i require this journal file for some reconciliation. Example:

<Details>
 <Name>Pooja</Name>
 <Country>India</Country>
 <OtherInfo>ABC</OtherInfo>
</Details>

I would need the .txt contents as below:

Pooja,India,ABC

How do i achieve this?

Pooja
  • 45
  • 5
  • 1
    If you really use XSLT 2.0 then you have `xsl:result-document` for e.g. ``. Use modes if other parts of your XSLT (i.e. the ones creating XSL-FO) process the `Details` element as well. – Martin Honnen Aug 01 '23 at 17:47
  • 1
    Really depends upon which XSLT processor and which version that you are using (or can use). – al.truisme Aug 01 '23 at 18:43
  • i figured i am using XSLT 1.0 :( How do i upgrade to 2.0 ? – Pooja Aug 03 '23 at 07:41
  • @Pooja If you're using an XSLT 1.0 processor, then why is your question tagged `xslt-2.0`?? As to how can you upgrade, it depends on your environment, of which you have told us nothing. Perhaps this can help: https://stackoverflow.com/questions/19379070/how-to-change-apache-fop-xalan-xslt-processor – michael.hor257k Aug 03 '23 at 13:37
  • @michael.hor257k I upgraded to 2.0 using the steps provided in the link and it worked wonders :) – Pooja Aug 04 '23 at 10:58
  • That's good. Note that if you picked a recent version of Saxon then you have support for XSLT 3.0. – michael.hor257k Aug 04 '23 at 11:12
  • Well i used jar version 8.7. I guess i can download the latest too. Thanks a ton! – Pooja Aug 04 '23 at 11:23

1 Answers1

1

There are several ways that you could generate the CSV output. If there is a chance those values could contain , then you might want to add some logic to escape or wrap with quotes. But for the simple case of joining those element values with , you could start with either of these:

XSLT 2.0 (or greater) using xsl:value-of with @separator=",":

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="text"/>
    <xsl:template match="Details">
        <xsl:value-of select="*" separator=","/>
    </xsl:template>
</xsl:stylesheet>

XSLT 2.0 (or greater) using fn:string-join():

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="text"/>
    <xsl:template match="Details">
        <xsl:value-of select="string-join(*,',')"/>
    </xsl:template>
</xsl:stylesheet>

XSLT 1.0 using xsl:for-each and testing the position() to determine whether to emit the ,:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>
    <xsl:template match="Details">
        <xsl:for-each select="*">
            <xsl:if test="position() > 1">,</xsl:if>
            <xsl:value-of select="."/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • I am using XSLT 1.0. Where exactly is the file getting created? I understand the for each and the output method. But where exactly is the filename and file location ? – Pooja Aug 03 '23 at 07:41
  • 1
    @Pooja It is not possible in XSLT 1.0 to generate both an XSL-FO and a .txt file in a single transformation. You will need to run the transformation twice, either using 2 different stylesheets, or a single stylesheet with a different parameter at a time. – michael.hor257k Aug 03 '23 at 13:31