Attachment 'shape2svg.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                 extension-element-prefixes="exsl str"
   6                 xmlns:dia="http://www.daa.com.au/~james/dia-shape-ns"
   7                 xmlns="http://www.w3.org/2000/svg"
   8                 xmlns:svg="http://www.w3.org/2000/svg"
   9                 xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
  10                 exclude-result-prefixed="svg"
  11                 >
  12   <xsl:output method="xml" omit-xml-declaration="no" indent="yes" />
  13   <xsl:strip-space elements="*" />
  14 
  15   <xsl:template match="/">
  16     <xsl:apply-templates select="//svg|//svg:svg"/>
  17   </xsl:template>
  18   
  19   <xsl:template match="svg|svg:svg">
  20     <xsl:element name="{local-name()}" namespace="{namespace-uri()}">
  21       <xsl:apply-templates select="@*"/>
  22       <g inkscape:groupmode="layer" inkscape:label="shape" id="shape">
  23         <xsl:apply-templates select="node()"/>
  24         <!-- copy in text name if there is one -->
  25         <xsl:if test="../*[name()='textbox']">
  26           <xsl:variable name="x" select="(../*[name()='textbox']/@x2 + ../*[name()='textbox']/@x1 ) div 2"/>
  27           <xsl:variable name="y" select="(../*[name()='textbox']/@y2 + ../*[name()='textbox']/@y1 ) div 2"/>
  28           <text id="textbox" style="font-family:Bitstream Vera Sans;font-weight:normal;font-style:normal;font-stretch:normal;font-variant:normal;font-size:0.5px;text-anchor:start;text-align:start;writing-mode:lr;line-height:125%">
  29             <xsl:attribute name="x"><xsl:value-of select="$x"/></xsl:attribute>
  30             <xsl:attribute name="y"><xsl:value-of select="$y"/></xsl:attribute>
  31             <tspan style="font-family:Bitstream Vera Sans;font-weight:normal;font-style:normal;font-stretch:normal;font-variant:normal;font-size:0.5px;text-anchor:middle;text-align:center;writing-mode:lr;line-height:125%">
  32               <xsl:attribute name="x"><xsl:value-of select="$x"/></xsl:attribute>
  33               <xsl:attribute name="y"><xsl:value-of select="$y"/></xsl:attribute>
  34               <xsl:value-of select="../*[name()='name']"/>
  35             </tspan>
  36           </text>
  37         </xsl:if>
  38       </g>
  39 
  40       <!-- now import connection points -->
  41       <g inkscape:groupmode="layer" inkscape:label="connections" id="connections">
  42         <xsl:variable name="size" select="0.1"/>
  43         <xsl:for-each select="../*[name()='connections']/*[name()='point']">
  44           <!-- do tiny rectangles for each one, width width 1 fill no stroke -->
  45           <rect id="{concat('connection_',position())}" style="stroke: none; fill: #000000" width="{$size}" height="{$size}">
  46             <xsl:if test="@main">
  47               <xsl:attribute name="main"><xsl:value-of select="@main"/></xsl:attribute>
  48             </xsl:if>
  49             <xsl:attribute name="x"><xsl:value-of select="@x - ($size div 2)"/></xsl:attribute>
  50             <xsl:attribute name="y"><xsl:value-of select="@y - ($size div 2)"/></xsl:attribute>
  51           </rect>
  52         </xsl:for-each>
  53       </g>
  54     </xsl:element>
  55   </xsl:template>
  56   
  57   <!-- add fill:none -->
  58   <xsl:template match="svg:polyline[not(@style)]|polyline[not(@style)]">
  59     <xsl:attribute name="style">fill:none</xsl:attribute>
  60   </xsl:template>  
  61 
  62   <!-- formally express all the style assumptions that dia makes and inkscape doesn't -->
  63   <xsl:template name="style">
  64     <xsl:param name="style" select="."/>
  65 
  66     <!-- replace :foreground with black -->
  67     <xsl:for-each select="str:tokenize($style,';')">
  68       <!-- some shapes are slack and miss the ;, we can tell if they have more than one space after normalizing space -->
  69       <xsl:variable name="stylet" select="normalize-space(.)"/>
  70       
  71       <xsl:variable name="property" select="normalize-space(substring-before($stylet,':'))"/>
  72       <xsl:variable name="value" select="normalize-space(substring-after($stylet,':'))"/>
  73       <xsl:choose>
  74         <!-- for some reason divide stroke width by 10 for inkscape. I think it gets them wrong -->
  75         <xsl:when test="$property='fill' and $value='default'">
  76           <xsl:value-of select="$property"/><xsl:text>:none; </xsl:text>
  77         </xsl:when>
  78         <xsl:when test="$property='stroke-width'">
  79           <xsl:text>stroke-width: </xsl:text><xsl:value-of select="$value div 10"/><xsl:text>; </xsl:text>
  80         </xsl:when>
  81         <xsl:when test="$value='foreground'">
  82           <xsl:value-of select="$property"/><xsl:text>:#000000; </xsl:text>
  83         </xsl:when>
  84         <xsl:when test="$value='background'">
  85           <xsl:value-of select="$property"/><xsl:text>:#ffffff; </xsl:text>
  86         </xsl:when>
  87         <xsl:otherwise>
  88           <xsl:value-of select="."/><xsl:text>; </xsl:text>
  89         </xsl:otherwise>
  90       </xsl:choose>
  91       <!-- if there is no fill mentioned, set fill to none -->
  92     </xsl:for-each>
  93     <xsl:if test="not(substring-after($style,'fill:'))">fill:none;</xsl:if>
  94     <xsl:if test="not(substring-after($style,'stroke:'))">stroke:#000000;</xsl:if>
  95     <xsl:if test="not(substring-after($style,'stroke-width:'))">stroke-width:0.1;</xsl:if>
  96   </xsl:template>
  97   
  98   <xsl:template match="@style">
  99     <xsl:attribute name="{name()}">
 100       <xsl:call-template name="style"/>
 101     </xsl:attribute>  
 102   </xsl:template>  
 103   
 104   <xsl:template match="svg:path[not(@style)]|path[not(@style)]">
 105     <xsl:copy>
 106       <xsl:apply-templates select="@*"/>
 107       <xsl:attribute name="style">
 108         <xsl:call-template name="style">
 109           <xsl:with-param name="style" select="''"/>
 110         </xsl:call-template>
 111       </xsl:attribute>
 112       <xsl:apply-templates select="node()"/>
 113     </xsl:copy>
 114   </xsl:template>
 115 
 116   <xsl:template match="@*">
 117     <xsl:copy select="."/>
 118   </xsl:template>  
 119   
 120   <xsl:template match="node()">
 121     <xsl:copy>
 122       <xsl:apply-templates select="@*"/>
 123       <xsl:apply-templates select="node()"/>
 124     </xsl:copy>
 125   </xsl:template>
 126 </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.