【问题标题】:XPATH getting all tags without <script> and </script> tagsXPATH 获取所有没有 <script> 和 </script> 标签的标签
【发布时间】:2011-04-20 09:23:30
【问题描述】:

我在使用 Xpath 获取没有 &lt;script&gt;&lt;script ... /&gt; 的所有 html 标签时遇到问题。

例如,在这部分 HTML 代码中,我想删除:

<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=fr"/>

对于这个代码

<li><!-- Search Google -->
<center>
                     <form action="http://www.google.fr/cse" id="cse-search-box" target="_blank">
                        <div>
                           <input type="hidden" name="cx" value="partner-pub-0959382714089534:mw3ssl65jk1"/>
                           <input type="hidden" name="ie" value="ISO-8859-1"/>
                           <input type="text" name="q" size="31"/>
                           <input type="submit" name="sa" value="Rechercher"/>
                        </div>
                     </form>
                     <script type="text/javascript"
                             src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=fr"/>
                  </center>
                  <!-- Search Google --></li>

我正在使用 Web-Harvest 生成一个 xml 文件,然后我必须删除一些具体的标签。 我尝试了很多 xpath(我在 html 正文中工作):

  • //body//*[not(name() = 'script')]

  • //body//*[not(self::script)]

  • //body//*[not(starts-with(name(),'script'))]

  • //body//*[not(contains(name(),'script'))]

但它不起作用。

请注意 //body//*[name() = 'script'] 正在工作,但我想要相反...

你有什么想法吗?

或者更笼统地说,如果您知道如何使用 Xpath 删除所有 &lt;script&gt; &lt;script/&gt; 标签,我也有兴趣:-)

提前致谢。

【问题讨论】:

  • 好问题,+1。请参阅我的回答,了解为什么仅使用 XPath 无法实现这一点,以及完整、简短且简单的 XSLT 解决方案。 :)

标签: html xpath tags


【解决方案1】:

首先,XPath 选择现有文档中的节点,它不会删除它们。并且您的路径 //body//* 您从选择 body 元素的所有子元素和后代元素开始。即使您现在添加像 //body//*[not(self::script)] 这样的谓词,该路径仍然会选择像 licenter 元素这样的元素,它们本身不是 script 元素,但包含 script 元素。所以//body//*[not(self::script)] 是不选择任何非script 元素的正确方法,但如果您想要删除script 元素的原始center 元素,它也无济于事。这不是纯 XPath 能为您做的事情,您需要转移到 XSLT 来转换文档,然后删除任何 script 元素。

【讨论】:

    【解决方案2】:

    XPath 只是 XML 文档的一种查询语言,因此它不能以任何方式改变正在查询的 XML 文档

    生成不同于初始 XML 文档的新 XML 文档最方便的方法是使用 XSLT。

    这个简短的 XSLT 转换

    <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="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="script"/>
    </xsl:stylesheet>
    

    应用于提供的 XML 文档时:

    <li>
        <!-- Search Google -->
        <center>
            <form action="http://www.google.fr/cse"
                  id="cse-search-box" target="_blank">
                <div>
                    <input type="hidden" name="cx"
                    value="partner-pub-0959382714089534:mw3ssl65jk1"/>
                    <input type="hidden" name="ie" value="ISO-8859-1"/>
                    <input type="text" name="q" size="31"/>
                    <input type="submit" name="sa" value="Rechercher"/>
                </div>
            </form>
            <script type="text/javascript"
            src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=fr"/>
        </center>
        <!-- Search Google -->
    </li>
    

    产生想要的正确结果

    <li><!-- Search Google -->
       <center>
          <form action="http://www.google.fr/cse" id="cse-search-box" target="_blank">
             <div>
                <input type="hidden" name="cx" value="partner-pub-0959382714089534:mw3ssl65jk1"/>
                <input type="hidden" name="ie" value="ISO-8859-1"/>
                <input type="text" name="q" size="31"/>
                <input type="submit" name="sa" value="Rechercher"/>
             </div>
          </form>
       </center><!-- Search Google -->
    </li>
    

    【讨论】:

      【解决方案3】:

      问题列表中的第一个 XPath 几乎就在那里:

      • //body//*[not(name() = 'script')]

      我们可以使用XPath Axes中的descendant,应该是:

      • //body/descendant::*[not(name() = 'script')]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-24
        • 2014-01-13
        • 1970-01-01
        • 2018-03-11
        • 1970-01-01
        • 2011-09-02
        • 1970-01-01
        相关资源
        最近更新 更多