XSLT转换(XML到CSV)

时间:2016-01-11 07:36:47

标签: xml csv xslt

我是xslt编程的新手,希望您能在下面提供帮助。

我必须从XML生成一个CSV报告,并显示以下外观。 (我不确定是否可以运行总计)

预期输出:

Sum Insured Report

UK
Sl.No.  Policy #        Customer #      Cover Note #    No. of Addendas     Sum Insured     GP(SumCustDeb - SumCustCred)        NP(SumInsCred - SumInsDeb)      Total Commission
1       414227          3321            50242           2                   21000           1700                                1700                            850
2                       ABC             12345           0                    2000            500                                 500                             10
3       1A2B            XYZ             98765           1                   60000            100                                 100                             60
                                                                    Total:  83000           2300                                2300                            920

XML:

<?xml version="1.0" encoding="UTF-8"?><?Siebel-Property-Set EscapeNames="true"?><Message MessageId="1-OC05" IntObjectName="Report IO" MessageType="Integration Object" IntObjectFormat="Hierarchical">
    <ReportIo>
        <Policies>
         <Addenda>50242-1</Addenda>
         <CommAmt>700</CommAmt>
         <Cover>50242</Cover>
         <Customer>3321</Customer>
         <Policy>414227</Policy>
         <SumCustCred>500</SumCustCred>
         <SumCustDeb>1000</SumCustDeb>
         <SumInsCred>1000</SumInsCred>
         <SumInsDeb>500</SumInsDeb>
         <SumInsured>10000</SumInsured>
         <Organization>UK</Organization>
        </Policies>
        <Policies>
         <Addenda>50242-2</Addenda>
         <CommAmt>50</CommAmt>
         <Cover>50242</Cover>
         <Customer>3321</Customer>
         <Policy>414227</Policy>
         <SumCustCred>0</SumCustCred>
         <SumCustDeb>500</SumCustDeb>
         <SumInsCred>500</SumInsCred>
         <SumInsDeb>0</SumInsDeb>
         <SumInsured>1000</SumInsured>
         <Organization>UK</Organization>
        </Policies>
        <Policies>
         <Addenda></Addenda>
         <CommAmt>100</CommAmt>
         <Cover>50242</Cover>
         <Customer>3321</Customer>
         <Policy>414227</Policy>
         <SumCustCred>0</SumCustCred>
         <SumCustDeb>700</SumCustDeb>
         <SumInsCred>700</SumInsCred>
         <SumInsDeb>0</SumInsDeb>
         <SumInsured>10000</SumInsured>
         <Organization>UK</Organization>
        </Policies>
        <Policies>
         <Addenda></Addenda>
         <CommAmt>10</CommAmt>
         <Cover>12345</Cover>
         <Customer>ABC</Customer>
         <Policy></Policy>
         <SumCustCred>0</SumCustCred>
         <SumCustDeb>500</SumCustDeb>
         <SumInsCred>500</SumInsCred>
         <SumInsDeb>0</SumInsDeb>
         <SumInsured>2000</SumInsured>
         <Organization>UK</Organization>
        </Policies>
        <Policies>
         <Addenda></Addenda>
         <CommAmt>50</CommAmt>
         <Cover>98765</Cover>
         <Customer>XYZ</Customer>
         <Policy>1A2B</Policy>
         <SumCustCred>0</SumCustCred>
         <SumCustDeb>0</SumCustDeb>
         <SumInsCred>0</SumInsCred>
         <SumInsDeb>0</SumInsDeb>
         <SumInsured>10000</SumInsured>
         <Organization>UK</Organization>
        </Policies>
        <Policies>
         <Addenda>98765-1</Addenda>
         <CommAmt>10</CommAmt>
         <Cover>98765</Cover>
         <Customer>XYZ</Customer>
         <Policy>1A2B</Policy>
         <SumCustCred>0</SumCustCred>
         <SumCustDeb>100</SumCustDeb>
         <SumInsCred>100</SumInsCred>
         <SumInsDeb>0</SumInsDeb>
         <SumInsured>50000</SumInsured>
         <Organization>UK</Organization>
        </Policies>
    </ReportIo>
