473,499 Members | 1,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Subtracting the values in attributes in an XSLT

How can I do simple subtraction in an XSLT.

I want to read a few attribute values from an XML document, calculate their
difference, and transform that value to an attribute in the XML output
document.

My original XML is,

<Dates>
<Birth Year="1966" />
<Current Year="2006" />
</Dates>
and the output that I want is simply,

<Age YearsOld="40"/>
but I can't get it to work. I have tried a number of ways to write it, along
the lines of,

<xsl:template match="Dates">
<Age>
<xsl:attribute name="YearsOld"/>
<xsl:value-of select="/Dates/Current/@Year -
/Dates/Birth/@Year"/>
</xsl:attribute>
</Age>
</xsl:template>
But I can never get a value for the YearsOld attribute. I am not sure if I
should be using tags, variables, curly brackets, or something else. I found
examples online that made it seem pretty easy when the values were not
attributes, just the tag contents, but I prefer keeping data in attributes.
Jan 14 '06 #1
4 8907


David S. Alexander wrote:

<Dates>
<Birth Year="1966" />
<Current Year="2006" />
</Dates>
and the output that I want is simply,

<Age YearsOld="40"/>


<xsl:template match="Dates">
<Age YearsOld="{Current/@Year - Birth/@Year}" />
</xsl:template>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jan 14 '06 #2
Martin,

I still cannot get my calculation to work correctly. My actual XML is,

<?xml version="1.0" encoding="utf-8" ?>
<Parameters xmlns="http://www.tempuri.org/Parameters">
<Client>
<Name First="Pat" Last="Jones" />
<DateOfBirth Month="5" Day="6" Year="1987" />
</Client>
<AnalysisDate Month="1" Day="16" Year="2006" />
</CalculatorParameters>

I am transforming XML to XML. What I want to transform my data to is,

<?xml version="1.0" encoding="utf-8" ?>
<Parameters xmlns="http://www.tempuri.org/CalculatorParameters">
<Client>
<Name First="Pat" Last="Jones" />
<DateOfBirth Age="19"/>
</Client>
<AnalysisDate Month="1" Day="16" Year="2006" />
</CalculatorParameters>

I tried using an absolute path in my XSLT,

<xsl:template match="DateOfBirth">
<DateOfBirth>
<xsl:attribute name="Age">
<xsl:value-of select="/Parameters/Client/DateOfBirth/@Year -
@Year"/>
</xsl:attribute>
</ClientDateOfBirth>
</xsl:template>

and also a relative one, <xsl:value-of select="../../AnalysisDate/@Year -
@Year"/> but both times, my Age attribute comes out empty, not 19. There
must be something about XPaths that I am missing. I have looked at the
documentation that I have, but it still isn't clear. I hope that I am just
not being dense.

Thanks,

David

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:Oa**************@tk2msftngp13.phx.gbl...


David S. Alexander wrote:

<Dates>
<Birth Year="1966" />
<Current Year="2006" />
</Dates>
and the output that I want is simply,

<Age YearsOld="40"/>


<xsl:template match="Dates">
<Age YearsOld="{Current/@Year - Birth/@Year}" />
</xsl:template>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jan 16 '06 #3


David S. Alexander wrote:

I still cannot get my calculation to work correctly. My actual XML is,

<?xml version="1.0" encoding="utf-8" ?>
<Parameters xmlns="http://www.tempuri.org/Parameters">


Namespaces are quite important, in your earlier post you also posted
your "actual" XML but that did not show any namespaces.
With that default namespace declaration all elements in the example are
in that namespace and XSLT templates need to cater for that so you need e.g.

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://www.tempuri.org/Parameters"
version="1.0">

<xsl:template match="ns1:*">
<xsl:element
name="{local-name()}"
namespace="http://www.tempuri.org/CalculatorParameters">
<xsl:apply-templates select="@* | node() "/>
</xsl:element>
</xsl:template>

