XSL输出到表

时间:2016-09-18 20:56:47

标签: xml xslt

所以我正在尝试学习XSL,而我正在考虑将XML文档输出到表中。我可以获得1行填充设置,我知道这是不正确的,但它结合了来自不同玩家的记录。如果我尝试选择PLAYER或TEAM_NAME,我就没有记录。我似乎无法理解它应该如何填充每个。出于某种原因,它正好在我的头上。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
		<h2>SEASON</h2>
			<table border="1">
				<tr>
					<th>YEAR</th>
					<th>LEAGUE</th>
					<th>DIVISION</th>
					<th>TEAM_CITY</th>
					<th>TEAM_NAME</th>
					<th>GIVEN_NAME</th>
					<th>SURNAME</th>
					<th>POSITION</th>
					<th>GAMES</th>
					<th>GAMES_STARTED</th>
					<th>WINS</th>
					<th>LOSSES</th>
					<th>SAVES</th>
					<th>COMPLETE_GAMES</th>
					<th>SHUT_OUTS</th>
					<th>ERA</th>
					<th>INNINGS</th>
					<th>EARNED_RUNS</th>
					<th>HIT_BATTER</th>
					<th>WILD_PITCHES</th>
					<th>BALK</th>
					<th>WALKED_BATTER</th>
					<th>STRUCK_OUT_BATTER</th>
					<th>AT_BATS</th>
					<th>RUNS</th>
					<th>HITS</th>
					<th>DOUBLES</th>
					<th>TRIPLES</th>
					<th>"HOME_RUNS" /></th>
					<th>RBI</th>
					<th>STEALS</th>
					<th>CAUGHT_STEALING</th>
					<th>SACRIFICE_HITS</th>
					<th>SACRIFICE_FLIES</th>
					<th>ERRORS</th>
					<th>WALKS</th>
					<th>STRUCK_OUT</th>
					<th>HIT_BY_PITCH</th>
				</tr>
				<xsl:for-each select="/SEASON/LEAGUE/DIVISION/TEAM/TEAM_NAME">
				<tr>
					<td><xsl:value-of select="SEASON/YEAR" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/LEAGUE_NAME" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/DIVISION_NAME" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/TEAM_CITY" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/TEAM_NAME" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/GIVEN_NAME" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/SURNAME" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/POSITION" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/GAMES" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/GAMES_STARTED" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/WINS" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/LOSSES" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/SAVES" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/COMPLETE_GAMES" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/SHUT_OUTS" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/ERA" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/INNINGS" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/EARNED_RUNS" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/HIT_BATTER" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/WILD_PITCHES" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/BALK" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/WALKED_BATTER" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/STRUCK_OUT_BATTER" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/AT_BATS" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/RUNS" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/HITS" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/DOUBLES" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/TRIPLES" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/HOME_RUNS" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/RBI" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/STEALS" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/CAUGHT_STEALING" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/SACRIFICE_HITS" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/SACRIFICE_FLIES" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/ERRORS" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/WALKS" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/STRUCK_OUT" /></td>
					<td><xsl:value-of select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER/HIT_BY_PITCH" /></td>
				</tr>
				</xsl:for-each>
			</table>

	</body>
</html>

</xsl:template>

