python - 如何根据标签之前的标签类别值删除标签?

标签 python xml xslt xpath

如何根据之前的标签类别值删除标签?

输入:

<html>
<body>
<div>
<p id="quarter-line-below1"><span class="dropcap-image-qc ><img alt="2014" src="243864_20.png" /></span><span class="dropcap-qc">2014 </span>has had some strange and negative commentary about publishing with HTML5. The comments appear to be focused on HTML for trade fiction books and the requirements of publishing genres beyond simple narratives seems to be ignored.</p>
</div>
</body>
</html>

我必须删除所有包含 dropcap-qc 的标签即<span class="dropcap-qc">2014 </span>

这样就完成了。

XSL 代码:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="no"/>
    <xsl:preserve-space elements="*"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>


<xsl:template match="//*[@class='dropcap-qc']"></xsl:template>

</xsl:stylesheet>

我正在通过 Python 代码创建上面的 XSL。我获取所有已删除的标记类名称,然后创建 XSL。

我的用于获取类名称的Python代码:https://stackoverflow.com/questions/30482435/how-to-get-count-of-every-column-value-of-table

我不太了解 XSL。

我的问题是,我想删除所有 dropcap-qc标签,但这应该是 dropcap-image-qc 的下一个标签标签。

任何人都可以帮助我获得正确的xpath吗?

最佳答案

Can anyone help me to get correct xpath?

在 xpath 中完美匹配 CSS 类 is a bit cumbersome 。假设您没有任何包含 dropcap-image-rw 的 CSS 类,以下更简单的 xpath 应该可以获取要删除的元素。 (如 f.e dropcap-image-qc-x )除了类 dropcap-image-rw 之外它本身:

//*[@class='dropcap-qc' and preceding-sibling::*[1][contains(@class, 'dropcap-image-qc')]]

上面的 xpath 选择所有具有类 dropcap-qc 的元素位于直接包含类的元素之后 dropcap-image-qc .

根据要求提供有关 xpath 的更多说明:

  • preceding-sibling::*[1] :获取当前上下文元素的直接前级元素。这将是同一级别当前元素之前的元素。

  • [contains(@class, 'dropcap-image-qc')] :验证当前元素(xpath 的前一位返回的元素)是否具有包含 "dropcap-image-qc" 的类属性

我也不熟悉 XSL,所以我无法就这部分提出任何建议

关于python - 如何根据标签之前的标签类别值删除标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30506160/

相关文章:

python - 具有多个元素的 Django __str__

android - 如何让我的抽屉导航始终位于所有内容之上?安卓

xml - XPath:返回具有特定子节点但没有自己文本的节点

c# - 如何搜索和导航 XML 节点

html - SSRS 中 HTML 的 Spacebefore 和 Spaceafter

xml - 使用 spreadsheetml 包装文本的样式

html - XSLT - CSS 样式表的所有元素属性的组合

python - 训练聊天机器人时,chatterbot python3 OperatingalError : no such column: statement. 对话

python - 使用python在django中将任何格式(flv、3gp、MXF等)的视频转换为MP4

python - SQLAlchemy 名称错误 : Name 'db' is not defined (? )