2011-12-27 82 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

相关问题