XSLT将名称中冒号的节点解析为CSV

时间:2017-02-08 04:11:06

标签: xml csv xslt

我是XML和XSLT的新手。我现在已经搜索谷歌几个小时了,还没有想出这个......

以下是我要解析的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:scoring>
    </gam:quarter>
    </gam:quarterSummary>
<gam:gameboxscore

这是我的XSLT代码。我正在尝试从XML文件创建CSV。它创建CSV,但只有已定义的列标题而没有实际数据。

<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="/">
  <xsl:text<GameDate;AwayTeam;HomeTeam;ScoringTeam;playDescription</xsl:text>
  <xsl:text>&#13;&#10;</xsl:text>
 <xsl:for-each select="/gam:gameboxscore">
   <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: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:teamabbreviation"/>
    <xsl:text>"</xsl:text>
    <xsl:text>&#13;&#10;</xsl:text>
</xsl:for-each>

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

1 个答案:

答案 0 :(得分:0)

您有两种类型的语法错误。我以此为例:

<xsl:value-of select="../gam:game/gam:awayteam/gam:Abbreviation"/>
  1. 这不起作用,因为你是在上下文中 gam:gameboxscore - 而gam:game不是 gam:gameboxscore..步骤是parent::node()的缩写。您 想用:

    <xsl:value-of select="./gam:game/gam:awayteam/gam:Abbreviation"/>
    

    或简单地说:

    <xsl:value-of select="gam:game/gam:awayteam/gam:Abbreviation"/>
    

    选择孩子 gam:game作为第一个位置步骤。

  2. XML区分大小写:gam:awayteam未选择 gam:awayTeam

  3. 现在,如果你尝试:

        <xsl:value-of select="gam:game/gam:awayTeam/gam:Abbreviation"/>
    

    您将获得实际价值。相应地更正其余select表达式。

    P.S。我不理解你输入的结构,或者就你的输出而言。会不会只有一款游戏?只有一场得分比赛?如果是这样,您可以将其缩短为:

    <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:template match="/gam:gameboxscore">
        <xsl:text>GameDate;AwayTeam;HomeTeam;ScoringTeam;playDescription&#13;&#10;"</xsl:text>
        <xsl:variable name="game" select="gam:game" />
        <xsl:value-of select="$game/gam:date"/>
        <xsl:text>";"</xsl:text>
        <xsl:value-of select="$game/gam:awayTeam/gam:Abbreviation"/>
        <xsl:text>";"</xsl:text>
        <xsl:value-of select="$game/gam:homeTeam/gam:Abbreviation"/>
        <xsl:text>";"</xsl:text>
        <xsl:variable name="scoring-play" select="gam:quarterSummary/gam:quarter/gam:scoring/gam:scoringPlay" />
        <xsl:value-of select="$scoring-play/gam:teamAbbreviation"/>
        <xsl:text>";"</xsl:text>
        <xsl:value-of select="$scoring-play/gam:playDescription"/>
        <xsl:text>"</xsl:text>
        <xsl:text>&#13;&#10;</xsl:text>
    </xsl:template>