</SiebelMessage>

XSLT已使用,但在尝试将组织显示为&#39; UK&#39;时,我陷入了困境。系统说&#34; XSLT处理例外:XSLT错误:xsl:value-of不允许在样式表中的这个位置&#34;

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://www.canto.com/ns/Export/1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="xs">
<xsl:output method="text" indent="yes" encoding="utf-8" standalone="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/ReportIo">
<xsl:text>Sum Insured Report</xsl:text>
<xsl:text>&#10;&#10;</xsl:text>
<xsl:text>SNo.</xsl:text>
<xsl:text>&#09;</xsl:text>
<xsl:text>Policy #</xsl:text>
<xsl:text>&#09;</xsl:text>
<xsl:text>Customer #</xsl:text>
<xsl:text>&#09;</xsl:text>
<xsl:text>Cover Note #</xsl:text>
<xsl:text>&#09;</xsl:text>
<xsl:text>No. of Addendas</xsl:text>
<xsl:text>&#09;</xsl:text>
<xsl:text>Sum Insured</xsl:text>
<xsl:text>&#09;</xsl:text>
<xsl:text>GP</xsl:text>
<xsl:text>&#09;</xsl:text>
<xsl:text>NP</xsl:text>
<xsl:text>&#09;</xsl:text>
<xsl:text>Total Commission</xsl:text>
<xsl:text>&#10;</xsl:text>
<xsl:for-each-group select="Policies" group-by="Organization"> <!--Group by Organization-->
<xsl:value-of select="Organization"/> <!--Display the Organization-->
<xsl:text>&#10;</xsl:text>
<xsl:text>&#10;</xsl:text>
<xsl:for-each-group select="current-group()" group-by="Cover">
<xsl:for-each-group select="position()"/> <!--Trying to generate Sequence Number-->
<xsl:text>&#09;</xsl:text>
<xsl:value-of select="Policy"/>
<xsl:text>&#09;</xsl:text>
<xsl:value-of select="Customer"/>
<xsl:text>&#09;</xsl:text>
<xsl:value-of select="Cover"/>
<xsl:text>&#09;</xsl:text>
<xsl:value-of select="count(distinct-values(current-group()/Addendas))"/> <!-- Getting Distinct Count of Addendas while it is not blank-->
<xsl:text>&#09;</xsl:text>
<xsl:value-of select="sum(current-group()/SumInsured)"/> <!-- Summation of SumInsured element within the 'Cover' group-->
<!-- Not sure how to get GP & NP, if it is at all possible>
<xsl:text>&#09;</xsl:text>
<xsl:value-of select="GP"/>
<xsl:text>&#09;</xsl:text>
<xsl:value-of select="NP"/>
<End of GP & NP-->
<xsl:text>&#09;</xsl:text>
<xsl:value-of select="sum(current-group()/CommAmt)"/>
<xsl:text>&#10;</xsl:text>
</xsl:for-each-group>
</xsl:for-each-group>   
</xsl:template>
</xsl:stylesheet>

以下标记也提供了错误的值,因为正在考虑空白附录:

<xsl:value-of select="count(distinct-values(current-group()/Addendas))"/> 

我无法得到这笔钱:

<xsl:value-of select="sum(current-group()/SumInsured)"/>

请原谅请求的大小。任何帮助都会很乐意。

1 个答案:

答案 0 :(得分:0)

您需要从样式表中删除此行(第31行):

<xsl:for-each-group select="position()"/> <!--Trying to generate Sequence Number-->

另请注意,您的<xsl:template match="/ReportIo">会认为ReportIo是根元素 - 但您向我们展示的输入包含<Message>开头标记和</SiebelMessage>结束标记,它格式不正确的XML。