</xsl:stylesheet>

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<SEASON>
	<YEAR>1998</YEAR>
	
	<LEAGUE>
		<LEAGUE_NAME>National</LEAGUE_NAME>

		<DIVISION>
			<DIVISION_NAME>East</DIVISION_NAME>
		
			<TEAM>
				<TEAM_CITY>Florida</TEAM_CITY>
				<TEAM_NAME>Marlins</TEAM_NAME>
			
				<PLAYER>
					<GIVEN_NAME>Eric</GIVEN_NAME>
					<SURNAME>Ludwick</SURNAME>
					<POSITION>Starting Pitcher</POSITION>
					<GAMES>13</GAMES>
					<GAMES_STARTED>6</GAMES_STARTED>
					<WINS>1</WINS>
					<LOSSES>4</LOSSES>
					<SAVES>0</SAVES>
					<COMPLETE_GAMES>0</COMPLETE_GAMES>
					<SHUT_OUTS>0</SHUT_OUTS>
					<ERA>7.44</ERA>
					<INNINGS>32.2</INNINGS>
					<EARNED_RUNS>31</EARNED_RUNS>
					<HIT_BATTER>27</HIT_BATTER>
					<WILD_PITCHES>0</WILD_PITCHES>
					<BALK>2</BALK>
					<WALKED_BATTER>0</WALKED_BATTER>
					<STRUCK_OUT_BATTER>17</STRUCK_OUT_BATTER>
					</PLAYER>

				<PLAYER>
					<GIVEN_NAME>Brian</GIVEN_NAME>
					<SURNAME>Daubach</SURNAME>
					<POSITION>First Base</POSITION>
					<GAMES>10</GAMES>
					<GAMES_STARTED>3</GAMES_STARTED>
					<AT_BATS>15</AT_BATS>
					<RUNS>0</RUNS>
					<HITS>3</HITS>
					<DOUBLES>1</DOUBLES>
					<TRIPLES>0</TRIPLES>
					<HOME_RUNS>0</HOME_RUNS>
					<RBI>3</RBI>
					<STEALS>0</STEALS>
					<CAUGHT_STEALING>0</CAUGHT_STEALING>
					<SACRIFICE_HITS>0</SACRIFICE_HITS>
					<SACRIFICE_FLIES>0</SACRIFICE_FLIES>
					<ERRORS>0</ERRORS>
					<WALKS>1</WALKS>
					<STRUCK_OUT>5</STRUCK_OUT>
					<HIT_BY_PITCH>1</HIT_BY_PITCH>
				</PLAYER>
			</TEAM>

			<TEAM>
				<TEAM_CITY>Montreal</TEAM_CITY>
				<TEAM_NAME>Expos</TEAM_NAME>
			</TEAM>

			<TEAM>
				<TEAM_CITY>New York</TEAM_CITY>
				<TEAM_NAME>Mets</TEAM_NAME>
			</TEAM>

			<TEAM>
				<TEAM_CITY>Philadelphia</TEAM_CITY>
				<TEAM_NAME>Phillies</TEAM_NAME>
			</TEAM>
		</DIVISION>
		
		<DIVISION>
			<DIVISION_NAME>Central</DIVISION_NAME>
			<TEAM>
				<TEAM_CITY>Chicago</TEAM_CITY>
				<TEAM_NAME>Cubs</TEAM_NAME>
			</TEAM>
		</DIVISION>

		<DIVISION>
			<DIVISION_NAME>West</DIVISION_NAME>
			<TEAM>
				<TEAM_CITY>Arizona</TEAM_CITY>
				<TEAM_NAME>Diamondbacks</TEAM_NAME>
			</TEAM>
		</DIVISION>
	</LEAGUE>

	<LEAGUE>
		<LEAGUE_NAME>American</LEAGUE_NAME>
	
		<DIVISION>
			<DIVISION_NAME>East</DIVISION_NAME>
			<TEAM>
				<TEAM_CITY>Baltimore</TEAM_CITY>
				<TEAM_NAME>Orioles</TEAM_NAME>
			</TEAM>
		</DIVISION>

		<DIVISION>
			<DIVISION_NAME>Central</DIVISION_NAME>
			<TEAM>
				<TEAM_CITY>Chicago</TEAM_CITY>
				<TEAM_NAME>White Sox</TEAM_NAME>
			</TEAM>
		</DIVISION>

		<DIVISION>
			<DIVISION_NAME>West</DIVISION_NAME>
			<TEAM>
				<TEAM_CITY>Anaheim</TEAM_CITY>
				<TEAM_NAME>Angels</TEAM_NAME>
			</TEAM>
		</DIVISION>
	</LEAGUE>
</SEASON>

1 个答案:

答案 0 :(得分:1)

如果(看起来)你希望每个玩家占据一排,那么你需要为每个玩家创建一行 - 并从相应的祖先节点获得赛季,联赛,分区和团队数据。

试试这个最小化的例子:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>

<xsl:template match="/">
    <html>
        <body>
            <h2>SEASON</h2>
            <table border="1">
                <tr>
                    <th>YEAR</th>
                    <th>LEAGUE</th>
                    <th>DIVISION</th>
                    <th>TEAM_CITY</th>
                    <th>TEAM_NAME</th>
                    <th>GIVEN_NAME</th>
                    <th>SURNAME</th>
                </tr>
                <xsl:for-each select="SEASON/LEAGUE/DIVISION/TEAM/PLAYER">
                    <tr>
                        <td><xsl:value-of select="ancestor::SEASON/YEAR" /></td>
                        <td><xsl:value-of select="ancestor::LEAGUE/LEAGUE_NAME" /></td>
                        <td><xsl:value-of select="ancestor::DIVISION/DIVISION_NAME" /></td>
                        <td><xsl:value-of select="../TEAM_CITY" /></td>
                        <td><xsl:value-of select="../TEAM_NAME" /></td>
                        <td><xsl:value-of select="GIVEN_NAME" /></td>
                        <td><xsl:value-of select="SURNAME" /></td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

应用于您的示例输入,(呈现)结果将为:

enter image description here