Skip to content

Instantly share code, notes, and snippets.

@sardinecan
Created November 7, 2023 18:19
Show Gist options
  • Save sardinecan/0d7bec841cff8612162c84d7d05ee316 to your computer and use it in GitHub Desktop.
Save sardinecan/0d7bec841cff8612162c84d7d05ee316 to your computer and use it in GitHub Desktop.
XPointer
<?xml-stylesheet href="xsltforms/xsltforms.xsl" type="text/xsl"?>
<?xsltforms-options debug="no"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events">
<head>
<title>Mouse Events in XForms</title>
<style>
span {
color: orangered;
}
</style>
<model xmlns="http://www.w3.org/2002/xforms" id="index">
<instance>
<data xmlns="">
<xpath></xpath>
</data>
</instance>
<xf:action ev:event="getStartXpath">
<xf:setvalue ref="xpath" value="event('response')"/>
</xf:action>
</model>
<script type="text/javascript">
<![CDATA[
document.onclick = function(event) {
if (event===undefined) event = window.event;
var target = 'target' in event? event.target : event.srcElement;
var path= getXPath(target);
XsltForms_xmlevents.dispatch(document.getElementById("index"), "getStartXpath", null, null, null, null, {
response: path
});
}
function getXPath(element) {
if (element.id!=='')
return "//*[@id='"+element.id+"']";
if (element===document.body)
return element.tagName.toLowerCase();
var ix = 0;
var siblings = element.parentNode.childNodes;
for (var i= 0; i<siblings.length; i++) {
var sibling = siblings[i];
if (sibling===element) return getXPath(element.parentNode) + '/' + element.tagName.toLowerCase() + '[' + (ix + 1) + ']';
if (sibling.nodeType===1 && sibling.tagName === element.tagName) {
ix++;
}
}
}
]]>
</script>
</head>
<body>
<div id="transcription">
<h1>Cliquez sur une portion de texte ci-dessous pour obtenir son XPath</h1>
<p id="p1">Lorem ipsum dolor sit amet, <span>consectetur</span> adipisicing elit. <span id="s1">Asperiores</span> cumque dignissimos dolore et, facilis iure iusto laborum neque, officiis quas quis velit? Dolor iure libero non optio repudiandae. Error, nulla.</p>
<!-- ne fonctionne pas avec le contenu mixte… -->
<p>Lorem ipsum dolor sit amet, <span>consectetur</span> adipisicing elit. <span id="s2">Asperiores</span> cumque dignissimos dolore et, facilis iure iusto laborum neque, officiis quas quis velit? Dolor iure libero non optio repudiandae. Error, nulla.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li><span>Item3</span></li>
</ul>
</div>
<group xmlns="http://www.w3.org/2002/xforms">
XPath: <output ref="xpath"/>
</group>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment