1
我想用matplotlib生成一些svg图像并将它们嵌入到html和pdf报告中。我在WebKit
中遇到this bug,这是我的应用程序使用的,以及Adobe Acrobat中的另一个渲染错误。这两个问题都是由matplotlib
将<clipPath>
元素放置在svg文件的末尾造成的。使用XSLT重新排列/合并SVG文件的节点
若要两种错误我想在文件的开始使用XSLT所有<defs>
节点合并成一个节点周围
例SVG
<svg height="288pt" version="1.1" viewBox="0 0 576 288" width="576pt" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<style type="text/css">
*{stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:100000;}
</style>
</defs>
<g id="figure_1">
...
</g>
<defs>
<clipPath id="p8596dc1504">
<rect height="230.5245" width="211.84" x="339.7475" y="28.90175"/>
</clipPath>
<clipPath id="p9534ff1f4d">
<rect height="230.5245" width="211.84" x="66.534375" y="28.90175"/>
</clipPath>
</defs>
</svg>
所需的输出
<svg height="288pt" version="1.1" viewBox="0 0 576 288" width="576pt" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<style type="text/css">
*{stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:100000;}
</style>
<clipPath id="p8596dc1504">
<rect height="230.5245" width="211.84" x="339.7475" y="28.90175"/>
</clipPath>
<clipPath id="p9534ff1f4d">
<rect height="230.5245" width="211.84" x="66.534375" y="28.90175"/>
</clipPath>
</defs>
<g id="figure_1">
...
</g>
</svg>
这是我目前的XSLT。该代码正确地删除了<defs>
部分,但我在文档开始处获得的所有内容都是<defs xmlns=""/>
,没有孩子。
XSL文件
<?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" indent="yes" encoding="utf-8" omit-xml-declaration="yes" doctype-system="about:legacy-compat"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<defs>
<xsl:copy-of select="/svg/defs/*"/>
</defs>
<xsl:apply-templates select="*[name()!='defs']"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出继电器
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs xmlns=""/>
<g id="figure_1">
...
</g>
</svg>
我仍然试图环绕XSLT我的头。我究竟做错了什么?
完美,这我想要做什么!在[SO回答](http://stackoverflow.com/a/18515711/3419537)中的评论我基于我提到的命名空间上的代码,但我不知道如何将它们考虑在内 – user3419537
@Pfafait好点。这不是原来的代码,而是倾向于关注主要问题并忽略其他问题。现在添加。 –