2012-03-03 18 views
3

1-n Path要素を持つsvgドキュメントがいくつかあります。これらのパス要素の色を変更したかったのです。XSLTを使用してタグに属性を追加する

私が行うための方法を発見していないしている。この

のSVG文書例:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<svg xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/2000/svg" xml:space="preserve" height="45" width="45" version="1.1" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/"> 
<g transform="matrix(1.25,0,0,-1.25,0,45)"> 
<path d="m9 18h18v-3h-18v3"/> 
</g> 
</svg> 

XSLT:私はそれを追加するために変更する必要が何

<?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' version='1.0'> 
<xsl:template match='path'> 
<xsl:copy> 
<xsl:attribute name='fill'>red</xsl:attribute> 
</xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 

/塗りつぶし属性を赤に変更しますか?

答えて

3

XSLTの動作を誤解していると思います。入力XMLツリーを受け取り、スタイルシートを解釈して新しいツリーを生成します。つまり、スタイルシートは、入力XMLツリーに基づいて、完全に新しいツリーが最初からどのように生成されるかを定義します。

元のXMLツリーを変更していないことを理解することが重要です。これは純粋に機能的な言語と命令的な言語の違いのようです。結論:の属性に変更できない場合は、属性が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 an existing fill attribute --> 
    <xsl:template match="svg:path/@fill"> 
     <!-- produce a fill attribute with content "red" --> 
     <xsl:attribute name="fill">red</xsl:attribute> 
    </xsl:template> 

    <!-- this template is applied to a path node that doesn't have a fill attribute --> 
    <xsl:template match="svg:path[not(@fill)]"> 
     <!-- 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="fill">red</xsl:attribute> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 
+0

ありがとう! – Peter

関連する問題