我是XSL的新手。我想從我的HTML中提取兩個值(標題和說明)。這是我的HTML看起來像如何使用XSL從html中提取標題和描述等元數據
...
tbody id="_tableBody">
<tr id="tcm:526-94999" class="alt-rowcolor" style="display: table-row;">
<th class="heading" scope="row" style="display: table-cell;">
<a onclick="displayAgreementPDFPopIn('202', 'ddctable-526-93813', 'Link_1382596320857', '540', 'false')" href="javascript:void(0)">529 Plan – Investment Instructions</a>
</th>
<td class="description" style="display: table-cell;">Change how your future contributions are invested or make an exchange of the contributions and earnings currently invested in your 529 college savings plan.</td>
</tr>
...
對於如我想
<title> 529 Plan – Investment Instructions</title>
<description> Change how your future contributions are invested or make an exchange of the contributions and earnings currently invested in your 529 college savings plan </description>
這是元素的XPATH:
/html/body/div[2]/div[4]/div[4]/table/tbody/
我下的所有其他標題和描述這條道路。我爲這個轉換創建了下面的XSL。
<xsl:template match="/">
<xsl:apply-templates select="/html/body/div[2]/div[4]/div[4]/table/tbody" />
</xsl:template>
<xsl:template match="tbody">
<xsl:call-template name="PDF_metadata">
</xsl:call-template>
</xsl:template>
<xsl:template name="PDF_metadata">
<xsl:variable name="title" select="/tr/th/a">
<xsl:variable name="description" select="/tr/th/td"/>
<xsl:attribute name="title">
<xsl:value-of select="$title" />
</xsl:attribute>
<xsl:attribute name="description">
<xsl:value-of select="$description" />
</xsl:template>
這是使用XSL的正確方法嗎?我做對了嗎?任何幫助,將不勝感激。
謝謝你的回答。我有一個很大的html文件,它有很多tr和td標籤。我想我必須指定整個路徑/ html/body/div [2]/div [4]/div [4]/table/tbody /才能到達該元素。你怎麼看? – Rose
@NupurJaiswal更新。由於'tbody'標籤具有'id'屬性,因此可以將標籤的搜索從'/ html/body/div [2]/div [4]/div [4]/table/tbody'大大簡化爲' // tbody [@id ='_ tableBody']' – AJNeufeld
太好了。非常感謝。我會檢查這個答案 – Rose