使用 XSLT(谢尔宾斯基地毯)转换 SVG [英] Transforming SVG with XSLT (Sierpinski carpet)

查看:20
本文介绍了使用 XSLT(谢尔宾斯基地毯)转换 SVG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前无法使用 XSLT 将 SVG 图像成功转换为另一个图像.不知何故,应用于图像的 XSLT 文档无法识别它应该将模板应用于的矩形节点,至少我是这么认为的.

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.

输入的 XML/SVG 很简单:

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>

XSLT 文件:

<?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:

  • 内联数学(-> 变量)
  • apply-templates 中的节点选择错误
  • 错误的输出方法

我什至尝试使用 for-each 循环在没有 apply-templates 的情况下应用此模板,但也没有成功.现在我不知道我还能尝试什么,所以问你.

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.

推荐答案

XML 文件将内容放置在命名空间中,而您的 XSLT 没有声明该命名空间.

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

  1. xmlns:svg="http://www.w3.org/2000/svg" 添加到您的样式表.
  2. 更改
  3. 更改
  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">

这篇关于使用 XSLT(谢尔宾斯基地毯)转换 SVG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