Reputation: 333
I have tried the following xml (SimpleSampleInput.xml), which refers to SimpleTransform1.xsl. I have used both Firefox and Xalan to transform the xml, rendering it as SVG. It seems that both give the same results. The explicit parts get output but the xsl:element parts don't. Also, the content of the <xsl:template name="metadata" match="sample">
shows up in Firebug but refuses to render. Confused!
At the moment I'm just trying to get to grips with the technology (xml + xsl -> svg) and so this example doesn't attempt all the output I'm aiming for but I'm taking small steps.
Happy to supply more details if the following paste isn't sufficient...
Thanks in advance:
Greg
(nb in my context, both files are in my Apache directory (/var/www/xmlxsl2svg/) and I have Xalan version 1.10.0 & Xerces version 2.8.0)
<?xml-stylesheet href="SimpleTransform1.xsl" type="text/xsl"?>
<sample frame="all">
<title>DO MYCOMMAND </title>
<groupseq>
<kwd>DO MYCOMMAND </kwd>
</groupseq>
<groupseq importance="optional">
<kwd>SWITCH1 </kwd>
<delim>( </delim>
<var>switch1 var </var>
<delim>) </delim>
<synnote>Switch1 description </synnote>
</groupseq>
<groupchoice importance="optional">
<kwd importance="default">SWITCH2(sw2var) </kwd>
<groupseq>
<kwd>PARM </kwd>
<delim>( </delim>
<var>member-name </var>
<delim>) </delim>
</groupseq>
</groupchoice>
</sample>
<?xml version="1.0" encoding="UTF-8"?>
<!--xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="metadata" match="sample">
<xsl:param name="synnote" select="groupseq"/>
<xsl:variable name="synnoteText" >
<xsl:element name = "text" >
<xsl:attribute name="x">50 </xsl:attribute>
<xsl:attribute name="y">100 </xsl:attribute>
<xsl:attribute name="id">xslGeneratedTextElement </xsl:attribute>
<xsl:attribute name="style">font-family:sans-serif; font-size:12; stroke:none; fill:black; </xsl:attribute>
xslGeneratedTextElement
<xsl:value-of select="$synnote" />
</xsl:element>
</xsl:variable>
<circle cx="90" cy="90" r="50" id="templateCircle" style="stroke:#255965; stroke-width:10px; stroke-dasharray:20,10; fill:red;"/>
<!--the following line is the only output when this template is called! -->
<text x="50" y="300" id="templateText" style="font-family:sans-serif; font-size:22; stroke:none; fill:green;">
<xsl:value-of select="$synnote" />
</text>
<!--the following line should output the xslGeneratedTextElement-->
<xsl:value-of select="$synnoteText" />
</xsl:template>
<xsl:template match="sample">
<svg width="1000" height="600" xmlns="http://www.w3.org/2000/svg">
<xsl:call-template name="metadata" />
<circle cx="300" cy="300" r="200" id="largeCircle" style="stroke:#255965; stroke-width:10px; stroke-dasharray:20,10; fill:pink;"/>
<text x="50" y="20" id="titleText" style="font-family:sans-serif; font-size:12; stroke:none; fill:black;">
<xsl:value-of select="title"/>
</text>
<!--xsl:param name="bbb" match="sample"/-->
<xsl:variable name="aaa" >
<xsl:element name = "text2" >
<xsl:attribute name="x">50 </xsl:attribute>
<xsl:attribute name="y">100 </xsl:attribute>
<xsl:attribute name="id">xslGeneratedTextElement2 </xsl:attribute>
<xsl:attribute name="style">font-family:sans-serif; font-size:12; stroke:none; fill:black; </xsl:attribute>
The title is: <xsl:value-of select="title" />
</xsl:element>
</xsl:variable>
<xsl:value-of select="$aaa" />
</svg>
</xsl:template>
</xsl:stylesheet>
Upvotes: 3
Views: 394
Reputation: 167541
In addition to the answers suggesting you to use xsl:copy-of
instead of xsl:value-of
, you also need to make sure you move xmlns="http://www.w3.org/2000/svg"
directly on the xsl:stylesheet
elements as that way it applies to literal result elements in all templates anywhere in your stylesheet and not as currently only to the svg
element and its descendants.
Upvotes: 0
Reputation: 243449
Replace this:
<xsl:variable name="aaa" >
<xsl:element name = "text2" >
<xsl:attribute name="x">50 </xsl:attribute>
<xsl:attribute name="y">100 </xsl:attribute>
<xsl:attribute name="id">xslGeneratedTextElement2 </xsl:attribute>
<xsl:attribute name="style">font-family:sans-serif; font-size:12; stroke:none; fill:black; </xsl:attribute>
The title is: <xsl:value-of select="title" />
</xsl:element>
</xsl:variable>
<xsl:value-of select="$aaa" />
with the much simpler and correct:
<text2 x="50" y="100" id="xslGeneratedTextElement2"
style="font-family:sans-serif; font-size:12; stroke:none; fill:black;">
The title is: <xsl:value-of select="title" />
</text2>
Remember:
It is meaningful to use xsl:element
only if the name and/or namespace-uri of the element are dynamically generated (non-fixed, calculated, coming from a variable).
<xsl:value-of select="someElement"/>
only outputs the string value of someElement
, not the element itself. The string value of an element as defined in the W3C XPath specification, is the concatenation of all of its text-node descendants. If you need to output the complete element, you can use the <xsl:copy-of select="someElement"/>
instruction.
Upvotes: 1
Reputation: 6956
There is no need to build the element in a variable prior to attempting to output it. Remove the lines relating to that, and it should work.
If you are building the element in this way in order to keep your XSLT DRY, I would recommend using a named template instead, which can be called with xsl:call-template
.
xsl:value-of
outputs the value of a node, in the case of an element, this is the text of the node. If you wish to copy xml into the result tree, you need to use xsl:copy
or xsl:copy-of
Upvotes: 1
Reputation: 70618
At the moment, you are creating the element in a variable, and using xsl:value-of to output it. However, xsl:value-of will only output the text value of the element, not the element itself. You need xsl:copy-of
<xsl:copy-of select="$synnoteText" />
Actually, you could do away with the variables altogether, and just move the xsl:element code to the place where you want the element to be created in the output XML.
Upvotes: 3