XSL: función suma (adición de valores en xml)

  • dogbert
  • Novice
  • Novice
  • Avatar de Usuario
  • Registrado: Dic 06, 2007
  • Mensajes: 28
  • Loc: Tacoma, WA
  • Status: Offline

Nota Mayo 5th, 2009, 3:10 pm

Hola,

Ive miró alrededor de la red en busca de ayuda y trataron una serie de maneras de conseguir lo que quiero, sin solución de trabajo para lo que Im tratando de hacer, que me está molestando, ya que parece que debe ser muy fácil. Quiero sumar elementos en mi doc XML, simple como eso.

ejemplo de archivo XML. adding.xml
Código: [ Select ]
 
<?xml version="1.0"?>
<things>
     <thing>
          <name>one</name>
          <description>one</description>
          <numbertoadd>1.2</numbertoadd>
     </thing>
     <thing>
          <name>two</name>
          <description>two</description>
          <numbertoadd>2.3</numbertoadd>
     </thing>
     <thing>
          <name>three</name>
          <description>three</description>
          <numbertoadd>3.2</numbertoadd>
     </thing>
</things>
 
  1.  
  2. <?xml version="1.0"?>
  3. <things>
  4.      <thing>
  5.           <name>one</name>
  6.           <description>one</description>
  7.           <numbertoadd>1.2</numbertoadd>
  8.      </thing>
  9.      <thing>
  10.           <name>two</name>
  11.           <description>two</description>
  12.           <numbertoadd>2.3</numbertoadd>
  13.      </thing>
  14.      <thing>
  15.           <name>three</name>
  16.           <description>three</description>
  17.           <numbertoadd>3.2</numbertoadd>
  18.      </thing>
  19. </things>
  20.  


Lo que quiero hacer es agregar el "numbertoadd" elemets de cada uno en este caso, resultado de 6. 7

Si importa, Im usando ASP clásico al formato XML con una hoja de estilo XSL para que puedas ponerlo donde yo quiera.
adding.asp ejemplo
Código: [ Select ]
 
<%
 
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("adding.xml"))
 
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("adding.xsl"))
 
    Response.Write(xml.transformNode (xsl))
 
    Response.write("show that it does something.......")
   
%>
 
  1.  
  2. <%
  3.  
  4. set xml = Server.CreateObject("Microsoft.XMLDOM")
  5. xml.async = false
  6. xml.load(Server.MapPath("adding.xml"))
  7.  
  8. set xsl = Server.CreateObject("Microsoft.XMLDOM")
  9. xsl.async = false
  10. xsl.load(Server.MapPath("adding.xsl"))
  11.  
  12.     Response.Write(xml.transformNode (xsl))
  13.  
  14.     Response.write("show that it does something.......")
  15.    
  16. %>
  17.  


Toda y cualquier ayuda o la orientación es muy apreciada.

Gracias,
Chris (dogbert)
  • Anonymous
  • Bot
  • No Avatar
  • Registrado: 25 Feb 2008
  • Mensajes: ?
  • Loc: Ozzuland
  • Status: Online

Nota Mayo 5th, 2009, 3:10 pm

  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8211
  • Loc: USA
  • Status: Offline

Nota Mayo 5th, 2009, 3:51 pm

Se esto ayudar de alguna manera?


Otro correo:
Quote:
Sólo tienes que seleccionar los nodos que desea con una suma Xpath y luego aplicar
suma () para los.


No sé si los ayudaría a...algo que encontré buscando google .
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • dogbert
  • Novice
  • Novice
  • Avatar de Usuario
  • Registrado: Dic 06, 2007
  • Mensajes: 28
  • Loc: Tacoma, WA
  • Status: Offline

Nota Mayo 8th, 2009, 9:19 am

Me señaló la dirección correcta, pero después de jugar con ella y otros ejemplos Ive encontró que no he tenido suerte, relly me molesta, así que decidió trasladarse a una solución que tengo más knoledge con.

mi tiro a tan sólo tirando de los datos y hacer los cálculos en el archivo ASP.

