Connecting Tech Pros Worldwide Forums | Help | Site Map

Subtracting the values in attributes in an XSLT

David S. Alexander
Guest
 
Posts: n/a
#1: Jan 14 '06
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.



Martin Honnen
Guest
 
Posts: n/a
#2: Jan 14 '06

re: Subtracting the values in attributes in an XSLT




David S. Alexander wrote:

[color=blue]
> <Dates>
> <Birth Year="1966" />
> <Current Year="2006" />
> </Dates>
> and the output that I want is simply,
>
> <Age YearsOld="40"/>[/color]

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



--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
David S. Alexander
Guest
 
Posts: n/a
#3: Jan 16 '06

re: Subtracting the values in attributes in an XSLT


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" <mahotrash@yahoo.de> wrote in message
news:OaY6ZsQGGHA.3864@tk2msftngp13.phx.gbl...[color=blue]
>
>
> David S. Alexander wrote:
>
>[color=green]
>> <Dates>
>> <Birth Year="1966" />
>> <Current Year="2006" />
>> </Dates>
>> and the output that I want is simply,
>>
>> <Age YearsOld="40"/>[/color]
>
> <xsl:template match="Dates">
> <Age YearsOld="{Current/@Year - Birth/@Year}" />
> </xsl:template>
>
>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/[/color]


Martin Honnen
Guest
 
Posts: n/a
#4: Jan 17 '06

re: Subtracting the values in attributes in an XSLT




David S. Alexander wrote:

[color=blue]
> 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">[/color]

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/
David S. Alexander
Guest
 
Posts: n/a
#5: Jan 18 '06

re: Subtracting the values in attributes in an XSLT


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" <mahotrash@yahoo.de> wrote in message
news:u6Hpt52GGHA.2680@TK2MSFTNGP09.phx.gbl...[color=blue]
>
>
> David S. Alexander wrote:
>
>[color=green]
>> 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">[/color]
>
> 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/[/color]


Closed Thread