xsl-list
[Top] [All Lists]

Re: [xsl] Sum in conjunction with string splitting?

2006-09-12 11:25:28
Thank you for spotting that error, Dimitre!

What is the difference between $expenses/*/expense and, say,
$expenses/Record/expense ?

-S

On 9/8/06, Dimitre Novatchev <dnovatchev(_at_)gmail(_dot_)com> wrote:
>   <xsl:template match="func-transform:*">
>       <xsl:param name="arg" select="0"/>
>       <xsl:value-of select="$arg" /> <!-- This isn't printing anything -->
>       <xsl:value-of select="substring-after($arg, 'mileage')"/>
>   </xsl:template>


Remove this line:

>       <xsl:value-of select="$arg" /> <!-- This isn't printing anything -->

and if everything else is OK you'll get the correct result.


Here's a working example (do note that it is complete and may be executed):

When the following stylesheet:

<xsl:stylesheet version='1.0' xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:func-transform="f:func-transform"
      exclude-result-prefixes="xsl func-transform">

      <xsl:import href="C:/XSLT/FXSL 1.2/transform-and-sum.xsl" />
      <func-transform:func-transform/>

<xsl:template match="/">
<xsl:variable name="expenses" select="/" />

<xsl:call-template name="transform-and-sum">
      <xsl:with-param name="pFuncTransform"
select="document('')/*/func-transform:*[1]"/>
      <xsl:with-param name="pList"
select="$expenses/*/expense[substring(.,1,7)='mileage']"/>
  </xsl:call-template>
</xsl:template>

  <xsl:template match="func-transform:*">
      <xsl:param name="arg" select="0"/>
      <xsl:value-of select="substring-after($arg, 'mileage')"/>
  </xsl:template>
</xsl:stylesheet>

is applied against the following xml document:

<Expenses>
 <expense>mileage 10</expense>
 <expense>basketball</expense>
 <expense>icing</expense>
 <expense>mileage 40</expense>
</Expenses>

the wanted result is produced:

50

Do note that I have corrected the wrong XPath expression that selects
the lists of expenses!

I also corrected your source xml, because it wasn't well-formed.

Please, in the future improve the preciseness of defining your
problems, otherwise you are intentionally making very low the chances
that someone might understand and help.


--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk



