Re: return value of a function as attribute value?

Hi Felix,

> I want to have the position of an svg element calculated by the
> rendering application. E.g. consider the following document
> <svg id="svg-root" width="100%" height="100%" viewBox="0 0 80 60">
>    <g transform="translate(10,30)">
>      <text x="0" y="0">one</text>
>      <text x="number(../text[1]/@textLength())" y="0">two</text>
>    </g>
> </svg>
> Now my questions are:
> A) Is the above document a legal SVG document? I.e. does SVG 1.1 allow
> functions (which are defined by XPath) to be evaluated to yield the
> actual value of an attribute or do all attribute values have to be 
> given
> as explicit, literal numbers?

SVG 1.1 does not allow for XPath evaluation to be evaluated within an 
attribute value nor anywhere else in the document.

> B) If this is not a legal document, is there some way to accomplish 
> what
> I want? I.e. is there some way to determine the length of one text
> element and have the position of another text element depend on it?

You can indeed do it through scripting. You're going to want to look at 
the SVGTextContentElement interface within the SVG DOM.

In there there is a method to get the length of a string using the 
getComputedTextLength() method. You can also obtain substring length 
and so on. I have written an article on doing simple text wrapping with 
SVG 1.0 and DOM scripting on, there is some code explained here 
that uses the getComputedTextLength() method:

Hope you can get some help from there.

Antoine Quint <>
SVG Consultant & Research Engineer, Fuchsia Design
W3C SVG Working Group Invited Expert
Journal -

Received on Sunday, 16 February 2003 11:53:23 UTC