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 January 22nd, 2007, 03:37 PM
Authorized User
 
Join Date: Jan 2007
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default apply-templates problem within for-each loop

im new to XSLT and am having a hard time trying to get apply-templates to work correctly within a for-each loop. the following simple XSLT works perfectly, and is what im trying to use apply-templates to. ill post my other XSLT that doesn't work with apply-templates below this one.

THIS ONE WORKS JUST FINE:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <table border="0" cellspacing="0" cellpadding="0">
      <xsl:for-each select="Collection/Content">
        <xsl:if test="Title!='Placeholder Title'">

          <tr>
            <td valign="top">
              <img src="http://172.16.2.33/images/global/bullet.gif" style="padding: 8px 8px 0px 4px;" />
            </td>
            <td>
              <a>
                <xsl:attribute name="href">
                  <xsl:choose>
                    <xsl:when test="Type ='Assets' or Type = 8 ">
                      javascript:void window.open(...blah...)
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="QuickLink"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:attribute>
                <xsl:value-of select="Title"/>
              </a>
            </td>
          </tr>

        </xsl:if>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

THIS ONE DOESN'T WORK WITH APPLY-TEMPLATES:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <table border="0" cellspacing="0" cellpadding="0">
      <xsl:for-each select="Collection/Content">
        <xsl:if test="Title!='Placeholder Title'">
          <xsl:apply-templates select="*" mode="output" />
        </xsl:if>
      </xsl:for-each>
    </table>
  </xsl:template>
  <xsl:template match="*" mode="output">
    <tr>
      <td valign="top">
        <img src="http://172.16.2.33/images/global/bullet.gif" style="padding: 8px 8px 0px 4px;" />
      </td>
      <td>
        <a>
          <xsl:attribute name="href">
            <xsl:choose>
              <xsl:when test="Type ='Assets' or Type = 8 ">
                javascript:void window.open(...blah...)
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="QuickLink"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <xsl:value-of select="Title"/>
        </a>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

i haven't a clue as to what it is that im doing wrong.
any help would be GREATLY appreciated!
thanks & cheers,
steve =)


 
Old January 22nd, 2007, 04:42 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

From a quick look I think your

xsl:apply-templates select="*"

should be select="."

you're going one step too deep in the hierarchy. select="*" means select="child::*".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 22nd, 2007, 05:40 PM
Authorized User
 
Join Date: Jan 2007
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks michael, the select="." works.

now im trying to move that template into a seperate XSLT file and call it through the use of the import statement, however im having trouble getting the same code to work correctly. it doesn't return any results. im trying this:

[u]ORIGINAL_FILE.xsl</u>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="TESTING_IMPORT.xsl"/>
  <xsl:template match="/">
    <table border="0" cellspacing="0" cellpadding="0">
      <xsl:for-each select="Collection/Content">
        <xsl:if test="Title!='Placeholder Title'">
          <xsl:apply-templates select="." mode="output" />
        </xsl:if>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

[u]TESTING_IMPORT.xsl</u>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="*" mode="output">
    <tr>
      <td valign="top">
        <img src="http://172.16.2.33/images/global/bullet.gif" style="padding: 8px 8px 0px 4px;" />
      </td>
      <td>
        <a>
          <xsl:attribute name="href">
            <xsl:choose>
              <xsl:when test="Type ='Assets' or Type = 8 ">
                javascript:void window.open(...blah...)
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="QuickLink"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <xsl:value-of select="Title"/>
        </a>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

does my X-PATH need to change somewhere to make the same code work correctly from an imported XSLT file?
thanks for your help!
steve






Similar Threads
Thread Thread Starter Forum Replies Last Post
Exclude Elements in Apply Templates mail4kaja XSLT 18 November 29th, 2008 12:09 PM
Apply-templates combined with with-param ? Smirre XSLT 6 November 11th, 2008 08:25 AM
xsl:apply-templates select problem harapraveen XSLT 2 October 22nd, 2007 08:05 AM
Templates Won't Apply neilac333 XSLT 5 October 26th, 2006 01:39 PM
Can I 'apply-templates' across multiple XML docume mphare XSLT 1 June 8th, 2006 05:12 PM





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