doushai4890 2019-02-04 15:56
浏览 275

如何修复“仅在文档开头允许的XML声明”

I am trying to generate a sitemap but somehow an extra DIV tag at the initial line of xml. I need to remove this wrong tag DIV from the xml output.

I've tried to gather the logic at first and segregate the generation of the xml side at the bottom. set header 'text/xml'. I tried to strip_tags the whole xml string before output, but then, it shows document empty

private function removeImageAndEmbeds ( $content )
{
    // remove img tags
    $re1='(<img).*?\\/.*?\\/.*?\\/.*?\\/.*?\\/.*?\\/.*?(\\/>)';
    if ( $c=preg_replace("/".$re1."/is", "", $content) ) $content = $c;

    // remove embedded tags
    $re2='(<div).*?(data-oembed-url=)(".*?").*?<\\/div>.*?(<\\/div>)';
    if ( $c=preg_replace("/".$re2."/is", "", $content) ) $content = $c;

    return $content;
}

public function sitemaps ($tenantName="") {

    if ( !empty($tenantName) ) {

            $this->db->like( 't.name', str_replace('-', ' ', rawurldecode($tenantName)), 'none' );
            $results = $this->db->get($this->TBL . ' t')->result_array();

            foreach ( $results as $result ) {

                $tenantId = $result['id'];
                $tenantNameinURL = formatTenantNameinURL( $result['name'] );

                $AllItems =  $this->db->get_where($this->DIVIEW . ' di', 'di.account_id = '. $tenantId)->result_array();

                $topics = [];
                $itemIds = [];
                $ddIds = [];
                $urls = [];
                foreach ( $AllItems as $k => $item ) {
                    $pieces = explode('_', $item['id']);
                    if ( $pieces[1] === $this->ITEMTBL ) {
                        if( !in_array($item['record_id'], $itemIds) ){

                            $itemIds[] = $item['record_id'];
                            $content = $this->removeImageAndEmbeds( $item['content'] );

                            $AllItems[$k]['content'] = $content;
                            $topics[$k][] = $AllItems[$k];
                            $urls[$k]['url'] = formatFrontEndURL( $this->current_class_name, $tenantName, 'show', $pieces[0] );
                        }
                    } else if ( $pieces[1] === 'dataDefinitions' ) {
                        if( !in_array($item['record_id'], $ddIds) ){

                            $ddIds[] = $item['record_id'];
                            $content = $this->removeImageAndEmbeds( $item['content'] );

                            $AllItems[$k]['content'] = $content;
                            $topics[$k][] = $AllItems[$k];
                            $urls[$k]['url'] = formatFrontEndURL( $this->current_class_name, $tenantName, 'data_definition', $pieces[0] );
                        }
                    }
                }

                $urlset = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset />');
                $urlset->addAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
                foreach ($topics as $i => $itemsInTopic) {

                    $url = $urlset->addChild('url');
                    $url->loc = $urls[$i]['url'];

                    $pageMap = $url->addChild('PageMap');
                    $pageMap->addAttribute('xmlns', 'http://www.google.com/schemas/sitemap-pagemap/1.0');
                    foreach ( $itemsInTopic as $item ) {

                        $content = $item['content'];
                        $content = trim( str_replace(["&nbsp;","","
","\t", "&#13;", "&#10;"], ' ', strip_tags( utf8_decode( $content ) )) );
                        $dataObject = $pageMap->addChild('DataObject');
                        $dataObject->addAttribute('type', 'document');
                        $dataObject->addAttribute('id', $item['record_id']);
                        $dataObject->Attribute[0]['name'] = 'title';
                        $dataObject->Attribute[0] = $item['title'];
                        $dataObject->Attribute[1]['name'] = 'content';
                        $dataObject->Attribute[1] = $content;
                    }
                }

                $xmlContent = $urlset->asXML();
                $this->output->set_content_type('text/xml')->set_output( $xmlContent );
            }
        }
  }

here are two errors generated from seochat validator https://drive.google.com/file/d/1vacmuJL6hnMErzqZ5zZWkkObT74rKOmT/view?usp=sharing https://drive.google.com/file/d/1y3z85D1WtJIT9GvOC-DeYwS-DtQCAxK5/view?usp=sharing

here is google console error https://drive.google.com/file/d/1qMvifyjGILqAjJzdWdc90jyymvdUFV5A/view?usp=sharing

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 spring后端vue前端
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题