svg - XSLT sort issue - attribute wont sort -
source
<roll> <dayquantum date="20130125"> <item index="2" value="4" product="margherita"/> <item index="3" value="2" product="capricciosa"/> <item index="4" value="2" product="quattro stagioni"/> <item index="5" value="7" product="bresola"/> <item index="6" value="1" product="gorgonzola"/> <item index="7" value="7" product="piccante"/> <item index="8" value="3" product="rosmarino"/> <item index="9" value="2" product="caprese"/> <item index="10" value="7" product="parma"/> <item index="11" value="1" product="parmigiana"/> <item index="12" value="2" product="pollo"/> <item index="13" value="2" product="hawaii"/> <item index="14" value="17" product="pepperoni"/> <item index="15" value="4" product="calzone"/> <item index="16" value="2" product="bologna"/> <item index="17" value="3" product="tonno"/> <item index="18" value="1" product="marinara"/> <item index="19" value="2" product="napoletana"/> <item index="20" value="1" product="carne"/> <item index="21" value="1" product="mascarpone"/> <item index="22" value="4" product="carpaccio"/> <item index="25" value="1" product="tartufo"/> <item index="26" value="8" product="prosciutto"/> <item index="27" value="3" product="lasagna originale"/> <item index="28" value="1" product="tortellini gorgonzola"/> <item index="29" value="1" product="tortellini tartufo"/> <item index="31" value="4" product="tagliatelle dolce vita"/> <item index="33" value="1" product="spaghetti carbonara"/> <item index="37" value="2" product="antipasta toto e pepino"/> <item index="38" value="1" product="vitello tonnato"/> <item index="41" value="4" product="bruschetta classica"/> <item index="44" value="1" product="tiramisu"/> <item index="47" value="4" product="panino al pollo"/> <item index="48" value="5" product="panino al prosciutto"/> <item index="49" value="8" product="panino al vitello tonnato"/> </dayquantum> </roll>
xslt
<svg viewbox="0 0 2400 1400" style="background: #000 ; font-family: 'racing sans one'" id="zcanvas" version="1.1" xsl:version="1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns="http://www.w3.org/2000/svg"> <defs><link xmlns="http://www.w3.org/1999/xhtml" href="http://fonts.googleapis.com/css?family=racing+sans+one|six+caps" type="text/css" rel="stylesheet" /></defs> <xsl:for-each select="roll/dayquantum"> <xsl:sort select="@date" order="descending" data-type="number"/> <xsl:variable name="y" select="(position() * 180) - 100" /> <text fill="#fff" font-size="48"> <xsl:attribute name="x"><xsl:value-of select="80" /></xsl:attribute> <xsl:attribute name="y"><xsl:value-of select="$y - 40" /></xsl:attribute> <xsl:attribute name="transform">rotate(90, 80, <xsl:value-of select="$y - 40 " />)</xsl:attribute> <xsl:value-of select="substring(@date,7,2)" /><xsl:value-of select="substring(' janfebmaraprmayjunjulaugsepoctnovdec', number(substring(@date,5,2)) * 3, 3)" /> </text> <text fill="#ff6000" font-size="48"> <xsl:attribute name="x"><xsl:value-of select="120" /></xsl:attribute> <xsl:attribute name="y"><xsl:value-of select="$y - 10" /></xsl:attribute> <xsl:value-of select="sum(item/@value)" /> </text> <xsl:for-each select="item"> <xsl:sort select="@value" order="descending" data-type="number"/> <rect fill="green" > <xsl:attribute name="x"><xsl:value-of select="200 + (sum(preceding-sibling::item/@value) * 16)" /></xsl:attribute> <xsl:attribute name="y"><xsl:value-of select="$y - 48" /></xsl:attribute> <xsl:attribute name="width"><xsl:value-of select="@value * 16" /></xsl:attribute> <xsl:attribute name="rx">10</xsl:attribute> <xsl:attribute name="height">48</xsl:attribute> </rect> <g font-family="sans-serif"> <text fill="#fff" font-size="20" text-anchor="middle"> <xsl:attribute name="x"><xsl:value-of select="200 + (sum(preceding-sibling::item/@value) * 16) + ((@value * 16) div 2)" /></xsl:attribute> <xsl:attribute name="y"><xsl:value-of select="$y - 20" /></xsl:attribute> <xsl:value-of select="@value" /> </text> <text fill="#888" font-size="18" text-anchor="start"> <xsl:attribute name="x"><xsl:value-of select="200 + (sum(preceding-sibling::item/@value) * 16) + ((@value * 16) div 2)" /></xsl:attribute> <xsl:attribute name="y"><xsl:value-of select="$y" /></xsl:attribute> <xsl:attribute name="transform">rotate(90, <xsl:value-of select="200 + (sum(preceding-sibling::item/@value) * 16) + ((@value * 16) div 2)" />, <xsl:value-of select="$y" />)</xsl:attribute> <xsl:value-of select="@product" /> </text> </g> </xsl:for-each> </xsl:for-each> </svg>
problem:
it won't sort on attribute "value" descending - because of preceeding statement ?
source can found here: http://xmlsoap.dk/xml/crcountlog.xml
the reason it's not working way expect preceding-sibling::
looks @ item
s' preceding siblings in document order, not current sort order. beingness sorted, x
values beingness computed though had not sorted them @ all.
the next recursive approach should accomplish trying do:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0" xmlns="http://www.w3.org/2000/svg"> <xsl:output method="xml" omit-xml-declaration="yes" /> <xsl:template match="/"> <svg viewbox="0 0 2400 1400" style="background: #000 ; font-family: 'racing sans one'" id="zcanvas" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink"> <defs> <link xmlns="http://www.w3.org/1999/xhtml" href="http://fonts.googleapis.com/css?family=racing+sans+one|six+caps" type="text/css" rel="stylesheet" /> </defs> <xsl:apply-templates select="roll/dayquantum"> <xsl:sort select="@date" order="descending" data-type="number"/> </xsl:apply-templates> </svg> </xsl:template> <xsl:template match="dayquantum"> <xsl:variable name="y" select="(position() * 180) - 100" /> <text fill="#fff" font-size="48" x="80" y="{$y - 40}" transform="rotate(90, 80, {$y - 40})"> <xsl:value-of select="substring(@date,7,2)" /> <xsl:value-of select="substring(' janfebmaraprmayjunjulaugsepoctnovdec', number(substring(@date,5,2)) * 3, 3)" /> </text> <text fill="#ff6000" font-size="48" x="120" y="{$y - 10}"> <xsl:value-of select="sum(item/@value)" /> </text> <xsl:variable name="topitem" select="item[not(../item/@value > @value)][1]" /> <xsl:apply-templates select="$topitem"> <xsl:with-param name="y" select="$y" /> <xsl:with-param name="remainingitems" select="item[generate-id() != generate-id($topitem)]" /> </xsl:apply-templates> </xsl:template> <xsl:template match="item"> <xsl:param name="y" /> <xsl:param name="previousitems" select="/.." /> <xsl:param name="remainingitems" /> <xsl:variable name="leadingspace" select="200 + sum($previousitems/@value) * 16" /> <xsl:variable name="width" select="@value * 16" /> <xsl:variable name="hcenter" select="$leadingspace + $width div 2" /> <rect fill="green" x="{$leadingspace}" y="{$y - 48}" width="{$width}" rx="10" height="48" /> <g font-family="sans-serif"> <text fill="#fff" font-size="20" text-anchor="middle" x="{$hcenter}" y="{$y - 20}"> <xsl:value-of select="@value" /> </text> <text fill="#888" font-size="18" text-anchor="start" x="{$hcenter}" y="{$y}" transform="rotate(90, {$hcenter}, {$y})"> <xsl:value-of select="@product" /> </text> </g> <xsl:variable name="topitem" select="$remainingitems[not($remainingitems/@value > @value)][1]" /> <xsl:apply-templates select="$topitem"> <xsl:with-param name="y" select="$y" /> <xsl:with-param name="previousitems" select="$previousitems | ." /> <xsl:with-param name="remainingitems" select="$remainingitems[generate-id() != generate-id($topitem)]" /> </xsl:apply-templates> </xsl:template> </xsl:stylesheet>
this approach selects first item no items less itself, , applies templates that, passing remaining items parameters. next top item selected , template recursively calls itself, repeatedly until of items have been used up.
xslt svg
No comments:
Post a Comment