473,323 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Calculate attributes with XSLT

Hello,
I want to calculate the value of an attribute. For example <rect x="2+3"
y="12"and be <rect x="5" y="12">. Is it possible using XSLT?

Thanks!
Oct 31 '06 #1
3 1729
Carles Company Soler wrote:
I want to calculate the value of an attribute. For example <rect x="2+3"
y="12"and be <rect x="5" y="12">. Is it possible using XSLT?
<rect x="{2 + 3}" y="12">...</rect>
The attribute value is interpreted as an attribute value template where
{} contains an XPath expression, in that example simply one adding two
numbers given as literals but of course you can have more complex XPath
expressions as needed to compute the attribute value.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 31 '06 #2
Carles Company Soler wrote:
I want to calculate the value of an attribute. For example <rect x="2+3"
y="12"and be <rect x="5" y="12">. Is it possible using XSLT?
Yes, but.

XSLT can certainly perform math, since the XPath language can do so. So
if the expression appears in the stylesheet, this is trivial.

But XSLT doesn't normally have the ability to interpret expressions
found in the input document rather than the stylesheet. Since that seems
to be what you want, you'll have to do some more work -- essentially,
you'll have to use XSLT as a programming language to implement an
expression interpreter, doing the string operations to break up the
expression into values and operators and executing the appropriate
operations.

This has certainly been done, and I suspect a websearch will dig up some
examples. It isn't difficult if you have any experience with
recursive-descent parsing, it's just a nuisance.

Alternatively, some XSLT processors support the "dynamic" section of the
EXSLT extension function library. This permits executing XPath
expressions found in arbitrary string data. It's overkill for your
stated needs, and of course relying on an extension function makes your
stylesheet less portable, but it is another alternative. (Or you could
plug in your own custom extension function, but obviously that's another
step away from portability.)

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Oct 31 '06 #3

Carles Company Soler wrote:
I want to calculate the value of an attribute. For
example <rect x="2+3" y="12"and be <rect x="5" y="12">.
Is it possible using XSLT?
It sure is, BUT. It's really not what XSLT is for: you'd
have to write your own expression evaluator, and XSLT is
ill-suited for that. If you need that type of processing,
you'd better use something else, or wait for XSLT 2.0 to
become Recommendation -- I believe it's much easier to do
something like that with XSLT2. EXSLT is probably an
option, too, but that's something I'd try to avoid if at
all possible.

Just for the heck of it, simple arithmetics-only:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ops="http://www.example.org/Operators">
<ops:ops>
<ops:op name="+"/><ops:op name="-"/>
<ops:op name="*"/><ops:op name="/"/>
</ops:ops>
<xsl:output method="xml"/>
<xsl:template match="ops:op[@name='+']" mode="eval">
<xsl:param name="l"/><xsl:param name="r"/>
<xsl:value-of select="$l+$r"/>
</xsl:template>
<xsl:template match="ops:op[@name='-']" mode="eval">
<xsl:param name="l"/><xsl:param name="r"/>
<xsl:value-of select="$l - $r"/>
</xsl:template>
<xsl:template match="ops:op[@name='*']" mode="eval">
<xsl:param name="l"/><xsl:param name="r"/>
<xsl:value-of select="$l*$r"/>
</xsl:template>
<xsl:template match="ops:op[@name='/']" mode="eval">
<xsl:param name="l"/><xsl:param name="r"/>
<xsl:value-of select="$l div $r"/>
</xsl:template>
<xsl:template name="eval">
<xsl:param name="l"/>
<xsl:param name="x"/>
<xsl:variable name="car" select="substring($x,1,1)"/>
<xsl:variable name="cdr" select="substring($x,2)"/>
<xsl:choose>
<xsl:when test="document('')//ops:op[@name=$car]">
<xsl:apply-templates
select="document('')//ops:op[@name=$car]"
mode="eval">
<xsl:with-param name="l" select="$l"/>
<xsl:with-param name="r">
<xsl:call-template name="eval">
<xsl:with-param name="l" select="''"/>
<xsl:with-param name="x" select="$cdr"/>
</xsl:call-template>
</xsl:with-param>
</xsl:apply-templates>
</xsl:when>
<xsl:when test="$car">
<xsl:call-template name="eval">
<xsl:with-param name="l"
select="concat($l,$car)"/>
<xsl:with-param name="x"
select="$cdr"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$l"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{name()}">
<xsl:call-template name="eval">
<xsl:with-param name="l" select="''"/>
<xsl:with-param name="x" select="."/>
</xsl:call-template>
</xsl:attribute>
</xsl:template>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Note that it's totally unaware of precedence. (It actually
calculates the right-most expression first. D'oh.) It can
recurse itself unto death really fast, too. And I don't
even mention precision problems it seems to be suffering
from (well, I suppose that's processor-dependent).

--
Pavel Lepin

Oct 31 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Wolfgang | last post by:
XSLT transformations by default seem to pass name space attributes into the root element of their output (example below). QUESTION: Is it possible to control this, i.e. not genrating a name...
9
by: Soren Kuula | last post by:
Hi, I just can't find namespaces of attributes stated clearly enough in the XML namespace spec. But .. I hear rumors that attributes, unless qualified otherwise, default to the namespace of the...
3
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ...
6
by: Martin | last post by:
Hi, I have a xml file like the one below <?xml version="1.0" encoding="utf-8"?><e1 xmlns:e1="http://tempuri.org/Source1.xsd" e1:att1="1" e1:att2="2" e1:rest="345"/> If I try to create a...
1
by: Foxpointe | last post by:
Given some arbitrary XHTML, I'd like to obtain a 'simplified' XHTML result which strips out a large subset of standard elements and attributes - but not all. The main things I would like to...
6
by: Jakub.Bednarczuk | last post by:
Hallo everybody I have the problem with getting attributes values and also attributes names. I am reading an xml file with DOM. Lets see an example: file I read <root> <Def></Def>...
5
by: asciz | last post by:
Hi I'm having a problem with an XML file, most likely because of my lack of understanding of XML schemas I have the following XML file: <?xml version="1.0" encoding="ISO-8859-1"?>...
5
by: mahesh.nimbalkar | last post by:
When I transform XML, XSLT automatically adds extra attributes to the node which are declared in DTD (default DTD attributes) . I just want XSLT not to add these extra default attributes from DTD....
12
by: blackirish | last post by:
Hi all, I am trying to merge 2 XML files that first of all i need to compare nodes of both files according to 2 attributes in the nodes. If those 3 attributes are equal, i need to replace the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.