Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 1st, 2006, 07:32 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your code

          <xsl:for-each select="fcs:leadParagraph">
        <p><strong><xsl:value-of select="fcs:paragraph"/></strong></p>
        </xsl:for-each>

iterates over the leadParagraph's - but there is only one. For each one, it outputs a paragraph. The content of that paragraph is obtained from the path expression select="fcs:paragraph", which selects three paragraphs. In XSLT 1.0, this will give you the text of the first child paragraph. In XSLT 2.0, it will give you the text of all three child paragraphs, space-separated.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 4th, 2006, 02:38 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hallo all,

Is it possible to write ein iteration in XSL like

        <table id="epoNewsItem" style="border-bottom:none">
        <xsl:variable name="i" select="0"/> <xsl:for-each select="fcs:folderHeadline">
                <xsl:variable name="content" select="fcs:contentParts/@contentType"/>

                <xsl:variable name="year" select="substring(fcs:publicationDate,0,5)"/>
                <xsl:variable name="month" select="substring(fcs:publicationDate,6,2)"/>
                <xsl:variable name="day" select="substring(fcs:publicationDate,9,2)"/>
                <tr>
                     <xsl:choose>
                                        <xsl:when test="$content='WebPage'">
                                            <td class="text"><img src="http://dg1-2.internal.epo.org/_test/Huerol/Factiva/fwebicon.gif" alt="Factiva Web Content Icon" style='margin-top:3px'/><xsl:text></xsl:text><xsl:value-of select="$day"/>.<xsl:value-of select="$month"/>.<xsl:value-of select="$year"/></td>
                                        </xsl:when>
                                        <xsl:otherwise>
                    <td class="text"><img src="http://dg1-2.internal.epo.org/_test/Huerol/Factiva/fac_pub.gif" alt="Factiva Publications Icon" style='margin-top:3px'/><xsl:text></xsl:text><xsl:value-of select="$day"/>.<xsl:value-of select="$month"/>.<xsl:value-of select="$year"/></td>
                    </xsl:otherwise>
                                    </xsl:choose>
                 </tr>
                 <tr>
                    <xsl:variable name="headline" select="fcs:headline/fcs:paragraph"/>
                    <xsl:variable name="cut" select="substring($headline,0,50)"/>
                    <td class="text"><h3><a href="javascript:sendContentRequest($i)"><xsl:valu e-of select="$cut"/><xsl:text>...</xsl:text></a></h3></td> </tr>
                 <tr>
                     <xsl:variable name="snippet" select="fcs:snippet"/>
                     <xsl:variable name="cPos" select="string-length(substring-before($snippet, '-- '))"/>
                    <xsl:choose>
                        <xsl:when test="$cPos >= 0">
                            <xsl:variable name="cutSnippet" select="substring($snippet,$cPos+2,100+$cPos+3)"/>
                            <td class="text"><xsl:value-of select="$cutSnippet"/></td>
                        </xsl:when>
                        <xsl:otherwise>
                         <xsl:variable name="cutSnippet" select="substring($snippet,0,100)"/>
                         <td class="text"><xsl:value-of select="$cutSnippet"/></td>
                        </xsl:otherwise>
                    </xsl:choose>
                    
                 </tr>
                 <xsl:variable name="i" select="$i+1"/>
                </xsl:for-each>
        </table>

If not, how can I construct such an iteration in XSL? Can anyone help me?

Your attitude determines your altitude
 
Old September 4th, 2006, 02:49 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

No, XSLT is a functional language, so you can't increment variables. In general you need to use recursion. See Chapter 8 of my book for a detailed discussion of the implications of this on your coding style.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 4th, 2006, 02:53 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry michael I don't have your book. Where can I find its PDF version? I cannot buy it now but later on Today but I need this recursion now. Can you please give me an example if it is possible?

Your attitude determines your altitude
 
Old September 4th, 2006, 03:08 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can't for the reason I stated before, xsl:for-each doesn't loop over the elements it processes them all simultaneously, in theory anyway.
If you are processing a sequence of nodes then you can use the position() function in XPath to find where they are in the list but there's no guarantee the last node won't be processed before the first node etc.
Code:
<xsl:for-each select="myElement">
  Position: <xsl:value-of select="position()/>
</xsl:for-each>
--

Joe (Microsoft MVP - XML)
 
Old September 4th, 2006, 03:20 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by joefawcett
 You can't for the reason I stated before, xsl:for-each doesn't loop over the elements it processes them all simultaneously, in theory anyway.
If you are processing a sequence of nodes then you can use the position() function in XPath to find where they are in the list but there's no guarantee the last node won't be processed before the first node etc.
Code:
<xsl:for-each select="myElement">
  Position: <xsl:value-of select="position()/>
</xsl:for-each>
--

Joe (Microsoft MVP - XML)
Hallo joe,

It is actually like this. These headlines are dynamic arrays, that its I have to iterate them like

for(int i=0; i<array.length; i++){
---
}

So how can the function position() work in this occasion? Can you please help me?


Your attitude determines your altitude
 
Old September 4th, 2006, 03:22 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, I can answer direct coding questions here but I can't give a training course.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 4th, 2006, 03:25 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael I found some resources and I redefine the code chunk like that is it alright??
<xsl:template name="recursionHeadline" match="SOAP-ENV:Envelope/SOAP-ENV:Body/fcs:GetFolderHeadlinesResponse/fcs:folderHeadlinesResponse/fcs:folderHeadlinesResult/fcs:folder/fcs:folderHeadlinesResultSet">
<xsl:param name="i"/>
    <head>
            <title>Factiva Test</title>
            <link href='http://intranet1-t.internal.epo.org/portal/themes/html/epoIntranetCD/cssTheme.css' rel="stylesheet" type="text/css" />
            <link href='http://intranet1-t.internal.epo.org/portal/themes/html/epoIntranetCD/cssPageBody.css' rel="stylesheet" type="text/css" />
    </head>
    <body>
        <img src="http://dg1-2.internal.epo.org/_test/Huerol/Factiva/fac1.bmp" align="right" />
        <br clear="all"/>
        <table id="epoNewsItem" style="border-bottom:none">

                <xsl:for-each select="fcs:folderHeadline">
                <xsl:variable name="content" select="fcs:contentParts/@contentType"/>

                <xsl:variable name="year" select="substring(fcs:publicationDate,0,5)"/>
                <xsl:variable name="month" select="substring(fcs:publicationDate,6,2)"/>
                <xsl:variable name="day" select="substring(fcs:publicationDate,9,2)"/>
                <tr>
                     <xsl:choose>
                                        <xsl:when test="$content='WebPage'">
                                            <td class="text"><img src="http://dg1-2.internal.epo.org/_test/Huerol/Factiva/fwebicon.gif" alt="Factiva Web Content Icon" style='margin-top:3px'/><xsl:text></xsl:text><xsl:value-of select="$day"/>.<xsl:value-of select="$month"/>.<xsl:value-of select="$year"/></td>
                                        </xsl:when>
                                        <xsl:otherwise>
                    <td class="text"><img src="http://dg1-2.internal.epo.org/_test/Huerol/Factiva/fac_pub.gif" alt="Factiva Publications Icon" style='margin-top:3px'/><xsl:text></xsl:text><xsl:value-of select="$day"/>.<xsl:value-of select="$month"/>.<xsl:value-of select="$year"/></td>
                    </xsl:otherwise>
                                    </xsl:choose>
                  </tr>
                  <tr>
                     <xsl:variable name="size" select="count(fcs:headline/fcs:paragraph)"/>
                      <xsl:if test="$i &lt; $size">
                    <xsl:variable name="headline" select="fcs:headline/fcs:paragraph"/>
                    <xsl:variable name="cut" select="substring($headline,0,50)"/>
                    <td class="text"><h3><a href="javascript:sendContentRequest($i)"><xsl:valu e-of select="$cut"/><xsl:text>...</xsl:text></a></h3></td>
                    <xsl:call-template name="recursion">
                        <xsl:with-param name="i" select="$i+1"/>
                    </xsl:call-template>
                    </xsl:if>
                     </tr>
                  <tr>
                     <xsl:variable name="snippet" select="fcs:snippet"/>
                     <xsl:variable name="cPos" select="string-length(substring-before($snippet, '-- '))"/>
                    <xsl:choose>
                        <xsl:when test="$cPos > 0">
                            <xsl:variable name="cutSnippet" select="substring($snippet,$cPos+3,100+$cPos+3)"/>
                            <td class="text"><xsl:value-of select="$cutSnippet"/></td>
                        </xsl:when>
                        <xsl:otherwise>
                         <xsl:variable name="cutSnippet" select="substring($snippet,0,100)"/>
                          <td class="text"><xsl:value-of select="$cutSnippet"/></td>
                        </xsl:otherwise>
                    </xsl:choose>

                 </tr>
                </xsl:for-each>
        </table>
        </body>

Your attitude determines your altitude
 
Old September 4th, 2006, 04:40 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok I redesigned the code but I am having a problem

I have written
<tr>
                     <td class="text"><h3><xsl:apply-templates select="fcs:headline/fcs:paragraph"></xsl:apply-templates>
                     </h3></td>
</tr>

    <xsl:template name="recursion" match="fcs:headline/fcs:paragraph">
        <xsl:param name="i" select="0"/>
                 <xsl:if test="$i &gt;0">
                     <xsl:variable name="headline" select="fcs:headline/fcs:paragraph"/>
                    <xsl:variable name="cut" select="substring(fcs:headline/$headline,0,50)"/>

                    <a href="{$i}"><xsl:value-of select="$cut"/><xsl:text>...</xsl:text></a>
                    <xsl:call-template name="recursion">
                        <xsl:with-param name="i" select="$i+1"/>

                    </xsl:call-template>
        </xsl:if>
    </xsl:template>

Ok I think it does the recursion but It doesn't make to show this <a href="{$i}"><xsl:value-of select="$cut"/><xsl:text>...</xsl:text></a> as a link. What should I do to have its a href functionality again?




Your attitude determines your altitude
 
Old September 4th, 2006, 05:11 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

As written, your code will recurse for ever, because the terminating condition ($i <= 0) is never reached.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to loop through a HashMap in XSL? sunilnk XSLT 6 April 10th, 2017 04:09 AM
help: how can I break from xsl for-each loop ? back2grave XSLT 5 May 31st, 2012 12:03 PM
XSL Loop and incrementing a variable 2007-03-29 XSLT 8 March 30th, 2007 10:41 AM
xsl:for-each loop Tomi XSLT 3 September 7th, 2006 05:35 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.