XSLT: Full path of a node

While writing a Munin plugin for the house's heatpump system, I needed to transform an XML file into something plain text that could be grepped easily.

XSLT is the premier solution to convert XML, so I used it. To avoid disambiguities, I needed to be able to grep for the full XML node path. Unfortunately, XSLT does not provide a native way to obtain the path.

By calling a template that prints the current element name and calls itself for its own parent element, I made it work:

Example XML

<?xml version="1.0">

Path-generating XSLT

<?xml version="1.0"?>
<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
 <template match="/PCOWEB/PCO/*/VARIABLE">
  <call-template name="path"/>
  <value-of select="INDEX"/>
  <text> </text>
  <value-of select="VALUE"/>
 <template name="path">
  <for-each select="parent::*">
   <call-template name="path"/>
  <value-of select="name()"/>


Written by Christian Weiske.

Comments? Please send an e-mail.