Functions extends SchemaLocalizations { public String getXPathFrom(GPathResult path) { if (path == null) { return ""; } final GPathResult parent = path.parent(); if (parent == path) { return ""; } StringBuilder currPath = new StringBuilder(getXPathFrom(parent)); if (currPath.length() > 0) { currPath.append("/"); } currPath.append(path.name()); final GPathResult singlingsAndSelf = (GPathResult) parent.getProperty(path.name()); if (singlingsAndSelf.size() > 1) { final int i = singlingsAndSelf.list().indexOf(path); currPath.append('[').append(i + 1).append(']'); } return currPath.toString(); } Functions(FormatterParams fparams, Environment env); String html(Closure htmlFunction); String translate(String key); String translate(String key, String file); SchemaPlugin getSchemaPlugin(); String getXPathFrom(GPathResult path);  }
@Test public void testGetXPath() throws Exception { final URL resource = AbstractCoreIntegrationTest.class.getResource("kernel/valid-metadata.iso19139.xml"); final String sampleMetadataXml = Resources.toString(resource, Constants.CHARSET); final GPathResult xml = AbstractFormatterTest.parseXml(sampleMetadataXml); GPathResult polygon = null, bbox = null; final Iterator iterator = xml.depthFirst(); while (iterator.hasNext()) { Object elem = iterator.next(); if (elem instanceof GPathResult) { GPathResult result = (GPathResult) elem; if (result.name().equals("gmd:EX_BoundingPolygon")) { polygon = result; } if (result.name().equals("gmd:EX_GeographicBoundingBox")) { bbox = result; } } } assertEquals("gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent[2]/gmd:EX_Extent/gmd:geographicElement[2]/gmd:EX_GeographicBoundingBox", functions.getXPathFrom(bbox)); assertEquals("gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent[2]/gmd:EX_Extent/gmd:geographicElement[1]/gmd:EX_BoundingPolygon", functions.getXPathFrom(polygon)); }