Xml XSL-总金额

Xml XSL-总金额,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我已经构建了下面的XSL(v2.0)来将CSV文件转换为XML文件。这个钻头很好用!我被卡住的部分是: 汇总所有“收据”元素中“金额”属性中的所有值 然后在“导入\u头”的“记录\u总计”属性中填充总和 运行XSLT后生成的XML如下所示,除了记录之外,没有计算总值(我无法工作的位) 以下是我目前基于包含3行的测试CSV文件的XSL: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Tra

我已经构建了下面的XSL(v2.0)来将CSV文件转换为XML文件。这个钻头很好用!我被卡住的部分是:

  • 汇总所有“收据”元素中“金额”属性中的所有值
  • 然后在“导入\u头”的“记录\u总计”属性中填充总和
  • 运行XSLT后生成的XML如下所示,除了记录之外,没有计算总值(我无法工作的位)

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

        <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新手变得危险了!:)

    您需要首先创建
    收据
    元素并将其存储在变量中,然后您可以对金额求和:

        <xsl:variable name="receipts" as="element(receipt)*">
         <!-- 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> 
        </xsl:variable>
    
        <IMPORT_HEADER record_count="{count($receipts)}" record_total="sum($receipts/@amount)}">
    
           <xsl:copy-of select="$receipts"/>  
    
        </IMPORT_HEADER>
    
    
    
    如果您确实想在一个表达式中执行,您可以执行
    ,但这样做效率不高,因为您会将每行标记两次,因此Martin的优秀答案确实是一种方法。您是一个明星!谢谢你,马丁!那真是太棒了!这让我咯咯地笑了,这个解决方案是多么简单,当你知道怎么做的时候,它是多么简单!安东尼
        <xsl:variable name="receipts" as="element(receipt)*">
         <!-- 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> 
        </xsl:variable>
    
        <IMPORT_HEADER record_count="{count($receipts)}" record_total="sum($receipts/@amount)}">
    
           <xsl:copy-of select="$receipts"/>  
    
        </IMPORT_HEADER>