0
我有一个HTML文件中使用XSLT,在这里我要抓住仅为后者的子节点使用的第一个子节点的日期值转换成XML文件,因为后者的子节点没有数据值,标签将显示为空。我试图使用定位来为XSLT做这件事,但无济于事。XSLT使用后孩子的第一个孩子节点值节点
相关的HTML代码是:
<tbody>
<tr>
<th rowspan="8" scope="rowgroup">2005DEC</th>
<th scope="row">Blemish on Card</th>
</tr>
<tr>
<th scope="row">Damaged</th>
</tr>
<tr>
<th rowspan="8" scope="rowgroup">2006JAN</th>
<th scope="row">Lost</th>
</tr>
<tr>
<th scope="row">Stolen</th>
</tr>
</tbody>
相关的XSLT代码:
<xsl:for-each select="html/body/div/div/div/table/tbody/tr">
<Entry>
<xsl:choose>
<xsl:when test="th[@scope='rowgroup']">
<Year>
<xsl:value-of select="substring(th[@scope='rowgroup']/., 1, 4)" />
</Year>
<Month>
<xsl:value-of select="substring(th[@scope='rowgroup']/., 5, 3)" />
</Month>
</xsl:when>
<xsl:otherwise>
<Year>
<xsl:value-of select="substring(tr[1]/th[@scope='rowgroup']/., 1, 4)" />
</Year>
<Month>
<xsl:value-of select="substring(tr[1]/th[@scope='rowgroup']/., 5, 3)" />
</Month>
</xsl:otherwise>
</xsl:choose>
</Entry>
</xsl:for-each>
的XML代码,输出是下面的,这不是我想要的第二个孩子节点不具有第一个子节点的日期值,只是显示为年份和月份的空值标记:
<Entry>
<Year>2005</Year>
<Month>DEC</Month>
<Reason>Blemish on Card</Reason>
</Entry>
<Entry>
<Year/>
<Month/>
<Reason>Damaged</Reason>
</Entry>
<Entry>
<Year>2006</Year>
<Month>JAN</Month>
<Reason>Lost</Reason>
</Entry>
<Entry>
<Year/>
<Month/>
<Reason>Stolen</Reason>
</Entry>
我需要的是:
<Entry>
<Year>2005</Year>
<Month>DEC</Month>
<Reason>Blemish on Card</Reason>
</Entry>
<Entry>
<Year>2005</Year>
<Month>DEC</Month>
<Reason>Damaged</Reason>
</Entry>
<Entry>
<Year>2006</Year>
<Month>JAN</Month>
<Reason>Lost</Reason>
</Entry>
<Entry>
<Year>2006</Year>
<Month>JAN</Month>
<Reason>Stolen</Reason>
</Entry>
我该如何为我的XSLT做些什么?
这有效,但会影响到foreach。因为foreach以多个时间段为目标,但现在尽管每个foreach中的后者子节点都捕获了第一个子节点的值,但它们仅使用第一个foreach中的第一个子节点的值。 – iteong
您只显示了一个表格和一个for-each,所以我恐怕不能说出您的评论与什么相关。如果问题没有解决,那么编辑问题并显示输入的必要细节,XSLT和你想要的输出。 –
是否使用'子(../前同辈:: TR [@范围= 'rowgroup'] [1] /第[@范围= 'rowgroup'] /。,1,4)'给你想要的结果?我目前不确定您想如何识别具有相关值的行。 –