adding.xsl
Código: [ Select ]
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
      <xsl:for-each select="dailytasks/task">
 
        <xsl:value-of select="tasktime" />#
      </xsl:for-each>
 
</xsl:template>
 
</xsl:stylesheet>
 
  1.  
  2. <?xml version="1.0" encoding="ISO-8859-1"?>
  3. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
  4. <xsl:template match="/">
  5.       <xsl:for-each select="dailytasks/task">
  6.  
  7.         <xsl:value-of select="tasktime" />#
  8.       </xsl:for-each>
  9.  
  10. </xsl:template>
  11.  
  12. </xsl:stylesheet>
  13.  


agregando. asp
Código: [ Select ]
 
<%
 
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("adding.xml"))
 
On Error Resume Next
 
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("adding.xsl"))
 
On Error Resume Next
 
Dim numbr, num, valu, length, fnl, kickr, kickrn
 
    numbr = xml.transformNode(xsl)
       
    num = split(numbr, "#")
   
    kickr = Cdbl(num(0))
   
    length = Ubound(num)
   
    valu = 0
   
    for x = 0 to Ubound(num)
        valu = (valu + Cdbl(num(x)))
    next
   
        fnl = (valu + kickr)
   
On Error Resume Next
   
   
%>
<%= numbr %> <em>: all together no format<br/></em>
<br/>Static Counts:<br/><br/>
<%= num(0) %> <em>: First After The Split<br/></em>
<%= num(1) %> <em>: Seccond After The Split<br/></em>
<%= num(2) %> <em>: Third After The Split<br/></em>
<%= num(3) %> <em>: Fourth After The Split<br/></em>
<%= num(4) %> <em>: Fifth After The Split<br/></em>
<%= num(5) %> <em>: Sixth After The Split<br/></em>
<%= num(6) %> <em>: Seventh After The Split<br/></em>
<%= num(7) %> <em>: Eight After The Split<br/><br/></em>
<%= length %> <em>: Lenght of the Array<br/><br/></em>
<%= valu %> <em>: Final Before the Kicker<br/></em>
<%= kickr %> <em>: The Kicker Value<br/><br/></em>
<%= fnl %> <em>: The Total Hours Spend per the adding.xml log - Should be "8"<br/></em>
 
  1.  
  2. <%
  3.  
  4. set xml = Server.CreateObject("Microsoft.XMLDOM")
  5. xml.async = false
  6. xml.load(Server.MapPath("adding.xml"))
  7.  
  8. On Error Resume Next
  9.  
  10. set xsl = Server.CreateObject("Microsoft.XMLDOM")
  11. xsl.async = false
  12. xsl.load(Server.MapPath("adding.xsl"))
  13.  
  14. On Error Resume Next
  15.  
  16. Dim numbr, num, valu, length, fnl, kickr, kickrn
  17.  
  18.     numbr = xml.transformNode(xsl)
  19.        
  20.     num = split(numbr, "#")
  21.    
  22.     kickr = Cdbl(num(0))
  23.    
  24.     length = Ubound(num)
  25.    
  26.     valu = 0
  27.    
  28.     for x = 0 to Ubound(num)
  29.         valu = (valu + Cdbl(num(x)))
  30.     next
  31.    
  32.         fnl = (valu + kickr)
  33.    
  34. On Error Resume Next
  35.    
  36.    
  37. %>
  38. <%= numbr %> <em>: all together no format<br/></em>
  39. <br/>Static Counts:<br/><br/>
  40. <%= num(0) %> <em>: First After The Split<br/></em>
  41. <%= num(1) %> <em>: Seccond After The Split<br/></em>
  42. <%= num(2) %> <em>: Third After The Split<br/></em>
  43. <%= num(3) %> <em>: Fourth After The Split<br/></em>
  44. <%= num(4) %> <em>: Fifth After The Split<br/></em>
  45. <%= num(5) %> <em>: Sixth After The Split<br/></em>
  46. <%= num(6) %> <em>: Seventh After The Split<br/></em>
  47. <%= num(7) %> <em>: Eight After The Split<br/><br/></em>
  48. <%= length %> <em>: Lenght of the Array<br/><br/></em>
  49. <%= valu %> <em>: Final Before the Kicker<br/></em>
  50. <%= kickr %> <em>: The Kicker Value<br/><br/></em>
  51. <%= fnl %> <em>: The Total Hours Spend per the adding.xml log - Should be "8"<br/></em>
  52.  


