2011-12-27 3 views
4

svgファイルにマージしようとしています。 、そして、2つの同一の行を交換すると、XSL変換によって異なる結果が得られます

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<svg 
     xmlns="http://www.w3.org/2000/svg" 
     version="1.1" 
     width="500" 
     height="500" 
     id="arrow" 
     xml:space="preserve"> 
    <g id="g10"> 
     <path d="M 250 245 L 250 255 L 400 255 L 400 265 L 415 250 L 400 235 L 400 245 L 250 245 " style="fill:red;stroke:#500;"></path> 
    </g> 
</svg>. 

私は、XSLテンプレートを次でそれをマージしようとしています:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<svg 
     xmlns="http://www.w3.org/2000/svg" 
     version="1.1" 
     width="500" 
     height="500" 
     xml:space="preserve"> 

    <g> 
     <circle cy="250px" cx="250px" r="200" style="fill:black"></circle> 
     <circle cy="250px" cx="250px" r="195" style="fill:white"></circle> 
    </g> 
</svg> 

と "arrow.svg":私は2つのファイル、 "bg.svg" を持っている

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:svg="http://www.w3.org/2000/svg"> 


    <xsl:variable name="bg-doc" select="document('bg.svg')"/> 

    <xsl:template match="/svg:svg"> 
     <xsl:copy> 
      <xsl:apply-templates select="./@*|./node()" /> 
      <xsl:apply-templates select="$bg-doc/svg:svg/svg:g" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 


</xsl:stylesheet> 

TransformerFactory factory = TransformerFactory.newInstance(); 
Transformer transformer = factory.newTransformer(new StreamSource(new File("merge.xsl"))); 
transformer.transform(new StreamSource(new File("arrow.svg")), 
          new StreamResult(new File("out.svg"))); 

このtransfo:このJavaのコードによって

...

<?xml version = '1.0' encoding = 'UTF-8'?> 
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="500" height="500" id="arrow" xml:space="preserve"> 
    <g id="g10"> 
     <path d="M 250 245 L 250 255 L 400 255 L 400 265 L 415 250 L 400 235 L 400 245 L 250 245 " 
       style="fill:red;stroke:#500;"/> 
    </g> 
    <g> 
     <circle cy="250px" cx="250px" r="200" style="fill:black"/> 
     <circle cy="250px" cx="250px" r="195" style="fill:white"/> 
    </g> 
</svg> 

しかし、私は、XSLテンプレートの11と12行の順序を変更しようとしています:rmationは正しい結果持って

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:svg="http://www.w3.org/2000/svg"> 


    <xsl:variable name="bg-doc" select="document('bg.svg')"/> 

    <xsl:template match="/svg:svg"> 
     <xsl:copy> 
      <xsl:apply-templates select="$bg-doc/svg:svg/svg:g" /> 
      <xsl:apply-templates select="./@*|./node()" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 


</xsl:stylesheet> 

を...変換は、奇妙な(非有効)XMLを返します。

<?xml version = '1.0' encoding = 'UTF-8'?> 
<svg xmlns="http://www.w3.org/2000/svg"> 
    <g> 
     <circle cy="250px" cx="250px" r="200" style="fill:black"/> 
     <circle cy="250px" cx="250px" r="195" style="fill:white"/> 
    </g> 
    <svg version="1.1" width="500" height="500" id="arrow" xml:space="preserve"> 
     <g id="g10"> 
      <path d="M 250 245 L 250 255 L 400 255 L 400 265 L 415 250 L 400 235 L 400 245 L 250 245 " 
        style="fill:red;stroke:#500;"/> 
     </g> 
    </svg> 

なぜ起こるのでしょうか?

答えて

2

しかし、私は、XSL テンプレートの11と12行の順序を変更しようとしています:変換は奇妙な(非有効)XML

を返し

<xsl:template match="/svg:svg"> 
    <xsl:copy> 
     <xsl:apply-templates select="$bg-doc/svg:svg/svg:g" /> 
     <xsl:apply-templates select="./@*|./node()" /> 
    </xsl:copy> 
</xsl:template> 

これは正確に最初にsvg:g要素がSVGドキュメントの1つからコピーされた場合、最上位要素の属性と残りのSVGドキュメント全体のコピーのみがコピーされます。

解決c:\temp\delete\

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<svg 
     xmlns="http://www.w3.org/2000/svg" 
     version="1.1" 
     width="500" 
     height="500" 
     id="arrow" 
     xml:space="preserve"> 
    <g id="g10"> 
     <path d="M 250 245 L 250 255 L 400 255 L 400 265 L 415 250 L 400 235 L 400 245 L 250 245 " style="fill:red;stroke:#500;"></path> 
    </g> 
</svg> 

と設けられた第二の文書bg.svg):この変換が提供されるXML文書(arrow.svg)に印加さ​​れる

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:svg="http://www.w3.org/2000/svg"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

    <xsl:variable name="bg-doc" select= 
    "document('file:///c:/temp/delete/bg.svg')"/> 

    <xsl:template match="/svg:svg"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*" /> 
      <xsl:apply-templates select="$bg-doc/svg:svg/svg:g" /> 
      <xsl:apply-templates select="node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="500" height="500" xml:space="preserve"> <g> <circle cy="250px" cx="250px" r="200" style="fill:black"></circle> <circle cy="250px" cx="250px" r="195" style="fill:white"></circle> </g> </svg> 

正しい結果は今が生成されます

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="500" height="500" id="arrow" xml:space="preserve"><g> 
     <circle cy="250px" cx="250px" r="200" style="fill:black"/> 
     <circle cy="250px" cx="250px" r="195" style="fill:white"/> 
    </g> 
    <g id="g10"> 
     <path d="M 250 245 L 250 255 L 400 255 L 400 265 L 415 250 L 400 235 L 400 245 L 250 245 " style="fill:red;stroke:#500;"/> 
    </g> 
</svg> 

説明:すぐにこの要素のxsl:copyの指示に従わなければならない要素の属性をコピーします。他の要素をコピーした後に配置すると、属性の元の所有者ではなく、最後にコピーされた要素にこれらの属性が配置されます。

+0

ありがとう、私は属性について完全に忘れました! –

関連する問題