529 -6ren">
gpt4 book ai didi

html - 如何使用 XSL 从 html 中提取标题和描述等元数据

转载 作者:行者123 更新时间:2023-11-28 01:02:14 24 4
gpt4 key购买 nike

我是 XSL 的新手。我想从我的 HTML 中提取两个值(标题和描述)。这就是我的 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 的正确方法吗?我这样做对吗?任何帮助将不胜感激。

最佳答案

我认为您将上一个模板过于复杂化了。未经测试,但我认为这更接近您想要的:

<xsl:template name="PDF_metadata">
<title>
<xsl:value-of select="tr/th/a" />
</title>
<description>
<xsl:value-of select="tr/td" />
</description>
</xsl:template>

更新

使用在线 XSLT 测试器。这应该适合你;它用一个简单的模板替换了所有 3 个模板。

<xsl:template match="//tbody[@id='_tableBody']">
<title>
<xsl:value-of select="tr/th/a" />
</title>
<description>
<xsl:value-of select="tr/td" />
</description>
</xsl:template>

解释:

//tbody找到任何 <tbody/>根节点下的节点。不管它嵌套多深,或者它在内部的什么位置 <div/>标签等。但是可以有不止一个,所以 ...

//tbody[@id='_tableBody'] ...只匹配<tbody/>具有属性 id='_tableBody' .自 id属性必须是唯一的,只能有一个。

<xsl:value-of select="..." /> , 我们已经在 <tbody/>节点。要获得标题,我们不想使用 /tr/th/a 从文档的根目录开始搜索... 仅来自当前节点(<tbody/> 节点),使用 tr/th/a (注意开头缺少 /)。描述同上。

关于html - 如何使用 XSL 从 html 中提取标题和描述等元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41580872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com