XML到CSV代码格式

时间:2015-09-12 11:04:07

标签: xml csv

我不是熟练的xml开发人员,我已经成功构建了我的xsl代码。我正在遍历节点并正确地从子节点获取数据但没有获得所需的结果。这是我的xml,也可以在http://xsltransform.net/gWmuiKs/4找到:

 <?xml version="1.0" encoding="UTF-8"?>
 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
       <WorkDayInfo>
           <Activities>test1</Activities>
           <Date>12-01-12</Date>
           <Entries>
              <HourEntry>
                 <CaseGUID>3fee9c7</CaseGUID>
                 <GUID>kfsdsdlf</GUID>
                 <InvoiceGUID>a2d169</InvoiceGUID>
                 <UserGUID>4b4328</UserGUID>
                 <WorkTypeGUID>31566</WorkTypeGUID>
              </HourEntry>  
              <HourEntry>
                 <CaseGUID>1111</CaseGUID>
                 <GUID>2222</GUID>
                 <InvoiceGUID>3333</InvoiceGUID>
                 <UserGUID>4444</UserGUID>
                 <WorkTypeGUID>5555</WorkTypeGUID>
              </HourEntry> 
           </Entries>
           <ExpectedHours>9</ExpectedHours>
           <TotalHours>10</TotalHours>
    </WorkDayInfo>
    <WorkDayInfo>
        <Activities>test2</Activities>
        <Date>13-01-09</Date>
           <Entries>
             <HourEntry>
                 <CaseGUID>3fee9c79c</CaseGUID>
                 <GUID>kfsdsdl</GUID>
                 <InvoiceGUID>a2d169c</InvoiceGUID>
                 <UserGUID>4b43281</UserGUID>
                 <WorkTypeGUID>31566f</WorkTypeGUID>
             </HourEntry>
                <HourEntry>
                 <CaseGUID>3fee9c7s</CaseGUID>
                 <GUID>kfsdsd4</GUID>
                 <InvoiceGUID>a2d16s</InvoiceGUID>
                 <UserGUID>4b43281ab</UserGUID>
                 <WorkTypeGUID>31566s6</WorkTypeGUID>
             </HourEntry>
           </Entries>
        <ExpectedHours>3</ExpectedHours>
        <TotalHours>5</TotalHours>   
    </WorkDayInfo>
    <WorkDayInfo>
        <Activities>test3</Activities>
        <Date>13-01-09</Date>
           <Entries>         
           </Entries>
        <ExpectedHours>1</ExpectedHours>
        <TotalHours>4</TotalHours>
    </WorkDayInfo>      
    <WorkDayInfo>
        <Activities>test4</Activities>
        <Date>13-01-09</Date>
           <Entries>
             <HourEntry>
                 <CaseGUID>3fee9c790</CaseGUID>
                 <GUID>kfsdsdlfk</GUID>
                 <InvoiceGUID>a2d16</InvoiceGUID>
                 <UserGUID>4b43281a</UserGUID>
                 <WorkTypeGUID>31566f</WorkTypeGUID>
             </HourEntry>
              <HourEntry>
                 <CaseGUID>aaaaa</CaseGUID>
                 <GUID>bbbbbb</GUID>
                 <InvoiceGUID>ccccc</InvoiceGUID>
                 <UserGUID>ddddd</UserGUID>
                 <WorkTypeGUID>eeeee</WorkTypeGUID>
             </HourEntry>
           </Entries>
        <ExpectedHours>1</ExpectedHours>
        <TotalHours>4</TotalHours>
    </WorkDayInfo>
  </s:Body>
</s:Envelope>

这是我的xsl文件:

<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:variable name="delimiter" select="','"/>
<xsl:template match="/">
    <!-- Output the CSV header -->
    <xsl:text>Activ,Date,ExpectedHours,TotalHrs,CaseGUID,GUID,InvoiceGUID,UserGUID,WorkTypeGUID&#10;</xsl:text>
    <!-- Output the values -->
    <xsl:for-each select="//WorkDayInfo">
        <!-- begin values -->
        <xsl:value-of select="concat(Activities, $delimiter, Date, $delimiter,ExpectedHours,$delimiter,TotalHours)"/>     

        <!-- Category Values -->
        <xsl:value-of select="$delimiter"/>
        <xsl:for-each select="Entries/HourEntry">
        <xsl:choose>           
        <xsl:when test="position()=0">        
            <xsl:value-of select="CaseGUID"/>
            <xsl:value-of select="$delimiter"/>
            <xsl:value-of select="GUID"/>
            <xsl:value-of select="$delimiter"/>
            <xsl:value-of select="InvoiceGUID"/>
            <xsl:value-of select="$delimiter"/>
            <xsl:value-of select="UserGUID"/>
            <xsl:value-of select="$delimiter"/>
            <xsl:value-of select="WorkTypeGUID"/>              
        </xsl:when>
            <xsl:otherwise>
                <xsl:text>&#9;&#9;&#9;&#9;</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:value-of select="concat(CaseGUID,'&#9;,',GUID,'&#9;,',InvoiceGUID,'&#9;,',UserGUID,'&#9;,',WorkTypeGUID,'&#10;')"/>           
        <!--<xsl:value-of select="concat(title, '&#9;', para, '&#10;')"/>-->
            <xsl:if test="position()!=last()">
                <xsl:text></xsl:text>
            </xsl:if>
        </xsl:for-each>          
        <xsl:if test="position()!=last()">
            <xsl:text>&#9;</xsl:text>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

