1
업데이트 :이 질문은 텍스트를 강조 표시하고 해당 강조 표시를 데이터베이스에 저장하여 나중에로드 할 수 있도록하기위한 노력의 일환입니다.자바를 사용하여 상대 Xpath를 얻는 방법
여기에 사용 된 코드는 다음과 같습니다. Range object with JSON 사용자가 선택한 텍스트의 위치를 캡처하여 데이터베이스에 저장 한 다음 아약스 호출로 복원하려고합니다.
먼저, 나는 그 모습 점점 오전 XPath를 만들고 싶어 같은이
endXPath: "/HTML[1]/BODY[1]/DIV[1]/DIV[5]/P[3]/text()[1]"
처럼 보이는이
endXPath: "/DIV[5]/P[3]/text()[1]"
DIV [1] 첫 번째 예에서의 ID가
"함유량."
나는이 경로가 나는 내가 contextNode을 변경해야 추측하고이 기능을 내가 지금까지 읽은에서function makeXPath (node, currentPath) {
/* this should suffice in HTML documents for selectable nodes, XML with namespaces needs more code */
currentPath = currentPath || '';
switch (node.nodeType) {
case 3:
case 4:
return makeXPath(node.parentNode, 'text()[' + (document.evaluate('preceding-sibling::text()', node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength + 1) + ']');
case 1:
return makeXPath(node.parentNode, node.nodeName + '[' + (document.evaluate('preceding-sibling::' + node.nodeName, node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength + 1) + ']' + (currentPath ? '/' + currentPath : ''));
case 9:
return '/' + currentPath;
default:
return '';
}
}
에서 오는 생각? 하지만 그 중 하나를 달성하는 방법을 잘 모르겠습니다.