XSLT on XML to CSV,仅在某些节点上使用列标题

时间:2017-02-11 17:05:36

标签: xml csv xslt

我有一个XML文件,其中包含有关足球比赛及其得分发生的信息。我想只选择我感兴趣的某些节点并将其保存为CSV。

我真的只对这些节点感兴趣:

  • / GAM:游戏/ GAM:日期
  • / GAM:游戏/ GAM:awayTeam / GAM:缩写
  • / GAM:游戏/ GAM:homeTeam / GAM:缩写
  • / GAM:quarterSummary / GAM:季度/ GAM:得分/ GAM:scoringPlay / GAM:teamAbbreviation
  • / GAM:quarterSummary / GAM:季度/ GAM:得分/ GAM:scoringPlay / GAM:playDescription

我的期望在CSV(逗号分隔)

中会是这样的
GameDate, AwayTeam, HomeTeam, ScoringTeam, PlayDescription

2017-01-07, DET, SEA, SEA, (7:14) R.Wilson pass short right to P.Richardson for 2 yards, TOUCHDOWN. Penalty on DET-T.Wilson, Defensive Pass Interference, declined.

2017-01-07, DET, SEA, SEA, S.Hauschka extra point is GOOD, Center-T.Ott, Holder-J.Ryan.

2017-01-07, DET, SEA, SEA, (2:00) S.Hauschka 43 yard field goal is GOOD, Center-T.Ott, Holder-J.Ryan.

2017-01-07, DET, SEA, DET, (:25) M.Prater 51 yard field goal is GOOD, Center-D.Muhlbach, Holder-S.Martin.

2017-01-07, DET, SEA, DET, (4:08) M.Prater 53 yard field goal is GOOD, Center-D.Muhlbach, Holder-S.Martin.

2017-01-07, DET, SEA, SEA, (14:15) S.Hauschka 27 yard field goal is GOOD, Center-T.Ott, Holder-J.Ryan.

2017-01-07, DET, SEA, SEA, (8:53) (Shotgun) T.Rawls right guard for 4 yards, TOUCHDOWN.

2017-01-07, DET, SEA, SEA, (3:43) (Shotgun) R.Wilson pass short middle to D.Baldwin for 13 yards, TOUCHDOWN.

2017-01-07, DET, SEA, SEA, S.Hauschka extra point is GOOD, Center-T.Ott, Holder-J.Ryan.

输入XML:

<?xml version="1.0"?>
<gam:gameboxscore xmlns:gam="http://leaguemanager.beacontender.com/plugin/feed/sport/usftbl/gameboxscore">
    <gam:lastUpdatedOn>2017-01-08 12:22:22 AM</gam:lastUpdatedOn>
    <gam:game>
        <gam:date>2017-01-07</gam:date>
        <gam:time>8:15PM</gam:time>
        <gam:awayTeam>
            <gam:ID>61</gam:ID>
            <gam:City>Detroit</gam:City>
            <gam:Name>Lions</gam:Name>
            <gam:Abbreviation>DET</gam:Abbreviation>
        </gam:awayTeam>
        <gam:homeTeam>
            <gam:ID>79</gam:ID>
            <gam:City>Seattle</gam:City>
            <gam:Name>Seahawks</gam:Name>
            <gam:Abbreviation>SEA</gam:Abbreviation>
        </gam:homeTeam>
        <gam:location>CenturyLink Field</gam:location>
    </gam:game>
    <gam:quarterSummary>
        <gam:quarter number="1">
            <gam:awayScore>0</gam:awayScore>
            <gam:homeScore>0</gam:homeScore>
            <gam:scoring/>
        </gam:quarter>
        <gam:quarter number="2">
            <gam:awayScore>3</gam:awayScore>
            <gam:homeScore>10</gam:homeScore>
            <gam:scoring>
                <gam:scoringPlay>
                    <gam:time>7:46</gam:time>
                    <gam:teamAbbreviation>SEA</gam:teamAbbreviation>
                    <gam:playDescription>(7:14) R.Wilson pass short right to P.Richardson for 2 yards, TOUCHDOWN. Penalty on DET-T.Wilson, Defensive Pass Interference, declined.</gam:playDescription>
                </gam:scoringPlay>
                <gam:scoringPlay>
                    <gam:time>7:53</gam:time>
                    <gam:teamAbbreviation>SEA</gam:teamAbbreviation>
                    <gam:playDescription>S.Hauschka extra point is GOOD, Center-T.Ott, Holder-J.Ryan.</gam:playDescription>
                </gam:scoringPlay>
                <gam:scoringPlay>
                    <gam:time>13:00</gam:time>
                    <gam:teamAbbreviation>SEA</gam:teamAbbreviation>
                    <gam:playDescription>(2:00) S.Hauschka 43 yard field goal is GOOD, Center-T.Ott, Holder-J.Ryan.</gam:playDescription>
                </gam:scoringPlay>
                <gam:scoringPlay>
                    <gam:time>14:35</gam:time>
                    <gam:teamAbbreviation>DET</gam:teamAbbreviation>
                    <gam:playDescription>(:25) M.Prater 51 yard field goal is GOOD, Center-D.Muhlbach, Holder-S.Martin.</gam:playDescription>
                </gam:scoringPlay>
            </gam:scoring>
        </gam:quarter>
        <gam:quarter number="3">
            <gam:awayScore>3</gam:awayScore>
            <gam:homeScore>0</gam:homeScore>
            <gam:scoring>
                <gam:scoringPlay>
                    <gam:time>10:52</gam:time>
                    <gam:teamAbbreviation>DET</gam:teamAbbreviation>
                    <gam:playDescription>(4:08) M.Prater 53 yard field goal is GOOD, Center-D.Muhlbach, Holder-S.Martin.</gam:playDescription>
                </gam:scoringPlay>
            </gam:scoring>
        </gam:quarter>
        <gam:quarter number="4">
            <gam:awayScore>0</gam:awayScore>
            <gam:homeScore>16</gam:homeScore>
            <gam:scoring>
                <gam:scoringPlay>
                    <gam:time>0:45</gam:time>
                    <gam:teamAbbreviation>SEA</gam:teamAbbreviation>
                    <gam:playDescription>(14:15) S.Hauschka 27 yard field goal is GOOD, Center-T.Ott, Holder-J.Ryan.</gam:playDescription>
                </gam:scoringPlay>
                <gam:scoringPlay>
                    <gam:time>6:07</gam:time>
                    <gam:teamAbbreviation>SEA</gam:teamAbbreviation>
                    <gam:playDescription>(8:53) (Shotgun) T.Rawls right guard for 4 yards, TOUCHDOWN.</gam:playDescription>
                </gam:scoringPlay>
                <gam:scoringPlay>
                    <gam:time>11:17</gam:time>
                    <gam:teamAbbreviation>SEA</gam:teamAbbreviation>
                    <gam:playDescription>(3:43) (Shotgun) R.Wilson pass short middle to D.Baldwin for 13 yards, TOUCHDOWN.</gam:playDescription>
                </gam:scoringPlay>
                <gam:scoringPlay>
                    <gam:time>11:24</gam:time>
                    <gam:teamAbbreviation>SEA</gam:teamAbbreviation>
                    <gam:playDescription>S.Hauschka extra point is GOOD, Center-T.Ott, Holder-J.Ryan.</gam:playDescription>
                </gam:scoringPlay>
            </gam:scoring>
        </gam:quarter>
        <gam:quarterTotals>
            <gam:awayScore>6</gam:awayScore>
            <gam:homeScore>26</gam:homeScore>
        </gam:quarterTotals>
    </gam:quarterSummary>
