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>
任何想法爲什麼會發生?
哦,非常感謝你,我完全忘了屬性! – 2011-12-27 13:55:30