相当新的XSLT,我正在努力理解为什么在使用for-each时我无法获得更多的项目。
我正在处理的xml来自一个网页,该网页在命名元素或在DIV标签中放置项目时不遵循最近的约定,无论它是什么。我希望找回给定团队的游戏时间表:Hockey schedule
我的目标是获得一组游戏,每个游戏都有日期/时间,地点和团队。
挑战在于,由于数据混乱或我的xslt无能,我似乎无法将游戏数据分开。
我想要处理的表的html:
<div id="pnlViewTeamSchedules">
<p>
</p><table class="table-border" cellspacing="0" width="100%" border="0">
<tbody><tr>
<td class="table-header1">
Game Date
</td>
<td class="table-header1">
Location
</td>
<td class="table-header1">
Home
</td>
<td class="table-header1">
Visitor
</td>
<td class="table-header1">
Score
</td>
<td class="table-header1">
Game Stats
</td>
</tr>
<tr>
<td class="table-header2" colspan="6">
Regular Season Schedule
</td>
</tr>
<tr>
<td class="table-content2">
Monday, May 4, 2015 <br>9:30 PM
</td>
<td class="table-content2">
Garage
</td>
<td class="table-content2">
<a href="teams.aspx?teamID=993&LeagueID=1&SeasonID=95&TeamName=Arsenal">
Arsenal</a><br>
<span class="jersey"></span>
</td>
<td class="table-content2">
<a href="teams.aspx?teamID=350&LeagueID=1&SeasonID=95&TeamName=Fried+Pickles">
Fried Pickles</a><br>
<span class="jersey">White</span>
</td>
<td class="table-content2" align="center" nowrap="">
3
-
1
</td>
<td class="table-content2" align="center" nowrap="">
<a href="scoreboard_details.aspx?scheduleID=30950">
Game Final</a>
</td>
</tr>
<tr>
<td class="table-content1">
Monday, May 11, 2015 <br>9:30 PM
</td>
<td class="table-content1">
Garage
</td>
<td class="table-content1">
<a href="teams.aspx?teamID=350&LeagueID=1&SeasonID=95&TeamName=Fried+Pickles">
Fried Pickles</a><br>
<span class="jersey">Black</span>
</td>
<td class="table-content1">
<a href="teams.aspx?teamID=871&LeagueID=1&SeasonID=95&TeamName=Hawkeyes">
Hawkeyes</a><br>
<span class="jersey">Yellow</span>
</td>
<td class="table-content1" align="center" nowrap="">
7
-
3
</td>
<td class="table-content1" align="center" nowrap="">
<a href="scoreboard_details.aspx?scheduleID=31007">
Game Final</a>
</td>
</tr>
<tr>
<td class="table-content2">
Monday, May 18, 2015 <br>7:45 PM
</td>
<td class="table-content2">
Igloo
</td>
<td class="table-content2">
<a href="teams.aspx?teamID=992&LeagueID=1&SeasonID=95&TeamName=Pull+Top+Tuners">
Pull Top Tuners</a><br>
<span class="jersey"></span>
</td>
<td class="table-content2">
<a href="teams.aspx?teamID=350&LeagueID=1&SeasonID=95&TeamName=Fried+Pickles">
Fried Pickles</a><br>
<span class="jersey">White</span>
</td>
<td class="table-content2" align="center" nowrap="">
6
-
7
</td>
<td class="table-content2" align="center" nowrap="">
<a href="scoreboard_details.aspx?scheduleID=31025">
Game Final</a>
</td>
</tr>
<tr>
<td class="table-content1">
Saturday, May 23, 2015 <br>8:30 PM
</td>
<td class="table-content1">
Igloo
</td>
<td class="table-content1">
<a href="teams.aspx?teamID=494&LeagueID=1&SeasonID=95&TeamName=The+Spirit">
The Spirit</a><br>
<span class="jersey">Navy</span>
</td>
<td class="table-content1">
<a href="teams.aspx?teamID=350&LeagueID=1&SeasonID=95&TeamName=Fried+Pickles">
Fried Pickles</a><br>
<span class="jersey">White</span>
</td>
<td class="table-content1" align="center" nowrap="">
5
-
6
</td>
<td class="table-content1" align="center" nowrap="">
<a href="scoreboard_details.aspx?scheduleID=31129">
Game Final</a>
</td>
</tr>
<tr>
<td class="table-content2">
Tuesday, May 26, 2015 <br>10:15 PM
</td>
<td class="table-content2">
Gardens
</td>
<td class="table-content2">
<a href="teams.aspx?teamID=350&LeagueID=1&SeasonID=95&TeamName=Fried+Pickles">
Fried Pickles</a><br>
<span class="jersey">Black</span>
</td>
<td class="table-content2">
<a href="teams.aspx?teamID=966&LeagueID=1&SeasonID=95&TeamName=The+Scoregasms">
The Scoregasms</a><br>
<span class="jersey">Dark or White</span>
</td>
<td class="table-content2" align="center" nowrap="">
7
-
1
</td>
<td class="table-content2" align="center" nowrap="">
<a href="scoreboard_details.aspx?scheduleID=31145">
Game Final</a>
</td>
</tr>
<tr>
<td class="table-content1">
Wednesday, Jun 3, 2015 <br>9:30 PM
</td>
<td class="table-content1">
Gardens
</td>
<td class="table-content1">
<a href="teams.aspx?teamID=991&LeagueID=1&SeasonID=95&TeamName=Benders">
Benders</a><br>
<span class="jersey">Yellow</span>
</td>
<td class="table-content1">
<a href="teams.aspx?teamID=350&LeagueID=1&SeasonID=95&TeamName=Fried+Pickles">
Fried Pickles</a><br>
<span class="jersey">White</span>
</td>
<td class="table-content1" align="center" nowrap="">
-
</td>
<td class="table-content1" align="center" nowrap="">
<a href="scoreboard_details.aspx?scheduleID=31333">
</a>
</td>
</tr>
<tr>
<td class="table-content2">
Sunday, Jun 7, 2015 <br>9:00 PM
</td>
<td class="table-content2">
Garage
</td>
<td class="table-content2">
<a href="teams.aspx?teamID=350&LeagueID=1&SeasonID=95&TeamName=Fried+Pickles">
Fried Pickles</a><br>
<span class="jersey">Black</span>
</td>
<td class="table-content2">
<a href="teams.aspx?teamID=998&LeagueID=1&SeasonID=95&TeamName=Whoops+Dangled">
Whoops Dangled</a><br>
<span class="jersey">black</span>
</td>
<td class="table-content2" align="center" nowrap="">
-
</td>
<td class="table-content2" align="center" nowrap="">
<a href="scoreboard_details.aspx?scheduleID=31352">
</a>
</td>
</tr>
<tr>
<td class="table-content1">
Friday, Jun 12, 2015 <br>10:30 PM
</td>
<td class="table-content1">
Forum
</td>
<td class="table-content1">
<a href="teams.aspx?teamID=350&LeagueID=1&SeasonID=95&TeamName=Fried+Pickles">
Fried Pickles</a><br>
<span class="jersey">Black</span>
</td>
<td class="table-content1">
<a href="teams.aspx?teamID=817&LeagueID=1&SeasonID=95&TeamName=The+Hitmen">
The Hitmen</a><br>
<span class="jersey">White</span>
</td>
<td class="table-content1" align="center" nowrap="">
-
</td>
<td class="table-content1" align="center" nowrap="">
<a href="scoreboard_details.aspx?scheduleID=31371">
</a>
</td>
</tr>
<tr>
<td class="table-content2">
Monday, Jun 15, 2015 <br>8:00 PM
</td>
<td class="table-content2">
Gardens
</td>
<td class="table-content2">
<a href="teams.aspx?teamID=993&LeagueID=1&SeasonID=95&TeamName=Arsenal">
Arsenal</a><br>
<span class="jersey"></span>
</td>
<td class="table-content2">
<a href="teams.aspx?teamID=350&LeagueID=1&SeasonID=95&TeamName=Fried+Pickles">
Fried Pickles</a><br>
<span class="jersey">White</span>
</td>
<td class="table-content2" align="center" nowrap="">
-
</td>
<td class="table-content2" align="center" nowrap="">
<a href="scoreboard_details.aspx?scheduleID=31388">
</a>
</td>
</tr>
</tbody></table>
<br>
<a id="lnkTeamList" href="viewSchedules.aspx?LeagueID=1&SeasonID=95">Go Back To Team List</a><p></p>
这里有一些xslt将每个游戏分开并将所有游戏数据整合成一个&#34;东西&#34;,这部分我理解。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<Games>
<xsl:for-each select="//table[@class='table-border']/tbody/tr">
<Game>
<Stuff><xsl:value-of select="current()"/></Stuff>
</Game>
</xsl:for-each>
</Games>
</xsl:template>
</xsl:stylesheet>
当我尝试梳理实际游戏数据时,我会发现挑战,你会注意到游戏数据实际上是分开的; table-content1和table-content2。
最终我想要两个,但我遇到第一个问题。
在我的逻辑中,添加另一个for-each withing应该分开日期,位置和队...
所以改变这个:
<Game>
<Stuff><xsl:value-of select="current()"/></Stuff>
</Game>
到此:
<Game>
<xsl:for-each select="//td[@class='table-content2']">
<Stuff><xsl:value-of select="current()"/></Stuff>
</xsl:for-each>
</Game>
不给我任何数据:
<Games xmlns="http://ws.wso2.org/dataservice">
<Game>
<Stuff/>
</Game>
<Game>
<Stuff/>
</Game>
<Game>
<Stuff/>
</Game>
<Game>
.... stuff removed ....
</Games>
我期待看到的地方:
<Games xmlns="http://ws.wso2.org/dataservice">
<Game>
<Stuff>date data</Stuff>
<Stuff>home team data</Stuff>
<Stuff>visitor team data</Stuff>
<Stuff>score data</Stuff>
<Stuff>game stats data</Stuff>
</Game>
<Game>
<Stuff>date data</Stuff>
<Stuff>home team data</Stuff>
<Stuff>visitor team data</Stuff>
<Stuff>score data</Stuff>
<Stuff>game stats data</Stuff>
</Game>
<Game>
<Stuff> etc etc etc</stuff>
</Game>
</Games>
非常感谢任何帮助或指示。
答案 0 :(得分:2)
当您向我们展示的输入不完整和不是格式良好的XML时,很难提供建议。我怀疑代替:
<xsl:for-each select="//td[@class='table-content2']">
你想做的事:
<xsl:for-each select=".//td[@class='table-content2']">
但我无法重现你的结果,所以这只是一个猜测。