Family
Family

Reputation: 1113

How can I send an x and y value to SVG text via XSLT?

I have a simple XSL file:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
<xsl:param name='x' />
<xsl:param name='y' />
<xsl:template match='/Sensor'>
  <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
    <x><xsl:value-of select="x"/></x>
    <y><xsl:value-of select="y"/></y>
    <text x="x" y="y" font-family="sans-serif" font-size="24px" fill="black">TEST</text>
  </svg>
 </xsl:template>
</xsl:stylesheet>

And I am using this code:

var xx = 20;
var yy = 30;
var argsList = new XsltArgumentList();
argsList.AddParam("x", "", xx);
argsList.AddParam("y", "", yy);
var xelement = ConvertToXml(sensor);
var transformedElement = new XElement("Sensor");
using (var writer = transformedElement.CreateWriter())
{
 var xslt = new XslCompiledTransform(true);
 xslt.Load(transformFile);
 xslt.Transform(xelement.CreateReader(), argsList, writer);
}
return transformedElement;

My final output HTML displayed in the browser is:

<Root>
 <svg version="1.1" xmlns="http://www.w3.org/2000/svg">
  <x></x>
  <y></y>
  <text x="x" y="y" font-family="sans-serif" font-size="24px" fill="black">TEST</text>
 </svg>
</Root>

I'm trying to get the value of xx into the value of the SVG text x, and the value of yy into the value of the SVG text y. I.e. for the HTML to be:

<text x="20" y="30" font-family="sans-serif" font-size="24px" fill="black">TEST</text>

I have tried several variations on this but can't get it to work. What am I doing wrong?

Upvotes: 0

Views: 289

Answers (2)

kstubs
kstubs

Reputation: 808

Try:

<x><xsl:value-of select="$x"/></x>
<y><xsl:value-of select="$y"/></y>

Prefixing your parameter name with a $

Upvotes: 1

jvverde
jvverde

Reputation: 631

To refer to params you must prefix them with a '$'

<x><xsl:value-of select="$x"/></x>
<y><xsl:value-of select="$y"/></y>
<text x="{$x}" y="{$y}" font-family="sans-serif" font-size="24px" fill="black">TEST</text>

And notice also the curly brackets {} to get value-of inside atributes

Upvotes: 1

Related Questions