我有一个相当平坦的桌子,看起来像
<tr>
<td class="tableResultsA" nowrap>1</td>
<td class="tableResultsA" nowrap><A HREF="docs/123456" target="_blank">Title</A></td>
<td class="tableResultsA" nowrap>SNIPPET</td>
<td class="tableResultsA" nowrap>Date</td>
<td class="tableResultsA" nowrap>Category</td>
</tr>
<tr>
<td class="tableResultsB" nowrap>1</td>
<td class="tableResultsB" nowrap><A HREF="docs/678901" target="_blank">Title</A></td>
<td class="tableResultsB" nowrap>SNIPPET</td>
<td class="tableResultsB" nowrap>Date</td>
<td class="tableResultsB" nowrap>Category</td>
</tr>
我需要从这些数据中创建一个文档,包括URL,Title和Snippet。
我已将根结果节点设置为:
<scope>
<xsl:value-of select>"//td[@class='tableResultsA'][2] | //td[@class='tableResultsB'][2]"
</scope>
对于我的文件,我有以下内容:
<xsl:template match="//td">
<document url="{a/@href}">
<content name="title">
<xsl:value-of select="a" />
</content>
<content name="snippet">
<xsl:value-of select="//td[@class='tableResultsA'][3] | //td[@class='tableResultsB'][3]" />
</content>
</document>
</xsl:template>
问题是片段。我在我的代码段输出中获得了完全相同的结果,因此它不会遍历数据。我不是XPath专家。我想知道跟随或跟随兄弟会是否有效但我无法在任何地方为他们找到一个好的例子。
任何帮助将不胜感激。
答案 0 :(得分:0)
此转化:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="tr">
<document url="{td/A/@HREF}">
<content name="title">
<xsl:value-of select="td/A"/>
</content>
<content name="snippet">
<xsl:value-of select="td[3]"/>
</content>
</document>
</xsl:template>
</xsl:stylesheet>
应用于已更正(成为格式良好的XML文档)提供的输入:
<table>
<tr>
<td class="tableResultsA" nowrap="nowrap">1</td>
<td class="tableResultsA" nowrap="nowrap">
<A HREF="docs/123456" target="_blank">Title</A>
</td>
<td class="tableResultsA" nowrap="nowrap">SNIPPET</td>
<td class="tableResultsA" nowrap="nowrap">Date</td>
<td class="tableResultsA" nowrap="nowrap">Category</td>
</tr>
<tr>
<td class="tableResultsB" nowrap="nowrap">1</td>
<td class="tableResultsB" nowrap="nowrap">
<A HREF="docs/678901" target="_blank">Title</A>
</td>
<td class="tableResultsB" nowrap="nowrap">SNIPPET</td>
<td class="tableResultsB" nowrap="nowrap">Date</td>
<td class="tableResultsB" nowrap="nowrap">Category</td>
</tr>
</table>
会产生想要的正确结果:
<document url="docs/123456">
<content name="title">Title</content>
<content name="snippet">SNIPPET</content>
</document>
<document url="docs/678901">
<content name="title">Title</content>
<content name="snippet">SNIPPET</content>
</document>