如何根据以前的标记类值删除标记?

2024-03-28 06:09:03 发布

您现在位置:Python中文网/ 问答频道 /正文

如何根据以前的标记类值删除标记?你知道吗

输入

<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。你知道吗

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

我不太懂XSL。你知道吗

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

有人能帮我找到正确的xpath吗?你知道吗


Tags: ofto代码标记divhtmlbodytemplate
2条回答

Can anyone help me to get correct xpath?

在xpathis a bit cumbersome中完美匹配CSS类。假设除了类dropcap-image-rw之外没有任何包含dropcap-image-rw(如f.edropcap-image-qc-x)的CSS类,那么下面的简单xpath应该可以用来删除元素:

//*[@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也不熟悉,所以我不能对此提出任何建议

Can anyone help me to get correct xpath?

chrome扩展Selector Gadget使得获得xpath非常容易。只需单击一个元素(或一组元素),它就会生成XPath或CSS选择器。你知道吗

相关问题 更多 >