특정노드의 XPath 경로를 가져오는 함수



/* 설정한 최초조상부터 $node까지의 XPath 반환
 *
 * @param object $node                           XPath를 구하고자 하는 노드
 * @param string  $firstAncestorNodeName XPath를 구하고자 하는 시작노드. 빈칸이면 최상위부터
 *
 * @return string XPath 경로
*/

function getXPath($node, $firstAncestorNodeName)
{
    $XPath = $node->nodeName;
    if ($XPath==$firstAncestorNodeName) return null;

    // 부모노드가 존재할 경우 재귀호출
    if (is_object($node->parentNode)==true) {
        $XPath = getXPath($node->parentNode, $firstAncestorNodeName)."/".$XPath;
    }

    return $XPath;
}





Posted by bloodguy
,