这是处理后的输出(不需要的):

     Activi,Date,ExpectedHours,TotalHrs,CaseGUID,GUID,InvoiceGUID,UserGUID,WorkTypeGUID
test1,12-01-12,9,10,    3fee9c7 ,kfsdsdlf   ,a2d169 ,4b4328 ,31566
            1111    ,2222   ,3333   ,4444   ,5555
test2,13-01-09,3,5, 3fee9c79c,kfsdsdl   ,a2d169c  ,4b43281  ,31566f
            3fee9c7s    ,kfsdsd4    ,a2d16s ,4b43281ab  ,31566s6
 test3,13-01-09,1,4,    
 test4,13-01-09,1,4, 3fee9c790  ,kfsdsdlfk  ,a2d16  ,4b43281a   ,31566f
            aaaaa   ,bbbbbb ,ccccc  ,ddddd  ,eeeee

但是正如您在上面的输出中看到的那样,第二行子元素应该具有相同的行数据(前4列)。在下面显示的示例中,这是我期望的输出(期望)。我希望有人能在我出错的地方投入一些亮点吗?

Activ,Date,ExpectedHours,TotalHours,CaseGUID,GUID,InvoiceGUID,UserGUID,WorkTypeGUID                               
test1,12-01-12,9,10,3fee9c7,kfsdsdlf,a2d169,4b4328,31566
test1,12-01-12,9,10,3fee9c7,aaaaa,bbbbbb,ccccc,ddddd,eeeee
....
test4,13-01-09,1,4,3fee9c790,kfsdsdlfk,a2d16,4b43281a,31566f
test4,13-01-09,1,4,aaaaa,bbbbbb,ccccc,ddddd,eeeee

2 个答案:

答案 0 :(得分:0)

如果你的成功是正常的,只需添加一个步骤来删除空格:

string undesired = @" Activi,Date,ExpectedHours,TotalHrs,CaseGUID,GUID,InvoiceGUID,UserGUID,WorkTypeGUID
test1,12-01-12,9,10,    3fee9c7 ,kfsdsdlf   ,a2d169 ,4b4328 ,31566
        1111    ,2222   ,3333   ,4444   ,5555
test2,13-01-09,3,5, 3fee9c79c,kfsdsdl   ,a2d169c  ,4b43281  ,31566f
        3fee9c7s    ,kfsdsd4    ,a2d16s ,4b43281ab  ,31566s6
test3,13-01-09,1,4,    
test4,13-01-09,1,4, 3fee9c790  ,kfsdsdlfk  ,a2d16  ,4b43281a   ,31566f
        aaaaa   ,bbbbbb ,ccccc  ,ddddd  ,eeeee";


string trimmed = undesired.Replace(" ", string.Empty);
Console.WriteLine(trimmed);

您将获得此输出:

Activi,Date,ExpectedHours,TotalHrs,CaseGUID,GUID,InvoiceGUID,UserGUID,WorkTypeGUID
test1,12-01-12,9,10,3fee9c7,kfsdsdlf,a2d169,4b4328,31566
1111,2222,3333,4444,5555
test2,13-01-09,3,5,3fee9c79c,kfsdsdl,a2d169c,4b43281,31566f
3fee9c7s,kfsdsd4,a2d16s,4b43281ab,31566s6
test3,13-01-09,1,4,
test4,13-01-09,1,4,3fee9c790,kfsdsdlfk,a2d16,4b43281a,31566f
aaaaa,bbbbbb,ccccc,ddddd,eeeee

答案 1 :(得分:0)

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>

    <xsl:template match="/">
        <!-- Output the CSV header -->
        <xsl:text>Activities,Date,CaseGUID,GUID</xsl:text>
        <xsl:text>&#xA;</xsl:text>

        <!-- Output the values -->
        <xsl:for-each select="//WorkDayInfo">
            <xsl:variable name="head">
                <xsl:value-of select="concat(Activities,',',Date)"/>
            </xsl:variable>

            <xsl:for-each select="./Entries">
                <xsl:if test="count(*) = 0">
                    <xsl:value-of select="$head"/>
                    <xsl:text>&#xA;</xsl:text>
                </xsl:if>

                <xsl:for-each select="./HourEntry">
                    <xsl:value-of select="$head"/>
                    <xsl:text>,</xsl:text>
                    <xsl:value-of select="concat(CaseGUID,',',GUID)"/>
                    <xsl:text>&#xA;</xsl:text>
                </xsl:for-each>
            </xsl:for-each>

        </xsl:for-each>

    </xsl:template>
</xsl:stylesheet>