首页  >  问答  >  正文

获取第一个段落之后的标题:XPath

<p>我想在我的网站上添加一个FAQPage架构。</p> <p>为了做到这一点,我需要找到每一个带有问号的<code><h2></code>或<code><h3></code>标签。那就是问题。</p> <p>之后,我需要将标题后的第一个<code><p></code>标签作为答案。</p> <p>最终结果应该如下所示:</p> <pre class="brush:php;toolbar:false;">{ "@type": "Question", "name": "处理退款需要多长时间?", "acceptedAnswer": { "@type": "Answer", "text": "从第一个P标签中获取的内容", "url": "https://www.example.com/answer#anchor_link" } }</pre> <ul> <li>问题的<code>"name"</code>是<code><h2></code>或<code><h3></code>标签。</li> <li>答案的<code>"url"</code>是从<code><h2></code>或<code><h3></code>标签中获取的永久链接和锚链接。</li> <li><strong>这两个参数已经解决了</strong></li> </ul> <p>不幸的是,我无法找到如何获取标题标签后的第一个段落标签。</p> <p>我需要在以下行中获取第一个段落的内容:</p> <pre class="brush:php;toolbar:false;">"text": "从第一个P标签中获取的内容",</pre> <p>这是我目前的代码:</p> <pre class="brush:php;toolbar:false;"><?php $content_postid = get_the_ID(); $content_post = get_post($content_postid); $content = $content_post->post_content; $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); libxml_use_internal_errors(true); $dom = new DOMDocument; $dom->loadHTML('<?xml encoding="utf-8" ?>' . $content); $xp = new DOMXPath($dom); $query = "//h2[contains(., '?')] | //h3[contains(., '?')]"; $nodes = $xp->query($query); $stack = []; if ($nodes) { $faq_count = count($nodes); $faq_i = 1; echo ' <script type="application/ld+json"> { "@context": "https://schema.org", "@type": "FAQPage", "mainEntity": ['; foreach($nodes as $node) { echo '{ "@type": "Question", "name": "'.$node->nodeValue.'", "acceptedAnswer": { "@type": "Answer", "text": "从第一个P标签中获取的内容", "url": "'.get_permalink().'#'.$node->getAttribute('id').'" } }'; if ($faq_i != $faq_count) : echo ','; endif; $faq_i++; } echo ']}</script>'; } ?></pre> <p>正如你所看到的,我使用这行代码来查找每一个带有<code>?</code>的<code><h2></code>或<code><h3></code>标签:</p> <pre class="brush:php;toolbar:false;">$query = "//h2[contains(., '?')] | //h3[contains(., '?')]";</pre> <p>我猜我需要一个第二个<code>$query</code>来找到标题后的段落?但是如何检查标题后的第一个标签呢?</p> <p>我尝试了这个额外的查询:</p> <pre class="brush:php;toolbar:false;">$query2 = "//h2[contains(., '?')]/following-sibling::p[1] | //h3[contains(., '?')]/following-sibling::p[1]";</pre> <p>但是<code>following-sibling::</code>和<code>following::</code>都不起作用。 它总是显示最后一个标题后的段落。</p> <p>我需要解决第一个查询吗?以了解我在什么级别上?</p> <p>这是<code>$content_post</code>的一个示例(它总是不同的):</p> <pre class="brush:php;toolbar:false;"><h2>Lorem ipsum dolor sit amet?</h2> <p>consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim</p> <p>veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p> <h3>Duis autem vel eum?</h3> <p>iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p> <h2>Nam liber tempor cum soluta?</h2> <h3>nobis eleifend option congue nihil</h3> <p>imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p> <p>Et wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p> <h3>Duis autem vel?</h3> <p>eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p> <h4>Nam liber tempor cum soluta nobis</h4> <p>eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum.</p></pre> <p><br /></p>
P粉447002127P粉447002127449 天前636

全部回复(1)我来回复

  • P粉957661544

    P粉9576615442023-08-21 18:20:03

    尝试将您的foreach更改为以下内容,看看是否有效。

    foreach($nodes as $node) {
            $ans = $xp->query("./following-sibling::p[1]",$node)[0]->nodeValue;
            echo "{
                    '@type': 'Question',
                    'name': '".$node->nodeValue."',
                    'acceptedAnswer': {
                        '@type': 'Answer',
                        'text': {$ans}
                    }
                }";

    回复
    0
  • 取消回复