XSL-总金额

时间:2018-07-04 08:11:11

标签: xml xslt xslt-2.0

我已经在下面构建了XSL(v2.0),可以将CSV文件转换为XML文件。这一点很棒!我卡住的部分是:

  1. 总结所有“接收”元素中“金额”属性中的所有值
  2. 然后在“ IMPORT_HEADER”的“ record_total”属性中填充总和

运行XSLT之后生成的XML如下所示,除了record_total之外(我无法工作的那一点都没有计算)...

<IMPORT_HEADER record_count="3" record_total="175.00" >
<receipt account_code="12345678" amount="25.00" />
<receipt account_code="23456789" amount="50.00" />
<receipt account_code="34567891" amount="100.00" />
</IMPORT_HEADER>

以下是我到目前为止的XSL,基于我的包含3行的测试CSV文件:

    <xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xsl:output method="xml" indent="yes"/>

<!-- Parameter used to specify the file location and name of the CSV file -->
<xsl:param name="pathToCSV" select="'file:///c:/csv.csv'" />

<xsl:template match="/">

    <xsl:choose>
    <xsl:when test="unparsed-text-available($pathToCSV)">

        <!-- Read the CSV file and return its contents as a string -->
        <xsl:variable name="csv" select="unparsed-text($pathToCSV)" />

        <!-- Split the csv string into individual rows -->
        <xsl:variable name="rows" select="tokenize($csv, '\r?\n')" /> 

        <!-- Create the root element node and namespace declarations -->        
        <IMPORT_HEADER>

        <!-- Creates the attributes within the root element node -->
        <xsl:attribute name="record_count" select="count($rows)-1"/>
        <xsl:attribute name="record_total" select="'636.13'"/>  

        <!-- Process each row in the CSV file, skip row 1 which contains the column headers -->
        <xsl:for-each select="$rows[position() !=1]">               

            <!-- Split each row into a comma separated list of columns -->
            <xsl:variable name="cols" select="tokenize(., ',')" />

            <!-- Create the child receipt node and populate the attributes -->                          
            <receipt>
                <xsl:attribute name="account_code" select="'12345678'" />
                <xsl:attribute name="amount" select="$cols[12]"/>
            </receipt>      

        </xsl:for-each> 

        </IMPORT_HEADER>

    </xsl:when>
    <xsl:otherwise>
        <xsl:text>Cannot locate : </xsl:text><xsl:value-of select="$pathToCSV" />
    </xsl:otherwise>
    </xsl:choose>   

</xsl:template>

任何帮助将不胜感激!

签署XSL新手越来越危险! :)

1 个答案:

答案 0 :(得分:2)

您需要首先创建public function addTag($tags, $book_id) { // Loop tags to insert foreach ($tags as $tag) { // insert tags if not existed $this->db->query('INSERT INTO tags (tag) VALUES (:tag) ON DUPLICATE KEY UPDATE tag = :tag'); // Bind values $this->db->bind(':tag', $tag); // Execute query $this->db->execute(); // Get lsat id $last_tag_id = $this->db->lastId(); $this->db->query('INSERT INTO books_tag (book_id, tag_id) VALUES (:book_id, :tag_id)'); // Add tag to books $this->db->bind(':book_id', $book_id); $this->db->bind('tag_id', $last_tag_id); // Execute query $this->db->execute(); } } 元素并将其存储在变量中,然后可以对金额求和:

receipt