Big-Blue
Big-Blue

Reputation: 429

Transforming SVG with XSLT (Sierpinski carpet)

I am currently failing to achieve a successful transformation of an SVG image into another one using XSLT. Somehow, the XSLT-document applied to the image doesn't recognize the rect-nodes that it's supposed to apply the template to, atleast that's what I think.

The input XML/SVG is simple:

<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg">
  <rect x="0" y="0" width="720" height="720" fill="white"/>
</svg>

The XSLT file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml"
 encoding="UTF-8"
 indent="yes"
 doctype-system="-//W3C//DTD SVG 20000303 Stylable//EN"
 doctype-public="http://www.w3.org/TR/2000/03/WD-SVG-20000303/DTD/svg-20000303-stylable.dtd"
 standalone="yes" />

<xsl:template match="/">
  <xsl:apply-templates select="svg/rect"/>
</xsl:template>

<xsl:template match="rect">
  <xsl:variable name="new-width" select="@width div 3"/>
  <xsl:variable name="new-height" select="@height div 3"/>

  <xsl:variable name="col1" select="@x"/>
  <xsl:variable name="col2" select="@x + $new-width"/>
  <xsl:variable name="col3" select="@x + (2 * $new-width)"/>

  <xsl:variable name="row1" select="@y"/>
  <xsl:variable name="row2" select="@y + $new-height"/>
  <xsl:variable name="row3" select="@y + (2 * $new-height)"/>

  <rect x="$col1" y="$row1" width="$new-width" height="$new-height" fill="white"/>
  <rect x="$col2" y="$row1" width="$new-width" height="$new-height" fill="white"/>
  <rect x="$col3" y="$row1" width="$new-width" height="$new-height" fill="white"/>

  <rect x="$col1" y="$row2" width="$new-width" height="$new-height" fill="white"/>
  <rect x="$col2" y="$row2" width="$new-width" height="$new-height" fill="black"/>
  <rect x="$col3" y="$row2" width="$new-width" height="$new-height" fill="white"/>

  <rect x="$col1" y="$row3" width="$new-width" height="$new-height" fill="white"/>
  <rect x="$col2" y="$row3" width="$new-width" height="$new-height" fill="white"/>
  <rect x="$col3" y="$row3" width="$new-width" height="$new-height" fill="white"/>
</xsl:template>

</xsl:stylesheet>

I've eliminated all sources of error I could've think of:

I even tried using a for-each loop to apply this template without apply-templates, but also had no success. Now I don't know what I could try anymore and thus ask you.

Upvotes: 2

Views: 1331

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86774

The XML file places the contents in a namespace, while your XSLT does not have that namespace declared.

  1. Add xmlns:svg="http://www.w3.org/2000/svg" to your stylesheet.
  2. Change <xsl:apply-templates select="svg:svg/svg:rect"/>
  3. Change <xsl:template match="svg:rect">

Upvotes: 3

Related Questions