1

我有一个场景将文本记录推送到数据库以跟踪个人的一些信息。

我的文本文件包含如下所示的数据:

logonID|agentName|modify|exception|start|stop|externalID        
14051286759|Jacks, Monica|1373477063|Break|01:45|02:00|USWMAJ43
14051286759|Jacks, Monica|1373477063|Break|06:10|06:25|USWMAJ43
14051286759|Jacks, Monica|1373477063|Lunch|03:45|04:30|USWMAJ43
14051286759|Jacks, Monica|1373477063|Open|00:00|01:45|USWMAJ43
14051286759|Jacks, Monica|1373477063|Open|02:00|03:45|USWMAJ43
14051286759|Jacks, Monica|1373477063|Open|04:30|06:10|USWMAJ43
14051286759|Jacks, Monica|1373477063|Open|06:25|08:30|USWMAJ43

现在我需要为 Break、Lunch 和 open 创建一个 XML(一条记录的打开数可能在 5 到 6 个条目之间变化),如下所示:

<info>
<break>
<break_1>01:45-02:00</break_1>
<break_2>06:10-06:25</break_1>
</break>
<lunch>03:45-04:30</lunch>
<open>
<open_1>00:00-01:45</open_1>
<open_2>02:00-03:45</open_2>
<open_3>04:30-06:10</open_3>
<open_4>06:25-08:30</open_4>
</open>
</info>

提前致谢

4

1 回答 1

0

XSLT

要使用 xslt,请将文件位置传递给 pText 参数,例如file:///C:/data.txt

<xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:param name="pText" />
    <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'"/>
    <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
    <xsl:variable name="vText" select="unparsed-text($pText)"/>
    <xsl:variable name="vLines" select="tokenize($vText, '\r?\n')[position() > 1][normalize-space()]"/>
    <xsl:template match="/">
        <info>
            <xsl:for-each-group select="$vLines" group-by="tokenize(.,'\|')[4]">
                <xsl:variable name="vKey" select="translate(current-grouping-key(), $uppercase, $smallcase)"/>
                <xsl:element name="{$vKey}">
                    <xsl:for-each select="current-group()">
                        <xsl:variable name="vValues" select="tokenize(.,'\|')"/>
                        <xsl:variable name="vPos" select="position()"/>
                        <xsl:element name="{$vKey}_{$vPos}">
                            <xsl:value-of select="concat($vValues[5],'-',$vValues[6])"/>
                        </xsl:element>
                    </xsl:for-each>
                </xsl:element>
            </xsl:for-each-group>
        </info>
    </xsl:template>
</xsl:stylesheet>

输入:

logonID|agentName|modify|exception|start|stop|externalID        
14051286759|Jacks, Monica|1373477063|Break|01:45|02:00|USWMAJ43
14051286759|Jacks, Monica|1373477063|Break|06:10|06:25|USWMAJ43
14051286759|Jacks, Monica|1373477063|Lunch|03:45|04:30|USWMAJ43
14051286759|Jacks, Monica|1373477063|Open|00:00|01:45|USWMAJ43
14051286759|Jacks, Monica|1373477063|Open|02:00|03:45|USWMAJ43
14051286759|Jacks, Monica|1373477063|Open|04:30|06:10|USWMAJ43
14051286759|Jacks, Monica|1373477063|Open|06:25|08:30|USWMAJ43

输出:

<info>
    <break>
        <break_1>01:45-02:00</break_1>
        <break_2>06:10-06:25</break_2>
    </break>
    <lunch>
        <lunch_1>03:45-04:30</lunch_1>
    </lunch>
    <open>
        <open_1>00:00-01:45</open_1>
        <open_2>02:00-03:45</open_2>
        <open_3>04:30-06:10</open_3>
        <open_4>06:25-08:30</open_4>
    </open>
</info>

C#

参考 1参考 2

XsltArgumentList argsList = new XsltArgumentList();
argsList.AddParam("pText", "", "file:///C:/data.txt");
XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;
XslCompiledTransform myXslTrans = new XslCompiledTransform();
myXslTrans.Load(myStyleSheet);
XmlTextWriter myWriter = new XmlTextWriter("result.html",null);
myXslTrans.Transform(myXPathDoc,argsList,myWriter);
于 2013-08-05T15:17:54.340 回答