解析 tei domxpath 在评估循环中获取文本子标记

parse tei domxpath get text child tag inside evaluate loop

从包含 tei 文件的字符串中,我生成一个索引以导航到它们的块,我检索所有 div 标签,我还想获取标签的内容(如果存在)(当前 div.

内的标签 <head>)

示例 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>

我试过了,但没用:

 //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>';

线路不通:

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

返回错误:

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

这一行的目的是获取循环内子标签头的文本(在本例中"LIBER I")

在负载上使用@符号可以隐藏各种问题。所以如果你把它拿出来,你的文件就会出错。

如果您将行更改为

$dom->loadXML($fulltext);

输出给你你想要的。

使用 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();