diff options
Diffstat (limited to 'blocks/xcos2xml/head.xsl')
-rw-r--r-- | blocks/xcos2xml/head.xsl | 211 |
1 files changed, 99 insertions, 112 deletions
diff --git a/blocks/xcos2xml/head.xsl b/blocks/xcos2xml/head.xsl index 22fb992c..5e58e0c9 100644 --- a/blocks/xcos2xml/head.xsl +++ b/blocks/xcos2xml/head.xsl @@ -20,85 +20,70 @@ </xsl:choose> </xsl:template> - <xsl:template name="si-format"> - <xsl:param name="num" /> + <xsl:template name="eval-rational"> + <xsl:param name="expr"/> - <!-- Compute absolute value manually --> - <xsl:variable name="absNum"> - <xsl:choose> - <xsl:when test="$num < 0"><xsl:value-of select="-$num" /></xsl:when> - <xsl:otherwise><xsl:value-of select="$num" /></xsl:otherwise> - </xsl:choose> - </xsl:variable> + <xsl:choose> + <xsl:when test="contains($expr, '/')"> + <xsl:variable name="num" select="number(substring-before($expr, '/'))" /> + <xsl:variable name="den" select="number(substring-after($expr, '/'))" /> + <xsl:value-of select="$num div $den" /> + </xsl:when> + <xsl:otherwise> + <xsl:value-of select="number($expr)" /> + </xsl:otherwise> + </xsl:choose> + </xsl:template> - <!-- Compute exponent --> - <xsl:variable name="exponent"> - <xsl:choose> - <xsl:when test="$absNum = 0">0</xsl:when> - <xsl:otherwise> - <xsl:call-template name="compute-exp"> - <xsl:with-param name="n" select="$absNum" /> - <xsl:with-param name="exp" select="0" /> - </xsl:call-template> - </xsl:otherwise> - </xsl:choose> - </xsl:variable> + <xsl:template name="si-format"> + <xsl:param name="expr" /> - <!-- Define SI prefixes --> - <xsl:choose> - <xsl:when test="$exponent >= -2 and $exponent <= 0"> - <xsl:value-of select="round($num div 1E-3)" /> m - </xsl:when> - <xsl:when test="$exponent >= -5 and $exponent <= -3"> - <xsl:value-of select="round($num div 1E-6)" /> μ <!-- Unicode for μ --> - </xsl:when> - <xsl:when test="$exponent >= -8 and $exponent <= -6"> - <xsl:value-of select="round($num div 1E-9)" /> n - </xsl:when> - <xsl:when test="$exponent >= -11 and $exponent <= -9"> - <xsl:value-of select="round($num div 1E-12)" /> p - </xsl:when> - <xsl:when test="$exponent >= 1 and $exponent <= 3"> - <xsl:value-of select="round($num div 1)" /> - </xsl:when> - <xsl:when test="$exponent >= 4 and $exponent <= 6"> - <xsl:value-of select="round($num div 1E3)" /> k</xsl:when> - <xsl:when test="$exponent >= 7 and $exponent <= 9"> - <xsl:value-of select="round($num div 1E6)" /> M - </xsl:when> - <xsl:when test="$exponent >= 10 and $exponent <= 12"> - <xsl:value-of select="round($num div 1E9)" /> G - </xsl:when> - <xsl:when test="$exponent >= 13 and $exponent <= 15"> - <xsl:value-of select="round($num div 1E12)" /> T - </xsl:when> - <xsl:otherwise> - <xsl:value-of select="$num" /> 10^<xsl:value-of select="$exponent " /> - </xsl:otherwise> - </xsl:choose> - </xsl:template> - <!-- Recursive template to compute the exponent --> - <xsl:template name="compute-exp"> - <xsl:param name="n" /> - <xsl:param name="exp" /> + <xsl:variable name="num"> + <xsl:call-template name="eval-rational"> + <xsl:with-param name="expr" select="$expr" /> + </xsl:call-template> + </xsl:variable> - <xsl:choose> - <xsl:when test="$n < 1"> - <xsl:call-template name="compute-exp"> - <xsl:with-param name="n" select="$n * 10" /> - <xsl:with-param name="exp" select="$exp - 1" /> - </xsl:call-template> - </xsl:when> - <xsl:when test="$n >= 10"> - <xsl:call-template name="compute-exp"> - <xsl:with-param name="n" select="$n div 10" /> - <xsl:with-param name="exp" select="$exp + 1" /> - </xsl:call-template> - </xsl:when> - <xsl:otherwise> - <xsl:value-of select="$exp" /> - </xsl:otherwise> - </xsl:choose> + <xsl:choose> + <xsl:when test="number($num) >= 1000000000000"> + <xsl:value-of select="format-number($num div 1000000000000, '#.##')" /> + <xsl:text> T</xsl:text> + </xsl:when> + <xsl:when test="number($num) >= 1000000000"> + <xsl:value-of select="format-number($num div 1000000000, '#.##')" /> + <xsl:text> G</xsl:text> + </xsl:when> + <xsl:when test="number($num) >= 1000000"> + <xsl:value-of select="format-number($num div 1000000, '#.##')" /> + <xsl:text> M</xsl:text> + </xsl:when> + <xsl:when test="number($num) >= 1000"> + <xsl:value-of select="format-number($num div 1000, '#.##')" /> + <xsl:text> k</xsl:text> + </xsl:when> + <xsl:when test="number($num) >= 1"> + <xsl:value-of select="format-number($num, '#.##')" /> + </xsl:when> + <xsl:when test="number($num) >= 0.001"> + <xsl:value-of select="format-number($num div 0.001, '#.##')" /> + <xsl:text> m</xsl:text> + </xsl:when> + <xsl:when test="number($num) >= 0.000001"> + <xsl:value-of select="format-number($num div 0.000001, '#.##')" /> + <xsl:text> μ</xsl:text> + </xsl:when> + <xsl:when test="number($num) >= 0.000000001"> + <xsl:value-of select="format-number($num div 0.000000001, '#.##')" /> + <xsl:text> n</xsl:text> + </xsl:when> + <xsl:when test="number($num) >= 0.000000000001"> + <xsl:value-of select="format-number($num div 0.000000000001, '#.##')" /> + <xsl:text> p</xsl:text> + </xsl:when> + <xsl:otherwise> + <xsl:value-of select="format-number($num, '#.##')" /> + </xsl:otherwise> + </xsl:choose> </xsl:template> <xsl:output method="xml" indent="no" /> @@ -279,59 +264,61 @@ </xsl:template> <xsl:template name="generate-block"> + <xsl:param name="style"/> + <xsl:param name="id"/> <xsl:param name="explicitInputPorts"/> <xsl:param name="implicitInputPorts"/> <xsl:param name="explicitOutputPorts"/> <xsl:param name="implicitOutputPorts"/> <xsl:param name="controlPorts"/> <xsl:param name="commandPorts"/> - <xsl:param name="blockId"/> - <xsl:param name="style"/> <xsl:param name="simulationFunction"/> <xsl:param name="display_parameter"/> <xsl:param name="data"/> <xsl:element name="mxCell"> - <xsl:attribute name="style"><xsl:value-of select="$style"/></xsl:attribute> - <xsl:attribute name="id"><xsl:value-of select="$blockId"/></xsl:attribute> - <xsl:attribute name="vertex">1</xsl:attribute> - <xsl:attribute name="connectable">0</xsl:attribute> - <xsl:attribute name="CellType">Component</xsl:attribute> - <xsl:attribute name="blockprefix">XCOS</xsl:attribute> - <xsl:attribute name="explicitInputPorts"><xsl:value-of select="$explicitInputPorts"/></xsl:attribute> - <xsl:attribute name="implicitInputPorts"><xsl:value-of select="$implicitInputPorts"/></xsl:attribute> - <xsl:attribute name="explicitOutputPorts"><xsl:value-of select="$explicitOutputPorts"/></xsl:attribute> - <xsl:attribute name="implicitOutputPorts"><xsl:value-of select="$implicitOutputPorts"/></xsl:attribute> - <xsl:attribute name="controlPorts"><xsl:value-of select="$controlPorts"/></xsl:attribute> - <xsl:attribute name="commandPorts"><xsl:value-of select="$commandPorts"/></xsl:attribute> - <xsl:attribute name="simulationFunction"><xsl:value-of select="$simulationFunction"/></xsl:attribute> - <xsl:attribute name="sourceVertex">0</xsl:attribute> - <xsl:attribute name="targetVertex">0</xsl:attribute> - <xsl:attribute name="tarx">0</xsl:attribute> - <xsl:attribute name="tary">0</xsl:attribute> + <xsl:attribute name="style"><xsl:value-of select="$style"/></xsl:attribute> + <xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute> + <xsl:attribute name="vertex">1</xsl:attribute> + <xsl:attribute name="connectable">0</xsl:attribute> + <xsl:attribute name="CellType">Component</xsl:attribute> + <xsl:attribute name="blockprefix">XCOS</xsl:attribute> + <xsl:attribute name="explicitInputPorts"><xsl:value-of select="$explicitInputPorts"/></xsl:attribute> + <xsl:attribute name="implicitInputPorts"><xsl:value-of select="$implicitInputPorts"/></xsl:attribute> + <xsl:attribute name="explicitOutputPorts"><xsl:value-of select="$explicitOutputPorts"/></xsl:attribute> + <xsl:attribute name="implicitOutputPorts"><xsl:value-of select="$implicitOutputPorts"/></xsl:attribute> + <xsl:attribute name="controlPorts"><xsl:value-of select="$controlPorts"/></xsl:attribute> + <xsl:attribute name="commandPorts"><xsl:value-of select="$commandPorts"/></xsl:attribute> + <xsl:attribute name="simulationFunction"><xsl:value-of select="$simulationFunction"/></xsl:attribute> + <xsl:attribute name="sourceVertex">0</xsl:attribute> + <xsl:attribute name="targetVertex">0</xsl:attribute> + <xsl:attribute name="tarx">0</xsl:attribute> + <xsl:attribute name="tary">0</xsl:attribute> - <xsl:apply-templates select="mxGeometry"/> - <Object as="displayProperties"> + <xsl:apply-templates select="mxGeometry"/> + + <Object as="displayProperties"> <xsl:attribute name="display_parameter"> - <xsl:value-of select="$display_parameter"/> + <xsl:value-of select="$display_parameter"/> </xsl:attribute> - </Object> - <Object as="parameter_values"> - <xsl:for-each select="$data"> - <xsl:attribute name="{concat('p', format-number(position() - 1, '000'), '_value')}"> - <xsl:value-of select="@value" /> - </xsl:attribute> - </xsl:for-each> - </Object> + </Object> + + <Object as="parameter_values"> + <xsl:for-each select="$data"> + <xsl:attribute name="{concat('p', format-number(position() - 1, '000'), '_value')}"> + <xsl:value-of select="@value" /> + </xsl:attribute> + </xsl:for-each> + </Object> </xsl:element> <xsl:call-template name="port"> - <xsl:with-param name="id" select="$blockId"/> - <xsl:with-param name="explicitInputPorts" select="$explicitInputPorts"/> - <xsl:with-param name="explicitOutputPorts" select="$explicitOutputPorts"/> - <xsl:with-param name="implicitInputPorts" select="$implicitInputPorts"/> - <xsl:with-param name="implicitOutputPorts" select="$implicitOutputPorts"/> - <xsl:with-param name="controlPorts" select="$controlPorts"/> - <xsl:with-param name="commandPorts" select="$commandPorts"/> + <xsl:with-param name="id" select="$id"/> + <xsl:with-param name="explicitInputPorts" select="$explicitInputPorts"/> + <xsl:with-param name="explicitOutputPorts" select="$explicitOutputPorts"/> + <xsl:with-param name="implicitInputPorts" select="$implicitInputPorts"/> + <xsl:with-param name="implicitOutputPorts" select="$implicitOutputPorts"/> + <xsl:with-param name="controlPorts" select="$controlPorts"/> + <xsl:with-param name="commandPorts" select="$commandPorts"/> </xsl:call-template> </xsl:template> |