473,624 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1743
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:styleshe et 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="substri ng($x,1,1)"/>
<xsl:variable name="cdr" select="substri ng($x,2)"/>
<xsl:choose>
<xsl:when test="document( '')//ops:op[@name=$car]">
<xsl:apply-templates
select="documen t('')//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:otherwis e>
<xsl:value-of select="$l"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="@*">
<xsl:attribut e 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
1823
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 space attribute in the root of the output, or generating other attributes in the root? The following illustrates the case. The XSLT script specifies an output root element
9
1834
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 owner element. Is that right ? Like:
3
9908
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 ========================= <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0"
6
2578
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 schema for it with Visual Studio, I get the error "Failed to create a schema for this data file because:
1
1264
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 accomplish: 1) Provide a list of elements/attributes to be stripped (i.e. everything else should be passed through) or those that should be passed through (i.e. everything else should be stripped) which would be applied recursively. 2) If an element...
6
1892
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> <Elements> <Element att1="1" att2="2" att3="3" //Some attributes are
5
2617
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"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
5
3904
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. See refname and shortname attributes are added to node automatically after transformation. Please let me know how to fix this. ...
12
18284
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 existing node with the new one. For example first xml file will be like; <Root> <Node Id ="1" FormId="form1" Value="value1"/> <Node Id ="2" FormId="form1" Value="value2"/> <Node Id ="3" FormId="form1" Value="value3"/>...
0
8246
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.