xslt - XSL Transformation to split a single node into two nodes in the same level -
i have source xml
<cars> <car> <make>fiat</make> <colors> <color>red</color> <color>blue</color> </colors> </car> <car> <make>volvo</make> <colors> <color>red</color> <color>white</color> </colors> </car> <car> <make>renault</make> <colors> <color>blue</color> <color>black</color> </colors> </car> </cars>
which want transform like
<cars> <detail> <name>makename</name> <entry>fiat</entry> <entry>volvo</entry> <entry>renault</entry> </detail> <detail> <name>availablecolors</name> <entry>red</entry> <entry>blue</entry> <entry>white</entry> <entry>black</entry> </detail> <cars>
i new xsl, , created 1 half of processing, stuck getting of colors separate elements in target
<xsl:template match="/"> <cars> <xsl:apply-templates /> </cars> </xsl:template> <xsl:template match="cars"> <xsl:apply-templates select="car" /> </xsl:template> <xsl:template match="car"> <detail> <name>makename</name> <xsl:apply-templates select="make" /> </detail> </xsl:template> <xsl:template match="make"> <entry><xsl:value-of select"text()"/></entry> </xsl:template>
i not able create xsl <name>availablecolors</name>
, quite new xsl , appreciated
here xslt 1.0 stylesheet shows how eliminate duplicate colors using muenchian grouping:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output indent="yes"/> <xsl:key name="k1" match="car/colors/color" use="."/> <xsl:template match="cars"> <xsl:copy> <detail> <name>makename</name> <xsl:apply-templates select="car/make"/> </detail> <detail> <name>availablecolors</name> <xsl:apply-templates select="car/colors/color[generate-id() = generate-id(key('k1', .)[1])]"/> </detail> </xsl:copy> </xsl:template> <xsl:template match="car/make | colors/color"> <entry> <xsl:value-of select="."/> </entry> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment