Re: [xsl] help with looping thru children

Subject: Re: [xsl] help with looping thru children
From: "CyberSpace Industries 2000 Inc." <csi2000@xxxxxxxxxxxxxxx>
Date: Wed, 3 May 2006 14:16:01 -0400
Lyn ...One piece of advice I give some of my students is to stop thinking procedurally.

The XSLT processor will read your XML, build an in-memory tree of nodes that corresponds to the hiearchy of your XML document, then traverse that tree in document order. This comes for free and you don't have to "program" this navigation.

What I did below is basically get to the EVENT node - then from there "pull in" everything I needed from the tree - relative to the EVENT node. Doing it this way, I also handle the case where there is missing nodes, such as your final EVENT where there is only a DOWNLOAD node.

Cheers...Hugh
CyberSpace Industries 2000 Inc.
XML Training and Consulting

I get:
---------------------------------------------------------------
http://w w w.google.com/search?q = web
+analytics,2004-04-12 11:54:48 PST,HBX Home,/Products/HBX,1,3,1123,1,,ENDROW
http://w w w.google.com/search?q = web
+analytics,2004-04-12 11:56:46 PST, Download Page,/Products/HBX/Downloads,,3,1123,3,,ENDROW
http://w w w.google.com/search?q = web
+analytics,2004-04-12 11:56:58 CST,,,,,,,Simple eBook,ENDROW


------------------------------------------------------------------

I didn't try to fix the line end problems.. or the "w w w" - The first one (before the +) comes from your xml.

Also didn't know what you wnated to do when there was no DOWNLOAD

--- xsl ---

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
<xsl:output method="text" encoding="ISO-8859-1"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>


<xsl:template match="SESSION">
  <xsl:apply-templates />
</xsl:template>

<xsl:template match="SUMMARY"/>

<!-- Pull in everything for a single output line for each EVENT -->
<xsl:template match="EVENTS">
  <xsl:apply-templates />
</xsl:template>

<!-- the following gets triggered for each EVENT node encountered -->
<xsl:template match="EVENT">
<!-- put the URL at the beginning of each output line -->
  <xsl:value-of select="//REFERRING_URL" />
  <xsl:text>,</xsl:text>
  <xsl:value-of select="TIMESTAMP" />
     <xsl:text>,</xsl:text>

  <xsl:value-of select="VIEW/PAGENAME"/>
  <xsl:text>,</xsl:text>
  <xsl:value-of select="VIEW/CONTENT"/>
  <xsl:text>,</xsl:text>
  <xsl:value-of select="VIEW/RELOADS"/>
  <xsl:text>,</xsl:text>

  <xsl:value-of select="FUNNELS/FUNNEL/IS_CONVERSION_LEVEL"/>
  <xsl:text>,</xsl:text>
  <xsl:value-of select="FUNNELS/FUNNEL/FUNNEL_ID"/>
  <xsl:text>,</xsl:text>
  <xsl:value-of select="FUNNELS/FUNNEL/FUNNEL_LEVEL"/>
  <xsl:text>,</xsl:text>

  <xsl:value-of select="DOWNLOAD"/>
  <xsl:text>,ENDROW&#013;</xsl:text>
</xsl:template>
</xsl:stylesheet>


----- Original Message ----- From: "Lyn Stewart-Hunter" <Lyn@xxxxxxxxxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Wednesday, May 03, 2006 1:07 PM
Subject: [xsl] help with looping thru children



Howdy.  I was able to use some past help to get header data to repeat in
a CSV format, but I then couldn't figure out where to put the for-each
to loop thru all EVENTS (or whatever structure would work).  From the
XML I would like a row in a CSV file for every EVENT (and include the
SUMMARY.REFERRING_URL).

I am also getting the DOWNLOAD on the 'wrong' row (maybe a new template
at that level?) and need to figure out how to get FUNNEL.CONVERSION ID
listed after its EVENT - if that's easy for someone to show me.  But I
am most interesting capturing each EVENT and maintaining the SUMMARY
data for each row - I don't know xml very well, so examples are ideal.

thank you. Lyn

My current output looks like:
2004-04-12 11:54:48 PST,HBX Home,Simple eBook,http://w w
w.google.com/search?q = web +analytics,ENDROW

XML is:
<?xml version='1.0' encoding='utf-8' ?>
<SESSION>
<SUMMARY>
<REFERRING_URL>http://w w w.google.com/search?q = web
+analytics</REFERRING_URL>
<BROWSER> MSIE 6.0</BROWSER>
</SUMMARY>
<EVENTS>
<EVENT>
<TIMESTAMP>2004-04-12 11:54:48 PST</TIMESTAMP>
<VIEW>
<PAGENAME>HBX Home</PAGENAME>
<CONTENT>/Products/HBX</CONTENT>
<RELOADS>1</RELOADS>
</VIEW>
<FUNNELS>
<FUNNEL>
<IS_CONVERSION_LEVEL>3</IS_CONVERSION_LEVEL>
<FUNNEL_ID>1123</FUNNEL_ID>
<FUNNEL_LEVEL>1</FUNNEL_LEVEL>
</FUNNEL>
</FUNNELS>
</EVENT>
<EVENT>
<TIMESTAMP>2004-04-12 11:56:46 PST</TIMESTAMP>
<VIEW>
<PAGENAME> Download Page</PAGENAME>
<CONTENT>/Products/HBX/Downloads</CONTENT>
</VIEW>
<FUNNELS>
<FUNNEL>
<IS_CONVERSION_LEVEL>3</IS_CONVERSION_LEVEL>
<FUNNEL_ID>1123</FUNNEL_ID>
<FUNNEL_LEVEL>3</FUNNEL_LEVEL>
</FUNNEL>
</FUNNELS>
</EVENT>
<EVENT>
<TIMESTAMP>2004-04-12 11:56:58 CST</TIMESTAMP>
<DOWNLOAD>Simple eBook</DOWNLOAD>
</EVENT>
</EVENTS>
</SESSION>
+++++++++++++++++++++
XSL is:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:output method="text"
encoding="ISO-8859-1"/>

<xsl:strip-space elements="*"/>

<xsl:template match="/">
<xsl:apply-templates select="SESSION"/>
</xsl:template>

<xsl:template match="SESSION">
<xsl:apply-templates select="EVENTS"/>
</xsl:template>

<xsl:template match="SUMMARY">
<xsl:value-of select="REFERRING_URL"/>
<xsl:text>,</xsl:text>
</xsl:template>

<xsl:template match="EVENTS">
               <xsl:value-of select="//TIMESTAMP"/>
<xsl:text>,</xsl:text>
               <xsl:value-of select="//PAGENAME"/>
<xsl:text>,</xsl:text>
               <xsl:value-of select="//DOWNLOAD"/>
<xsl:text>,</xsl:text>
<xsl:apply-templates select="../SUMMARY"/>
              <xsl:text>ENDROW&#013;&#010;</xsl:text>

</xsl:template>
</xsl:stylesheet>

Current Thread