I've got HTML object (generated by XML and XSL via XSLTProcessor) that comes with <a name=""> tags at all the major divisions. What I need to do is create a Table of Contents before I show the HTML.
I've got the major stuff working, in that I create an unordered list (which gets changed into a horizontal menu via CSS). Each item in the list is named from the
<a name="section-1"></a>
tags. This ends up giving me a list like:
<div id="nav">
<ul>
<li><a href="#section-1">Section-1</a></li>
<li><a href="#section-2">Section-2</a></li>
</ul>
</div>
What I want to end up with is something more like this:
<div id="nav">
<ul>
<li><a href="#section-1">1 INTRODUCTION</a></li>
<li><a href="#section-2">2 INSTALLATION</a></li>
</ul>
</div>
where the item names come from the
<h1>1 INTRODUCTION</h1>
tags that follow the
<a name="section-1"></a>
tags.
The HTML stream I'm working with looks like this:
<a name="section-5"></a><p></p>
<h1>2 INSTALLATION</h1>
I can't always guarantee the exact sequence of html nodes, but the <h1> node always follows closely behind the related <a name= ""> node. Sometimes there is the single <p></p> and sometimes a pair of breaks <br /><br />.
Here is my current code that creates an array of <a> nodes and then iterates through the ones with name="section-":
$proc = new XSLTProcessor();
$proc->importStylesheet($xsl);
$xml = new DOMDocument();
if (file_exists($xmlFile))
{
$xml->load($xmlFile);
$domTranObj = $proc->transformToDoc($xml);
$linkPairs = array();
$arrayAnodes = $domTranObj->getElementsByTagName('a');
if (count($arrayAnodes) > 0)
{
echo '<div id="nav"><ul>';
foreach ($arrayAnodes as $anode)
{
if ($nodeName = $anode->hasAttribute('name'))
{
if (($nodeNameVal = $anode->getAttribute('name')) &&
(strpos($nodeNameVal,'section') !== false) &&
(strpos($nodeNameVal,'.') === false)) /* I only want major divisions not any subdivsions*/
{
echo '<li><a href="#'.$nodeNameVal.'">'.ucwords($nodeNameVal).'</a></li>';
}
}
}
echo '</div>';
}
}
Can anyone help me to get the contents of the <h1> nodes that follow each <a name=""> nodes?
Thanks for any help you can offer!