</gam:gameboxscore>

这是我正在使用的最新版本的XSLT。目前这只是给了我一张第一场得分赛的记录。

    <xsl:stylesheet version="2.0" xmlns:gam="http://leaguemanager.beacontender.com/plugin/feed/sport/usftbl/gameboxscore"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
    <xsl:template match="gam:gameboxscore">
        <xsl:text>GameDate,AwayTeam,HomeTeam,ScoringTeam,playDescription&#13;&#10;"</xsl:text>
        <xsl:value-of select="gam:game/gam:date"/>
        <xsl:text>","</xsl:text>
        <xsl:value-of select="gam:game/gam:awayTeam/gam:Abbreviation"/>
        <xsl:text>","</xsl:text>
        <xsl:value-of select="gam:game/gam:homeTeam/gam:Abbreviation"/>
        <xsl:text>","</xsl:text>
        <xsl:value-of select="gam:quarterSummary/gam:quarter/gam:scoring/gam:scoringPlay/gam:teamAbbreviation"/>
        <xsl:text>","</xsl:text>
        <xsl:value-of select="gam:quarterSummary/gam:quarter/gam:scoring/gam:scoringPlay/gam:playDescription"/>
        <xsl:text>"</xsl:text>
        <xsl:text>&#13;&#10;</xsl:text>
    </xsl:template>
    </xsl:stylesheet>

提前致谢!

1 个答案:

答案 0 :(得分:0)

如果您想为每个scoringPlay添加一行,则必须这样说。尝试:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:gam="http://leaguemanager.beacontender.com/plugin/feed/sport/usftbl/gameboxscore">
<xsl:output method="text" encoding="UTF-8" />

<xsl:template match="/gam:gameboxscore">
    <xsl:text>GameDate,AwayTeam,HomeTeam,ScoringTeam,playDescription&#13;&#10;</xsl:text>
    <xsl:variable name="common">
        <xsl:text>"</xsl:text>
        <xsl:value-of select="gam:game/gam:date"/>
        <xsl:text>","</xsl:text>
        <xsl:value-of select="gam:game/gam:awayTeam/gam:Abbreviation"/>
        <xsl:text>","</xsl:text>
        <xsl:value-of select="gam:game/gam:homeTeam/gam:Abbreviation"/>
        <xsl:text>","</xsl:text>
    </xsl:variable>
    <xsl:for-each select="gam:quarterSummary/gam:quarter/gam:scoring/gam:scoringPlay">
        <xsl:value-of select="$common"/>
        <xsl:value-of select="gam:teamAbbreviation"/>
        <xsl:text>","</xsl:text>
        <xsl:value-of select="gam:playDescription"/>
        <xsl:text>"&#13;&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

请注意,这假设您的文档仅包含有关一个游戏的数据。

相关问题