On 9/8/06, Steve <subsume(_at_)gmail(_dot_)com> wrote:
> Sorry. Was trying to be brief. =)
>
>
> Gist of my xsl is like so:
> --------
>
> <xsl:stylesheet version='1.0' xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>       xmlns:msxsl="urn:schemas-microsoft-com:xslt"
>       xmlns:func-transform="f:func-transform"
>       exclude-result-prefixes="xsl func-transform">
>
>       <xsl:import href="../../../Includes/fxsl-MS-1.1/transform-and-sum.xsl" 
/>
>       <func-transform:func-transform/>
> <xsl:template match="/">
> <xsl:variable name="expenses" select="document('remote document')" />
> <xsl:call-template name="transform-and-sum">
>       <xsl:with-param name="pFuncTransform"
> select="document('')/*/func-transform:*[1]"/>
>       <xsl:with-param name="pList"
> select="$expenses[substring(expense,1,7)='mileage']/expense"/>
>   </xsl:call-template>
> </xsl:template>
>
>   <xsl:template match="func-transform:*">
>       <xsl:param name="arg" select="0"/>
>       <xsl:value-of select="$arg" /> <!-- This isn't printing anything -->
>       <xsl:value-of select="substring-after($arg, 'mileage')"/>
>   </xsl:template>
> --
>
> $expenses actually looks like this:
>
> <Expenses>
>  <expense>mileage 10</expense>
>  <expense>basketball</expense>
>  <expense>icing</expense>
>  <expense>mileage 40</expense>
> </Expense>
>
> -----
>
> Desired output of the call to transform-and-sum = "50"
>
> -Steve
>
> On 9/8/06, Dimitre Novatchev <dnovatchev(_at_)gmail(_dot_)com> wrote:
> > > Does this help?
> >
> > No, separate pieces of code do not help but confuse
> >
> > On 9/8/06, Steve <subsume(_at_)gmail(_dot_)com> wrote:
> > > Above that code I expressed...
> > >
> > > <xsl:stylesheet version='1.0' 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> > >        xmlns:msxsl="urn:schemas-microsoft-com:xslt"
> > >        xmlns:func-transform="f:func-transform"
> > >        exclude-result-prefixes="xsl func-transform">
> > >
> > >        <xsl:import 
href="../../../Includes/fxsl-MS-1.1/transform-and-sum.xsl" />
> > >        <func-transform:func-transform/>
> > >
> > > --
> > >
> > > $Expenses actually looks like this:
> > >
> > > <Expenses>
> > >   <expense>mileage 10</expense>
> > >   <expense>basketball</expense>
> > >   <expense>icing</expense>
> > >   <expense>mileage 40</expense>
> > > </Expense>
> > >
> > > I need a result of "50".
> > >
> > > Does this help?
> > >
> > > -Steve
> > >
> > > On 9/8/06, Dimitre Novatchev <dnovatchev(_at_)gmail(_dot_)com> wrote:
> > > > I don't see your complete stylesheet so it can only be noticed that
> > > > the expression:
> > > >
> > > >     $expenses[substring(expense,1,7)='mileage']/expense
> > > >
> > > > is very unlikely to be what you really want to express as the node-set
> > > > to be passed as argument.
> > > >
> > > > This will select all "expense" children if the first "expense" child
> > > > happens to start with "mileage".
> > > >
> > > >
> > > > The problem could be that the $expenses variable doesn't contain what
> > > > you think it should.
> > > >
> > > > Also, I am not sure you have specified a template reference node -- at
> > > > least it is not in the code, shown in your message.
> > > >
> > > >
> > > >
> > > > --
> > > > Cheers,
> > > > Dimitre Novatchev
> > > > ---------------------------------------
> > > > Truly great madness cannot be achieved without significant intelligence.
> > > > ---------------------------------------
> > > > To invent, you need a good imagination and a pile of junk
> > > >
> > > >
> > > >
> > > > On 9/8/06, Steve <subsume(_at_)gmail(_dot_)com> wrote:
> > > > > Dimitre,
> > > > >
> > > > > Incredibly helpful .txt file on the issue, but I seem to be
> > > > > misapplying it. Do you see any obvious errors?
> > > > >
> > > > > I don't get the underlying logic, so it could be that my use of
> > > > > $expense instead of a source doc is my problem. Less likely is the
> > > > > fact that I'm using fxsl-MS.
> > > > >
> > > > > <xsl:call-template name="transform-and-sum">
> > > > >        <xsl:with-param name="pFuncTransform"
> > > > > select="document('')/*/func-transform:*[1]"/>
> > > > >        <xsl:with-param name="pList"
> > > > > select="$expenses[substring(expense,1,7)='mileage']/expense"/>
> > > > > </xsl:call-template>
> > > > >
> > > > > <xsl:template match="func-transform:*">
> > > > >        <xsl:param name="arg" select="0"/>
> > > > >        <xsl:value-of select="$arg" /> <!-- This isn't printing anything 
-->
> > > > >        <xsl:value-of select="substring-after($arg, 'mileage')"/>
> > > > > </xsl:template>
> > > > >
> > > > > $expense does indeed contain something like:
> > > > >
> > > > > <Expenses>
> > > > >   <expense>mileage 50</expense>
> > > > >   <expense>mileage 20</expense>
> > > > >   <expense>mileage 30</expense>
> > > > > </Expense>
> > > > >
> > > > > On 9/8/06, Dimitre Novatchev <dnovatchev(_at_)gmail(_dot_)com> wrote:
> > > > > > On 9/8/06, Steve <subsume(_at_)gmail(_dot_)com> wrote:
> > > > > > > Posted yesterday, but I isolated the problem significantly =)
> > > > > > >
> > > > > > > The snippet below attempts to split the string 'mileage 6' into
> > > > > > > [mileage] and [6] and then store it in $miles (for example).
> > > > > > >
> > > > > > > I am trying to get the SUM of the resulting SPLIT of the nodes 
that
> > > > > > > match 'mileage'.
> > > > > > >
> > > > > > > I am close. Right now I only get one node, not the sum of all 
matches.
> > > > > > >
> > > > > > > <xsl:variable name="miles">
> > > > > > >        <xsl:call-template name="str-split-to-words">
> > > > > > >                <xsl:with-param name="pStr"
> > > > > > > 
select="msxsl:node-set($expenses[substring(expense,1,7)='mileage']/expense)"
> > > > > > > />
> > > > > > >                <xsl:with-param name="pDelimiters" select="' '" />
> > > > > > >                                      <!-- splitting expense at blank 
spaces -->
> > > > > > >        </xsl:call-template>
> > > > > > > </xsl:variable>
> > > > > > > <xsl:value-of select="msxsl:node-set($miles)/*[2]" /><br />
> > > > > > >
> > > > > > > ====
> > > > > > >
> > > > > > > $expenses example:
> > > > > > >
> > > > > > > <Expenses>
> > > > > > >    <expense>Mileage 6</expense>
> > > > > > >    <expense>cheese</expense> (wouldn't match)
> > > > > > >    <expense>Mileage 10</expense>
> > > > > > > </Expenses>
> > > > > > >
> > > > > > > ====
> > > > > > >
> > > > > > > Desired value of $miles: 16.
> > > > > >
> > > > > > With FXSL2.0 one would simply write:
> > > > > >
> > > > > >       sum(f:map(f:compose(f:decimal(),
> > > > > >                           f:flip(f:substring-after(),' ')
> > > > > >                           ),
> > > > > >                 /*/*[contains(.,' ')]
> > > > > >                 )
> > > > > >           )
> > > > > >
> > > > > >
> > > > > > Or simply use the f:transform-and-sum() function:
> > > > > >
> > > > > >       f:transform-and-sum(f:flip(f:substring-after(),' '),
> > > > > >                           /*/*[contains(.,' ')]
> > > > > >                           )
> > > > > >
> > > > > >
> > > > > > In XSLT 1.0, one can use the "transform-and-sum" template of FXSL 
1.x
> > > > > >
> > > > > >
> > > > > > --
> > > > > > Cheers,
> > > > > > Dimitre Novatchev
> > > > > > ---------------------------------------
> > > > > > Truly great madness cannot be achieved without significant 
intelligence.
> > > > > > ---------------------------------------
> > > > > > To invent, you need a good imagination and a pile of junk
> > > > > >
> > > > > >
> > > > > >
> > > > > > >
> > > > > > > Thanks a bunch,
> > > > > > >
> > > > > > > -Steve
> > > > > > >
> > > > >
> > > > > --~------------------------------------------------------------------
> > > > > XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> > > > > To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
> > > > > or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
> > > > > --~--
> > > > >
> > > > >
> > > >
> > > > --~------------------------------------------------------------------
> > > > XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> > > > To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
> > > > or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
> > > > --~--
> > > >
> > > >
> > >
> > > --~------------------------------------------------------------------
> > > XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> > > To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
> > > or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
> > > --~--
> > >
> > >
> >
> >
> > --
> > Cheers,
> > Dimitre Novatchev
> > ---------------------------------------
> > Truly great madness cannot be achieved without significant intelligence.
> > ---------------------------------------
> > To invent, you need a good imagination and a pile of junk
> >
> > --~------------------------------------------------------------------
> > XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> > To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
> > or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
> > --~--
> >
> >
>
> --~------------------------------------------------------------------
> XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
> or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
> --~--
>
>

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--