resultado ser muy estrecha sin embargo, no me deja convertir el primer valor de la matriz num "num (0)" a un entero decimal que se añadirán por lo que no agrega él. nota de la matriz num salta (0), porque el bucle tiene que empezar a 1 por lo tanto, agregó que en Im después. Funciona perfecto, excepto por el primer número, juzgado con otros archivos XML con el mismo resultado.
Código: [ Select ]
 
0.50# 0.25# 0.50# 0.50# 1.50# 0.50# 0.50# 3.75# : all together no format
 
Static Counts to check:
 
0.50 : First After The Split
0.25 : Seccond After The Split
0.50 : Third After The Split
0.50 : Fourth After The Split
1.50 : Fifth After The Split
0.50 : Sixth After The Split
0.50 : Seventh After The Split
3.75 : Eight After The Split
 
8 : Lenght of the Array
 
7.5 : Final Before the Kicker
: The Kicker Value
 
7.5 : The Total Number per the adding.xml log - Should be "8"
 
  1.  
  2. 0.50# 0.25# 0.50# 0.50# 1.50# 0.50# 0.50# 3.75# : all together no format
  3.  
  4. Static Counts to check:
  5.  
  6. 0.50 : First After The Split
  7. 0.25 : Seccond After The Split
  8. 0.50 : Third After The Split
  9. 0.50 : Fourth After The Split
  10. 1.50 : Fifth After The Split
  11. 0.50 : Sixth After The Split
  12. 0.50 : Seventh After The Split
  13. 3.75 : Eight After The Split
  14.  
  15. 8 : Lenght of the Array
  16.  
  17. 7.5 : Final Before the Kicker
  18. : The Kicker Value
  19.  
  20. 7.5 : The Total Number per the adding.xml log - Should be "8"
  21.  
  • Zwirko
  • Guru
  • Guru
  • Avatar de Usuario
  • Registrado: May 29, 2005
  • Mensajes: 1417
  • Loc: 55° N, 3° W
  • Status: Offline

Nota Mayo 8th, 2009, 1:39 pm

Usted puede sumar los nodos de una manera muy simple, así:

Código: [ Select ]
<?xml version="1.0" encoding="UTF-8"?>
 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>
 
<xsl:template match="/">
 
<total><xsl:value-of select='sum(/things/thing/numbertoadd)'/></total>
 
</xsl:template>
</xsl:stylesheet>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <xsl:stylesheet version="1.0"
  4. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  5. <xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>
  6.  
  7. <xsl:template match="/">
  8.  
  9. <total><xsl:value-of select='sum(/things/thing/numbertoadd)'/></total>
  10.  
  11. </xsl:template>
  12. </xsl:stylesheet>


El Ive XSLT escrito puede ser simplista para sus necesidades, pero sí la suma de los valores de la "numbertoadd" nodos a 6,7
  • dogbert
  • Novice
  • Novice
  • Avatar de Usuario
  • Registrado: Dic 06, 2007
  • Mensajes: 28
  • Loc: Tacoma, WA
  • Status: Offline

Nota Mayo 22nd, 2009, 10:40 am

Ver que era exactamente lo que estaba buscando, no sé por qué no he podido obtener la suma funcione en absoluto. Pero que parece funcionar para mí, podría haber sido el guión = "sí"?

De todos modos, mucho agradecimiento.

Publicar Información

  • Total de mensajes en este tema: 5 mensajes
  • Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 118 invitados
  • No puede abrir nuevos temas en este Foro
  • No puede responder a temas en este Foro
  • No puede editar sus mensajes en este Foro
  • No puede borrar sus mensajes en este Foro
  • No puede enviar adjuntos en este Foro
 
 

© 2011 Unmelted, LLC. Ozzu® es una marca registrada de Unmelted, LLC