2011-12-27 61 views
4

我想在一個合併到svg文件。我有兩個文件, 「bg.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」:

<?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"?> 
<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> 

...此Java代碼:

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息有正確的結果:

<?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> 

但是,當我試圖改變11條12線XSL模板的順序:

<?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

但是,當我試圖改變11條12線XSL 模板的順序:

<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> 

轉型返回奇怪的(非有效)的XML

這正是你已經指定了兩行代碼的交換:首先從其中一個SVG文檔複製一個svg:g元素,然後只複製頂層元素的屬性和整個剩餘的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> 

當這種轉化是在arrow.svg)所提供的XML文檔應用:在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 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

哦,非常感謝你,我完全忘了屬性! – 2011-12-27 13:55:30

相關問題