Skip to content

Instantly share code, notes, and snippets.

@vicmortelmans
Created January 28, 2011 15:04
Show Gist options
  • Save vicmortelmans/800354 to your computer and use it in GitHub Desktop.
Save vicmortelmans/800354 to your computer and use it in GitHub Desktop.
templates for flattening a purely hierarchical XML structure
<!-- templates for flattening a purely hierarchical XML structure
into a list of <item> records -->
<xsl:template match="*" mode="flatten">
<xsl:apply-templates mode="flatten"/>
</xsl:template>
<xsl:template match="*[not(*)]" mode="flatten">
<!-- working upwards starting from leaf nodes -->
<item>
<xsl:apply-templates select="." mode="flatten-ancestor"/>
</item>
</xsl:template>
<xsl:template match="*" mode="flatten-ancestor">
<xsl:copy>
<xsl:copy-of select="@*"/>
</xsl:copy>
<xsl:apply-templates select="ancestor::*[1]" mode="flatten-ancestor"/>
</xsl:template>
@mykelangelo
Copy link

Cool, thanks! It's not what I needed, though... but I have already found what I was looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment