How do i wrap the unparsed-text function in a condition so that it is not called all the time?

40 Views Asked by At

Following up on question How do i pass a dynamic filename to unparsed-text function in xslt?

I have the below snippet of code:

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

I am making use of the variable PDFFileStatus in other parts of the codes and and a check to it.

For example in some part of the code i have ,

<xsl:when test="DunningSummary and $PDFFileStatus='Found'">
                <xsl:value-of select="generate-id(DunningSummary)"/>
</xsl:when>

The code works perfectly so far. But i have some cases where i dont want to enter the code where i have written the unparsed-text function.

Like wrapping it with an if condition :

<xsl:if test="InvoiceIndicators/LetterTypeCode = 'PU'">
    <xsl:variable name="imagename" select="substring(substring-after(InvoiceDetails/InvoiceNumber, '_'), 3, 9)"/>
    <xsl:variable name="vText" select="tokenize(unparsed-text(concat('../data/ebpp/fopinvoices/arn/emfe_afp/', $FileName, '.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(.,$imagename)">
                <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>
    </xsl:if>

But now i have the issue with the code snippet where i added a check to the variable PDFFileStatus.

How do i handle such case?

1

There are 1 best solutions below

0
Martin Honnen On

Based on the comments you want to use the function unparsed-text-available https://www.w3.org/TR/xpath-functions-31/#func-unparsed-text-available and then only use unparsed-text if the unparsed-text-available check returns true.