Attachment 'svg2shape.xslt'

Download

   1 <?xml version="1.0" encoding="ISO-8859-1"?>
   2 <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   3                 xmlns:str="http://exslt.org/strings"
   4                 xmlns:exsl="http://exslt.org/common"
   5 		xmlns:math="http://exslt.org/math"
   6                 extension-element-prefixes="exsl str"
   7                 xmlns:dia="http://www.daa.com.au/~james/dia-shape-ns"
   8 		xmlns:svg="http://www.w3.org/2000/svg"
   9                 xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
  10                 xmlns="http://www.daa.com.au/~james/dia-shape-ns"
  11                 >
  12   <xsl:output method="xml" omit-xml-declaration="no" indent="yes" />
  13   <xsl:strip-space elements="*" />
  14   <xsl:param name="icon-file"/>
  15 
  16   <xsl:template match="/">
  17     <shape xmlns="http://www.daa.com.au/~james/dia-shape-ns" xmlns:svg="http://www.w3.org/2000/svg">
  18       <xsl:variable name="textbox" select="//*[name()='text' and @id='textbox']"/>
  19       <xsl:variable name="_name" select="$textbox/*[name()='tspan']"/>
  20       
  21       <name><xsl:value-of select="$_name"/></name>
  22       <xsl:if test="string-length($icon-file)">
  23         <icon><xsl:value-of select="$icon-file"/></icon>
  24       </xsl:if>
  25       
  26       <connections>
  27         <xsl:for-each select="//*[name()='g' and @id='connections']/*[starts-with(name(),'connection_')]|//*[starts-with(@id,'connection_')]">
  28           <xsl:call-template name="connection-point"/>
  29         </xsl:for-each>
  30       </connections>
  31       <xsl:if test="$textbox">
  32         <textbox align="center" resize="no">
  33           <xsl:attribute name="x1"><xsl:value-of select="$textbox/@x - 1"/></xsl:attribute>
  34           <xsl:attribute name="y1"><xsl:value-of select="$textbox/@y - 1"/></xsl:attribute>
  35           <xsl:attribute name="x2"><xsl:value-of select="$textbox/@x + 1"/></xsl:attribute>
  36           <xsl:attribute name="y2"><xsl:value-of select="$textbox/@y + 1"/></xsl:attribute>
  37         </textbox>
  38       </xsl:if>
  39       <aspectratio type="fixed"/>
  40       <xsl:apply-templates select="svg|svg:svg"/>
  41     </shape>
  42   </xsl:template>
  43 
  44   <xsl:template name="minx">
  45     <xsl:param name="draw" select="."/>
  46     <xsl:variable name="x">
  47       <xsl:call-template name="d">
  48         <xsl:with-param name="draw" select="$draw"/>
  49         <xsl:with-param name="index" select="1"/>
  50       </xsl:call-template>
  51     </xsl:variable>
  52     <xsl:value-of select="math:min(str:tokenize(normalize-space($x)))"/>
  53   </xsl:template>
  54 
  55   <xsl:template name="maxx">
  56     <xsl:param name="draw" select="."/>
  57     <xsl:variable name="x">
  58       <xsl:call-template name="d">
  59         <xsl:with-param name="draw" select="$draw"/>
  60         <xsl:with-param name="index" select="1"/>
  61       </xsl:call-template>
  62     </xsl:variable>
  63     <xsl:value-of select="math:max(str:tokenize(normalize-space($x)))"/>
  64   </xsl:template>
  65 
  66   <xsl:template name="miny">
  67     <xsl:param name="draw" select="."/>
  68     <xsl:variable name="y">
  69       <xsl:call-template name="d">
  70         <xsl:with-param name="draw" select="$draw"/>
  71         <xsl:with-param name="index" select="2"/>
  72       </xsl:call-template>
  73     </xsl:variable>
  74     <xsl:value-of select="math:min(str:tokenize(normalize-space($y)))"/>
  75   </xsl:template>
  76 
  77   <xsl:template name="maxy">
  78     <xsl:param name="draw" select="."/>
  79     <xsl:variable name="y">
  80       <xsl:call-template name="d">
  81         <xsl:with-param name="draw" select="$draw"/>
  82         <xsl:with-param name="index" select="2"/>
  83       </xsl:call-template>
  84     </xsl:variable>
  85     <xsl:value-of select="math:max(str:tokenize(normalize-space($y)))"/>
  86   </xsl:template>
  87 
  88   <xsl:template name="d">
  89     <xsl:param name="index" select="1"/>
  90     <xsl:param name="draw" select="."/>
  91   
  92     <xsl:for-each select="str:tokenize($draw)">
  93       <!-- interest in those that have a , in -->
  94       <xsl:if test="contains(.,',')">
  95         <xsl:value-of select="normalize-space(str:tokenize(.,',')[$index])"/><xsl:text>&#32;</xsl:text>
  96       </xsl:if>
  97     </xsl:for-each>
  98   </xsl:template>
  99 
 100   <!-- output a point tag which is the avg(minx+maxx),avg(miny,maxy) -->
 101   <xsl:template name="connection-point">
 102     <xsl:variable name="x1">
 103       <xsl:choose>
 104         <xsl:when test="@x"><xsl:value-of select="@x"/></xsl:when>
 105         <xsl:when test="@x1"><xsl:value-of select="@x1"/></xsl:when>
 106         <xsl:otherwise>
 107           <xsl:call-template name="minx">
 108             <xsl:with-param name="draw" select="@d"/>
 109           </xsl:call-template>
 110         </xsl:otherwise>
 111       </xsl:choose>
 112     </xsl:variable>
 113 
 114     <xsl:variable name="y1">
 115       <xsl:choose>
 116         <xsl:when test="@y"><xsl:value-of select="@y"/></xsl:when>
 117         <xsl:when test="@y1"><xsl:value-of select="@y1"/></xsl:when>
 118         <xsl:otherwise>
 119           <xsl:call-template name="miny">
 120             <xsl:with-param name="draw" select="@d"/>
 121           </xsl:call-template>
 122         </xsl:otherwise>
 123       </xsl:choose>
 124     </xsl:variable>
 125 
 126     <xsl:variable name="x2">
 127       <xsl:choose>
 128         <xsl:when test="@x2"><xsl:value-of select="@x2"/></xsl:when>
 129         <xsl:when test="@width"><xsl:value-of select="$x1 + @width"/></xsl:when>
 130         <xsl:otherwise>
 131           <xsl:call-template name="maxx">
 132             <xsl:with-param name="draw" select="@d"/>
 133           </xsl:call-template>
 134         </xsl:otherwise>
 135       </xsl:choose>
 136     </xsl:variable>
 137 
 138     <xsl:variable name="y2">
 139       <xsl:choose>
 140         <xsl:when test="@y2"><xsl:value-of select="@y2"/></xsl:when>
 141         <xsl:when test="@height"><xsl:value-of select="$y1 + @height"/></xsl:when>
 142         <xsl:otherwise>
 143           <xsl:call-template name="maxy">
 144             <xsl:with-param name="draw" select="@d"/>
 145           </xsl:call-template>
 146         </xsl:otherwise>
 147       </xsl:choose>
 148     </xsl:variable>
 149 
 150     <xsl:if test="string-length($x1) and string-length($x2) and string-length($y1) and string-length($y2)">
 151       <point>
 152         <xsl:attribute name="x"><xsl:value-of select="( $x1 + $x2 ) div 2"/></xsl:attribute>
 153         <xsl:attribute name="y"><xsl:value-of select="( $y1 + $y2 ) div 2"/></xsl:attribute>
 154         <xsl:if test="@main">
 155           <xsl:attribute name="main"><xsl:value-of select="@main"/></xsl:attribute>
 156         </xsl:if>
 157       </point>
 158     </xsl:if>
 159   </xsl:template>
 160 
 161   <!-- ignore defs -->
 162   <xsl:template match="svg/defs|svg:svg/svg:defs"/>
 163   
 164   <!-- if our twin generated the svg, it will have put the shape in a shape group, so remove that group -->
 165   <xsl:template match="svg/g[@id='shape']">
 166     <xsl:apply-templates select="*"/>
 167   </xsl:template>
 168 
 169   <!-- if our twin generated the svg, it will have put the connections in the connections group which we convert seperately -->
 170   <xsl:template match="svg/g[@id='connections']"/>
 171 
 172   <xsl:template match="*[name()='text' and @id='textbox']"/>
 173 
 174   <xsl:template match="svg">
 175     <xsl:element name="svg:svg" namespace="http://www.w3.org/2000/svg">
 176       <xsl:apply-templates select="@*"/>
 177       <xsl:apply-templates select="node()"/>
 178     </xsl:element>
 179   </xsl:template>
 180 
 181 
 182   <xsl:template match="@*">
 183     <xsl:copy select="."/>
 184   </xsl:template>  
 185 
 186   <xsl:template match="*">
 187     <xsl:element name="{concat('svg:',local-name())}">
 188       <xsl:apply-templates select="@*"/>
 189       <xsl:apply-templates select="node()"/>
 190     </xsl:element>
 191   </xsl:template>
 192 </xsl:transform>

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2021-02-25 09:42:56, 428.6 KB) [[attachment:48.shape]]
  • [get | view] (2021-02-25 09:42:56, 0.3 KB) [[attachment:genshape]]
  • [get | view] (2021-02-25 09:42:56, 5.9 KB) [[attachment:shape2svg.xslt]]
  • [get | view] (2021-02-25 09:42:56, 7.1 KB) [[attachment:svg2shape.xslt]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.