Expand compact xml representation to more verbose representation using xslt -
i'm trying learn xslt hobby, , 1 of use cases i'm interested in expansion of definitions of components complete forms. example should clarify mean. e.g. if input xml looks like:
<universe> <galaxies> <galaxy name="milky way"> <system name="solar system"/> </galaxy> <galaxy name="lactose free"> <system name="windmill system"/> </galaxy> <galaxy name="parallelish solar system"> <system name="the earth system"/> </galaxy> </galaxies> <systems> <system name="solar system"> <planet name="pluto"/> <system name="the earth system"/> <planet name="mercury"/> </system> <system name="windmill system"> <planet name="windy"/> </system> <system name="the earth system"> <planet name="earth"/> <satellite name="moon"/> </system> </systems> </universe>
my output xml have these system definitions expanded in galaxy definitions recursively (e.g. earth system in solar system) , in every single place definition occurs (e.g. earth system occurs in 2 different systems). systems section removed:
<universe> <galaxies> <galaxy name="milky way"> <system name="solar system"> <planet name="pluto"/> <system name="the earth system"> <planet name="earth"/> <satellite name="moon"/> </system> <planet name="mercury"/> </system> </galaxy> <galaxy name="lactose free"> <system name="windmill system"> <planet name="windy"/> </system> </galaxy> <galaxy name="parallelish solar system"> <system name="the earth system"> <planet name="earth"/> <satellite name="moon"/> </system> </galaxy> </galaxies> </universe>
i'm pretty new xslt, , of now, line of thought i've followed has been somehow define these component blocks variables , insert them appropriate in (second?) pass. however, i've googled around , read bit trying find examples, haven't been successful. i'm wondering if @ possible (i expect so), , concepts need use achieve this. example snippets helpful.
thanks!
xslt has useful keys mechanism resolving cross-references. demonstrate how work, have first modified xml example avoid circular references:
xml
<universe> <galaxies> <galaxy name="milky way"> <system-ref name="solar system"/> </galaxy> <galaxy name="lactose free"> <system-ref name="windmill system"/> </galaxy> <galaxy name="parallelish solar system"> <system-ref name="the earth system"/> </galaxy> </galaxies> <systems> <system name="solar system"> <planet name="pluto"/> <system-ref name="the earth system"/> <planet name="mercury"/> </system> <system name="windmill system"> <planet name="windy"/> </system> <system name="the earth system"> <planet name="earth"/> <satellite name="moon"/> </system> </systems> </universe>
xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="system-by-name" match="system" use="@name" /> <!-- identity transform --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="system-ref"> <xsl:apply-templates select="key('system-by-name', @name)"/> </xsl:template> <xsl:template match="systems"/> </xsl:stylesheet>
result
<?xml version="1.0" encoding="utf-8"?> <universe> <galaxies> <galaxy name="milky way"> <system name="solar system"> <planet name="pluto"/> <system name="the earth system"> <planet name="earth"/> <satellite name="moon"/> </system> <planet name="mercury"/> </system> </galaxy> <galaxy name="lactose free"> <system name="windmill system"> <planet name="windy"/> </system> </galaxy> <galaxy name="parallelish solar system"> <system name="the earth system"> <planet name="earth"/> <satellite name="moon"/> </system> </galaxy> </galaxies> </universe>
Comments
Post a Comment