473,738 Members | 5,934 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:attribut e 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 8996


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="{Curr ent/@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" />
</CalculatorParam eters>

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/CalculatorParam eters">
<Client>
<Name First="Pat" Last="Jones" />
<DateOfBirth Age="19"/>
</Client>
<AnalysisDate Month="1" Day="16" Year="2006" />
</CalculatorParam eters>

I tried using an absolute path in my XSLT,

<xsl:template match="DateOfBi rth">
<DateOfBirth>
<xsl:attribut e name="Age">
<xsl:value-of select="/Parameters/Client/DateOfBirth/@Year -
@Year"/>
</xsl:attribute>
</ClientDateOfBir th>
</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*******@yaho o.de> wrote in message
news:Oa******** ******@tk2msftn gp13.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="{Curr ent/@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:styleshe et
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/CalculatorParam eters">
<xsl:apply-templates select="@* | node() "/>
</xsl:element>
</xsl:template>

<xsl:template match="ns1:Date OfBirth">
<xsl:element name="{local-name()}"
namespace="http ://www.tempuri.org/CalculatorParam eters">
<xsl:attribut e name="Age">
<xsl:value-of
select="../following-sibling::ns1:An alysisDate/@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*******@yaho o.de> wrote in message
news:u6******** ******@TK2MSFTN GP09.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:styleshe et
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/CalculatorParam eters">
<xsl:apply-templates select="@* | node() "/>
</xsl:element>
</xsl:template>

<xsl:template match="ns1:Date OfBirth">
<xsl:element name="{local-name()}"
namespace="http ://www.tempuri.org/CalculatorParam eters">
<xsl:attribut e name="Age">
<xsl:value-of
select="../following-sibling::ns1:An alysisDate/@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
1827
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
1841
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
9922
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
2588
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:
4
1604
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 I need to write them to xml explicitly. I would like to load them from xsd. Is it possible in xerces ? I create empty DOM document and store it to file. Is it possible to bind schema to document and retrieve default values for attributes before...
1
1273
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...
5
1324
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" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"> </xsl:output>
5
2631
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
3908
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. ...
0
8788
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,...
0
9335
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
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
6751
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
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
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
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
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.