Skip to content

Instantly share code, notes, and snippets.

@helderdarocha
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helderdarocha/cb8c330c321f0265288e to your computer and use it in GitHub Desktop.
Save helderdarocha/cb8c330c321f0265288e to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<!-- Prints the root node and selects all descendant nodes -->
<xsl:template match="/">
<xsl:text>/</xsl:text>
<xsl:apply-templates select="descendant::node()" mode="print-path"/>
</xsl:template>
<!-- Prints a new line and the root document element -->
<xsl:template match="*[not(parent::*)]" mode="print-path">
<xsl:text>&#xa;/</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>[1]</xsl:text>
</xsl:template>
<!-- Prints any element that has a parent element -->
<xsl:template match="*[parent::*]" mode="print-path">
<xsl:apply-templates select="parent::*" mode="print-path"/>
<xsl:variable name="name" select="name()"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="$name"/>
<xsl:text>[</xsl:text>
<xsl:value-of select="count(preceding-sibling::*[name()=$name])+1" />
<xsl:text>]</xsl:text>
</xsl:template>
<!-- Prints a text node -->
<xsl:template match="text()[parent::node()]" mode="print-path">
<xsl:apply-templates select="parent::*" mode="print-path"/>
<xsl:text>/text()[</xsl:text>
<xsl:value-of select="count(preceding-sibling::text())+1" />
<xsl:text>] - "</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"</xsl:text>
</xsl:template>
<!-- Prints a comment -->
<xsl:template match="comment()[parent::node()]" mode="print-path">
<xsl:apply-templates select="parent::*" mode="print-path"/>
<xsl:text>/comment()[</xsl:text>
<xsl:value-of select="count(preceding-sibling::comment())+1" />
<xsl:text>]</xsl:text>
</xsl:template>
<!-- Prints a processing instruction -->
<xsl:template match="processing-instruction()[parent::node()]" mode="print-path">
<xsl:apply-templates select="parent::*" mode="print-path"/>
<xsl:text>/processing-instruction(</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>)[</xsl:text>
<xsl:value-of select="count(preceding-sibling::processing-instruction())+1" />
<xsl:text>]</xsl:text>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment