Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 22nd, 2007, 03:37 PM
Authorized User
 
Join Date: Jan 2007
Location: , , .
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 =)


Reply With Quote
  #2 (permalink)  
Old January 22nd, 2007, 04:42 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
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
Reply With Quote
  #3 (permalink)  
Old January 22nd, 2007, 05:40 PM
Authorized User
 
Join Date: Jan 2007
Location: , , .
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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:52 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.