xsltxslt-2.0xsl-fo

How do i pass a dynamic filename to unparsed-text function in xslt?


Im working with Apache FOP to generate AFP and PDFs. In my project im required to read a text file. Im using unparsed-text to achieve this.

<xsl:variable name="PDFNAME" select="InvoiceIndicators/PdfFileName"/>
    <xsl:variable name="vText" select="tokenize(unparsed-text('../data/ebpp/fopinvoices/arn/emfe_afp/sample.txt'),'&#xD;&#xA;')"/>
    <xsl:message>value of unparsed-text is <xsl:value-of select="$vText"/> </xsl:message>
    <xsl:variable name="PDFFileStatus">
        <xsl:for-each select="$vText">
            <xsl:if test="contains(.,$PDFNAME)">
                <xsl:value-of select="substring-after(.,',')"/>
            </xsl:if>
        </xsl:for-each>
    </xsl:variable>
    <xsl:message><xsl:text>PDFFileStatus :</xsl:text> <xsl:value-of 
select="$PDFFileStatus"/> </xsl:message>
    

This code works perfect if the text file that im reading has a fixed name. In this case it is sample.txt. How do i make sure to read from a text file which has a dynamic name ? For example i have a parameter like this :

<xsl:param name="FileName"/>

How can i use this $FileName as the name of the txt file?

<xsl:variable name="vText" select="tokenize(unparsed-text('../data/ebpp/fopinvoices/arn/emfe_afp/{$FileName}.txt'),'&#xD;&#xA;')"/>

The above line doesnt seem to work. Please help.


Solution

  • In any version of XSLT there is a string concat function so that you can do e.g. concat('../data/ebpp/fopinvoices/arn/emfe_afp/', $FileName, '.txt'). Any XSLT 3 processor will also support the || string concatenation operator for e.g. '../data/ebpp/fopinvoices/arn/emfe_afp/' || $FileName || '.txt'.