Frage

I have an xslt file which transforms a csv file into xml. Here is my xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="invoice_date" select="tokenize(., ',')[3]"/>
    <xsl:template match="/">
        <transactions>
            <payee_transactions>
                <xsl:for-each select="tokenize(.,'\r?\n')">
                    <!-- DETERMINE THE LINE -->
                    <xsl:variable name="line" select="tokenize(., ',')"/>
                    <xsl:if test="$line[1]='$$$'">
                        <xsl:variable name="batch_date" select="$line[3]"/>
                        <xsl:variable name="batch_count" select="$line[4]"/>
                        <xsl:variable name="batch_amount" select="$line[5]"/>
                        <batch_date><xsl:sequence select="normalize-space($batch_date)"/></batch_date>
                        <batch_count><xsl:sequence select="normalize-space($batch_count)"/></batch_count>
                        <batch_amount><xsl:sequence select="normalize-space($batch_amount)"/></batch_amount>
                        <batch_description><xsl:sequence select="normalize-space($batch_reference)"/> - Payees</batch_description>
                    </xsl:if>
                    <xsl:if test="$line[1]='PAY'">
                        <payee_transaction>
                            <xsl:variable name="payee_name" select="$line[2]"/>
                            <xsl:variable name="addr1" select="$line[4]"/>
                            <payee_name><xsl:value-of select="normalize-space($payee_name)"/></payee_name>
                            <payee_id><xsl:value-of select="normalize-space($source_system_id)"/></payee_id>
                            <payee_address_line1><xsl:value-of select="normalize-space($addr1)"/></payee_address_line1>
                            <amount><xsl:value-of select="normalize-space($amount)"/></amount>
                            <line_memo><xsl:value-of select="normalize-space($line_memo)"/></line_memo>
                            <invoice_date><xsl:value-of select="$batch_date"/></invoice_date>
                        </payee_transaction>
                    </xsl:if>
                </xsl:for-each>             
            </payee_transactions>
        </transactions>
    </xsl:template>
</xsl:stylesheet>

My main problem is that I get a compiler error when I try to use the batch_date variable in the second if statement.

I want each record to have an invoice_date which is the same as the batch_date.

I looked into global variables, but I come across the same message from people, a global variable can not be re-assigned in a template, a new variable gets created, with that same name. So, I would like to create a global variable and assign it a value before I hit the template, so I always have it.

I need it to grab the third element from the first line of the csv file. What I wrote does not work.

How can I do this?

War es hilfreich?

Lösung 2

Right now the scope for the invoice_date variable isn't set because it is outside of any template, which is the fine place for "global" variables.

<xsl:variable name="invoice_date" select="tokenize(., ',')[3]"/>

Based on that the . character doesn't yet have meaning. If it is changed to / it should now have scope of the document being processed.

<xsl:variable name="invoice_date" select="tokenize(/, ',')[3]"/>

Andere Tipps

Using this sample csv file (Book1.csv)

<csv>
a,b,c
d,e,f
g,h,i
</csv>

and this stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output omit-xml-declaration="yes"/>

   <xsl:variable name="batch_date">
       <xsl:for-each select="tokenize(document('Book1.csv'), '\r?\n')[2]">
           <xsl:for-each select="tokenize(.,',')[3]">
               <xsl:value-of select="."></xsl:value-of>
           </xsl:for-each>
       </xsl:for-each>
   </xsl:variable>

    <xsl:template match="/">
        <variable>
            <xsl:value-of select="$batch_date"/>
        </variable>
    </xsl:template>
</xsl:stylesheet>

the output is

<variable>c</variable>

see if you can adapt this to your current problem.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top