简体   繁体   English

解析tei domxpath获取评估循环内的文本子标记

[英]parse tei domxpath get text child tag inside evaluate loop

From a string that contain a tei file, I generate an index to navigate to their blocks, I retrieve all the div tags, I also want to get, if present, the content of a tag (the tag <head> ) inside current div. 从包含tei文件的字符串中,我生成一个索引以导航到它们的块,检索所有div标签,我还想获取当前div中标签的内容(标签<head> )(如果存在) 。

Example tei file: tei文件示例:

    <div type="lib" n="1"><head>LIBER I</head>...
<div type="pr">...</div>
<div type="cap" n="1"><head>CAP EX</head><p><milestone unit="par" n="1" />...<milestone unit="par" n="2" />...</div>
<div type="cap" n="2"><head>CAP EX</head><milestone unit="par" n="1" />...<milestone unit="par" n="2" />...</div>
</div>

I tried this but don't work: 我尝试了这个,但是不起作用:

 //source file:
  $fulltext = '<div type="lib" n="1"><head>LIBER I</head>...<div type="pr">...</div><div type="cap" n="1"><head>CAP EX</head><p><milestone unit="par" n="1" />...<milestone unit="par" n="2" />...</div><div type="cap" n="2"><head>CAP EX</head><milestone unit="par" n="1" />...<milestone unit="par" n="2" />...</div></div>';
    $dom = new DOMDocument();
    @$dom->loadHTML($fulltext);
    $domx = new DOMXPath($dom);
    $entries = $domx->evaluate("//div");
    echo '<ul>';
    foreach ($entries as $entry){
    $title = '';
    type = $entry->getAttribute( 'type' );
    $n = $entry->getAttribute( 'n' );
    $head = $domx->evaluate("string(./head[1])",$entry);
    if( $head != '' ) $title = $head; else $title = $n;
    echo '<li><a href="#'.$type.'-'.$n.'">'.$title.'</li>';
    }
    echo '</ul>';

The line don't work: 该行不起作用:

$head = $domx->evaluate("string(./head[1])",$entry);

Error returned: 返回错误:

 DOMDocument::loadHTML(): htmlParseStartTag: misplaced <head> tag in Entity, line: 3

The purpose of this line is to get the text of the child tag head inside the loop (in this example "LIBER I") 该行的目的是在循环内获取子标签头的文本(在此示例中为“ LIBER I”)

Using the @ symbol on the load can hide all sorts of issues. 在负载上使用@符号可以隐藏各种问题。 So if you take it out you get errors with your document. 因此,如果将其取出,则文档会出错。

If however you change the line to 但是,如果您将行更改为

$dom->loadXML($fulltext);

The output gives you what your after. 输出将为您提供服务。

Resolved using XMLReader: 使用XMLReader解决:

    $level = 0;
                $indici_bc = array();
                $indici_head = array();
                $passed_milestone = false;
                $xml = new XMLReader(); 
                $xml->open($pathTei);
                //$xml->xml($testo);
                while ($xml->read()){
                    if($xml->nodeType == XMLReader::END_ELEMENT && $xml->name == 'div'){
                        $level--;
                        $last_blocco = $xml->name;
                        if($passed_milestone){ $level--; $passed_milestone = false; }
                    }
                    if($xml->nodeType == XMLReader::ELEMENT && ($xml->name == 'div' || $xml->name == 'milestone' )){
                        $blocco = $xml->name;
                        $type = $xml->getAttribute('type');
                        $n = $xml->getAttribute('n');
                        $unit =  isset($xml->getAttribute('unit')) ? $xml->getAttribute('unit') : '';

//here I get the child node
$node = new SimpleXMLElement($xml->readOuterXML());
                        $head = $node->head ? (string)$node->head : '';

                        $indici_head[] = $head;
                        if($last_blocco != 'milestone') $level++;
                        if($blocco == 'div') $bc[$level] = $n; else $bc[($level+1)] = $n;
                        $bc_str = '';
                        for($j=1;$j<$level;$j++){
                            if( $bc_str != '' ) $bc_str.='.';
                            $bc_str.=$bc[$j];
                        }
                        if( $bc_str != '' ) $bc_str.='.';
                        $bc_str.=$n;

                        $last_blocco = $xml->name;
                        if( $blocco == 'milestone' ) $passed_milestone = true;

                        $indici_bc[]=$bc_str;
                    }
                }
                $xml->close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM