Created
January 28, 2011 15:04
-
-
Save vicmortelmans/800354 to your computer and use it in GitHub Desktop.
templates for flattening a purely hierarchical XML structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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> |
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
Input:
Output:
using this complet stylesheet:
So what it does: it's taking each bottom-most element in the hiearchy and giving it it's own
<item>
, containing all the parent values, thus breaking down the hierarchy.