从包含tei文件的字符串中,我生成一个索引以导航到它们的块,我检索所有div标记,我还想获得标记的内容(如果存在)(标记{{1} })当前div。
示例tei文件:
float
我试过这个但是不行:
<head>
该行不起作用:
<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>';
这一行的目的是在循环中获取子标记头的文本(在本例中为“LIBER I”)
答案 0 :(得分:0)
在加载上使用@
符号可以隐藏各种问题。因此,如果您将其删除,则会出现文档错误。
但是,如果您将行更改为
$dom->loadXML($fulltext);
输出为您提供了您的服务。
答案 1 :(得分:0)
使用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();