XSLT:向SVG图像的最后给定元素添加属性

时间:2019-02-08 07:19:33

标签: xslt

如何将属性stroke="red"添加到circle标记的最后一次出现?

我的SVG示例如下:

<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-900 -900 1800 1800" width="1800" height="1800">
<circle r="20" stroke="#343434" stroke-width="10"/>
<g stroke="#77868b" stroke-width="8" fill="none">
 <circle id="o4" r="600"/>
 <circle id="o5" r="700"/>
 <circle id="o6" r="800" />
 <g stroke-width="60" stroke-linecap="round" transform="rotate(90)">
  <circle id="e4" r="600" stroke-dasharray="1,116.75"/>
  <circle id="e5" r="700" stroke-dasharray="1,365.5"/>
  <circle id="e6" r="800" stroke-dasharray="1,2512.25"/>
 </g>
</g>
</svg>

当我使用以下XSLT模板时,我得到三个带有stroke="red"的圆圈,而不是最后一个。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:svg="http://www.w3.org/2000/svg" version='1.0'>
    <!-- this template is applied by default to all nodes and attributes -->
    <xsl:template match="@*|node()">
        <!-- just copy all my attributes and child nodes, except if there's a better template for some of them -->
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- this template is applied to a path node that doesn't have a fill attribute -->
    <xsl:template match="svg:circle[not(@stroke)][last()]">
        <!-- copy me and my attributes and my subnodes, applying templates as necessary, and add a fill attribute set to red -->
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:attribute name="stroke">red</xsl:attribute>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

g/添加到<xsl:template match="svg:g/circle[not(@stroke)][last()]">这样的模板中解决了我的问题。