【发布时间】:2014-03-18 20:04:13
【问题描述】:
我有一个将 csv 文件转换为 xml 的 xslt 文件。 这是我的 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>
我的主要问题是当我尝试在第二个 if 语句中使用 batch_date 变量时出现编译器错误。
我希望每条记录都有一个invoice_date,它与batch_date 相同。
我查看了全局变量,但我从人们那里得到相同的消息,无法在模板中重新分配全局变量,创建了一个具有相同名称的新变量。 所以,我想在点击模板之前创建一个全局变量并为其赋值,所以我总是有它。
我需要它从 csv 文件的第一行中获取第三个元素。 我写的不行。
我该怎么做?
【问题讨论】:
-
什么是编译器错误?
-
在您的变量中,您认为此时分配给该期间的范围是什么?你试过用
/代替吗? -
@LegoStormtroopr - 我得到的错误是:
Variable batch_date has not been declared -
@JasonAller - 我在想全局发票数据变量声明中的句点会引用输入文档。即使它引用了整个文档,第三个元素仍然是发票日期。我现在正在使用斜线
/进行测试。 -
@JasonAller - 使用斜线有效。如果你把你的评论变成答案,我会选择它。谢谢
标签: xml variables xslt csv xslt-2.0