<xsl:template match="ns1:DateOfBirth">
<xsl:element name="{local-name()}"
namespace="http://www.tempuri.org/CalculatorParameters">
<xsl:attribute name="Age">
<xsl:value-of
select="../following-sibling::ns1:AnalysisDate/@Year - @Year" />
</xsl:attribute>
</xsl:element>
</xsl:template>

</xsl:stylesheet>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jan 17 '06 #4
Martin,

Thank you for taking the time to type up that code for me. It really helped
me to make my design decision. I am working on a proof-of-concept for a
calculation and report engine.

My project is to create a calculation engine that allows users to select
multiple calculations that they want performed, input the required date, and
receive a report that contains their results. Based selected calculations, a
dynamically-generated form is presented with the parameters needed for the
calculations. I will use a dynamically-created XSD to generate the parameter
entry form and validate the data.

I wanted a design that would allow me to revise and add available
calculations without redeploying compiled code. I was thinking that I could
do the calculations using XSDs, since most of the calculations are simple
arithmetic and iteration. By doing the calculations in XSLT rather than
compiled code, I could update or add XSLTs and database entries without
changing the compiled code.

I have come to the conclusion that XSLT is not a good choice for simple
calculations, even relatively simple ones, because it lacks some of the
functionality that I need and would require a lot more developer effort. I
have changed my design to use C# to do the calculations, output them as XML,
and use XSLT to transform the results to formatted HTML. I may revise the
engine later, abstractring and extracting out calculation descriptions as
XML files that C# code reads to determine how to perform calculations on the
data, depending on if I can avoid writing much of a math parser. I certainly
want to avoid redesigning even a small subset of content-based MathML. My
current OOP design contains the majority of the functionality in the base
class and the specific calculations in derived classes, so I will have to
deploy small DLLs, but that will work okay.

Would you say that XSLT is most effectively used for transforming data,
rather than general calculation? The most common uses that I have seen are
XHTML output and XML-to-XML data format changes.

Thanks again for your help. You helped me avoid taking a path that would
have been unproductive.

Regards,

David S. Alexander

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:u6**************@TK2MSFTNGP09.phx.gbl...


David S. Alexander wrote:

I still cannot get my calculation to work correctly. My actual XML is,

<?xml version="1.0" encoding="utf-8" ?>
<Parameters xmlns="http://www.tempuri.org/Parameters">


Namespaces are quite important, in your earlier post you also posted your
"actual" XML but that did not show any namespaces.
With that default namespace declaration all elements in the example are in
that namespace and XSLT templates need to cater for that so you need e.g.

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://www.tempuri.org/Parameters"
version="1.0">

<xsl:template match="ns1:*">
<xsl:element
name="{local-name()}"
namespace="http://www.tempuri.org/CalculatorParameters">
<xsl:apply-templates select="@* | node() "/>
</xsl:element>
</xsl:template>

<xsl:template match="ns1:DateOfBirth">
<xsl:element name="{local-name()}"
namespace="http://www.tempuri.org/CalculatorParameters">
<xsl:attribute name="Age">
<xsl:value-of
select="../following-sibling::ns1:AnalysisDate/@Year - @Year" />
</xsl:attribute>
</xsl:element>
</xsl:template>

</xsl:stylesheet>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jan 18 '06 #5

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

Similar topics

1
1816
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
1818
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
9895
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
2555
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...
4
1588
by: Jacek | last post by:
Hi All, I have small problem. We are using xslt to display xml in web browser. But our (any?) xslt does not support default values stored in xsd ... it leaves empty holes in table. Therefore...
1
1252
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...
5
1316
by: Dhurandhar Bhatvadekar | last post by:
I am a bit puzzled and don't know where to look for. Here is a stylesheet: <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE stylesheet > <xsl:stylesheet version="1.0"...
5
2601
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
3895
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....
0
7014
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...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
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,...
1
4921
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...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3108
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...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
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...

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.