我正在尝试使用 XSL-T 处理充满 SVG 文件的目录。我目前已经尝试过:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" doctype-public="-//W3C//DTD SVG 1.1//EN http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"/>
<xsl:strip-space elements="*"/>
<xsl:template name="main">
<svg xmlns="http://www.w3.org/2000/svg">
<defs/>
<xsl:apply-templates select="collection(concat(resolve-uri('src/images'),'?select=*.svg;content-type=application/xml'))"/>
</svg>
</xsl:template>
<xsl:template match="/">
<xsl:message>root</xsl:message>
<xsl:apply-templates select="svg"/>
</xsl:template>
<xsl:template match="svg">
<xsl:message>svg</xsl:message>
<xsl:attribute name="id"><xsl:value-of select="document-uri(/)"/></xsl:attribute>
<xsl:attribute name="viewBox"><xsl:value-of select="@viewBox"/></xsl:attribute>
<xsl:apply-templates select="path"/>
</xsl:template>
<xsl:template match="path">
<xsl:message>path</xsl:message>
<xsl:attribute name="d"><xsl:value-of select="@d"/></xsl:attribute>
<xsl:apply-templates select="@*"/>
</xsl:template>
<xsl:template match="@fill">
<xsl:message>@fill</xsl:message>
<xsl:attribute name="fill"><xsl:value-of select="."/></xsl:attribute>
</xsl:template>
</xsl:stylesheet>
从命令行使用它:
java net.sf.saxon.Transform -it:main -xsl:"%XSL%\svg-sprite.xsl" -o:"%OUT%\sprite.svg"
但是,当我在包含 29 个 SVG 文件的目录上运行它时,我得到的只是打印出 29 个
root
。由于某种原因,<xsl:apply-templates select="svg"/>
似乎不起作用。
有谁知道如何解决这个困境,或者至少知道如何弄清楚为什么它没有应用定义的
svg
模板?
在 XSLT 的根元素上添加
xpath-default-namespace="http://www.w3.org/1999/svg"
。基本上是 SVG 命名空间,我希望我已经记住并输入了正确的内容。