Modifying SVG file with XSLT
I'm fairly new to XSLT and have only used it to do some basic XML-to-HTML conversions. Currently, I'm trying to create a parts manual from an XML bill of material using XSLT. During the process I need to create a copy of an SVG file and make some modifications to the output. However, I can't seem to get my XSLT to copy or output the contents of the source SVG.
Here is a snippet of the XML code I'm trying to parse:
<Assembly>
<Item HPN="1234567" Rev="0" Desc="Assembly" Dwg="7654321" DwgRev="8">
</Assembly>
In my XSLT, I have the following code to read the file name for the SVG and make a copy of it:
<xsl:template match="Assembly/Item">
<xsl:variable name="Dwg"><xsl:value-of select="@Dwg"/></xsl:variable>
<xsl:variable name="DwgRev"><xsl:value-of select="@DwgRev"/></xsl:variable>
<xsl:if test="not(@Dwg = preceding::Item/@Dwg)">
<xsl:call-template name="SVGConversion">
<xsl:with-param name="Dwg" select="$Dwg"/>
<xsl:with-param name="DwgRev" select="$DwgRev"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
The SVGConversion template is saved in a separate XSLT style sheet that has been imported into the main style sheet. This may not have been necessary, but I was concerned about the output method.
<xsl:output method="xml" indent="no" media-type="image/svg"/>
<xsl:template name="SVGConversion">
<xsl:param name="Dwg"/>
<xsl:param name="DwgRev"/>
<xsl:variable name="SVGCall">document('Drawings/<xsl:value-of select="$Dwg"/>_&l开发者_Go百科t;xsl:value-of select="$DwgRev"/>.svg')</xsl:variable>
<xsl:result-document encoding="UTF-8" indent="yes" href="{$ProjectPath}/App/Graphics/{$Dwg}_{$DwgRev}.svg">
<xsl:apply-templates select="$SVGCall"/>
</xsl:result-document>
</xsl:template>
This code produces an SVG file with the correct file name in the output location, but it's empty. How do I copy the code from the original SVG into the new SVG file, keeping in mind that I also want to make modifications to parts of the SVG code during processing?
I've tried using and as well to grab the content of the SVG file, but got the same results.
It may also help to know that I am using the Kernow 1.6.1 parser. Not sure if that is part of the problem.
Any advice anyone can offer would be appreciated.
I think instead of <xsl:variable name="SVGCall">document('Drawings/<xsl:value-of select="$Dwg"/>_<xsl:value-of select="$DwgRev"/>.svg')</xsl:variable>
you simply want <xsl:variable name="SVGCall" select="document(concat('Drawing/', $Dwg, '_', $DwgRev, '.svg'))"/>
.
And while you are at it, change stuff like <xsl:variable name="Dwg"><xsl:value-of select="@Dwg"/></xsl:variable>
to <xsl:variable name="Dwg" select="@Dwg"/>
.
I believe you should add an XSLT identity transformation rule, which will copy all nodes by default:
http://en.wikipedia.org/wiki/Identity_transform#Using_XSLT
Without such a rule, XML nodes that are not matched by a rule will simply be ignored.
精彩评论