Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 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
  #1 (permalink)  
Old October 3rd, 2006, 09:40 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT n00b question

Hi,

Im just beginning to learn XSLT from w3schools' pages and I ran into this difficulty:

my XML looks like this
<data>
    <option value="76">Accounting</option>
    <option value="324">Acoustics</option>
    <option value="87">Actuarial Science</option>
</data>

I would like to list all the values of the <option> tags.
This XSLT just repeats the first value multiple times

<xsl:template match="/">
  <html>
  <body>
    <h2>options</h2>
    <xsl:for-each select="data/option">
      <p><xsl:value-of select="/data/option"/></p>
    </xsl:for-each>
  </body>
  </html>
</xsl:template>

what should I change to get
  Accounting
  Acoustics
  Actuarial Science

instead of
  Accounting
  Accounting
  Accounting

Thanks in advance

  #2 (permalink)  
Old October 3rd, 2006, 09:45 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Never mind I figured it out for myself within minutes of making this posting.
Sorry for the noise.

In case somebody else is wondering, the correct line is
<p><xsl:value-of select="."/></p>

  #3 (permalink)  
Old October 3rd, 2006, 09:52 AM
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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

w3schools, I find, is great for quick reference to the details, but not strong on teaching the underlying concepts. There are three things you need to know here:

(a) xsl:for-each establishes a current node. Within the for-each, you will normally make selections relative to the current node. You can refer to the current node as ".", so for example <xsl:value-of select="."/> gives you the string value of the current node.

(b) a path expression starting with "/" selects from the root of the document. Using it inside a for-each will therefore give you the same selection each time.

(c) In XSLT 1.0, if <xsl:value-of select=..."/> selects multiple nodes, then only the first one is displayed. That's "Accounting" in your case.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #4 (permalink)  
Old October 3rd, 2006, 10:09 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the insight Michael.

How would I do the same without using "xsl:for-each" like you mentioned on a different thread?

<xsl:template match="/data/option">
  <html>
  <body>
        <h2>Options</h2>
              <xsl:value-of select="."/><br/>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

doesn't seem to do it..


  #5 (permalink)  
Old October 3rd, 2006, 10:23 AM
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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

That code is fine, so long as there's an apply-templates somewhere that actually selects the /data/option element for processing.

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
XSLT Question amhicraig XSLT 1 December 6th, 2007 05:06 AM
xslt basic question kgoldvas XSLT 2 January 25th, 2006 02:48 PM
simple XSLT question _thinking XSLT 4 January 24th, 2006 10:41 AM
basic xslt Question abhishek verma XSLT 4 January 24th, 2006 05:35 AM
3 asp.net n00b question, please help ^_^;;; CSharp1314 Classic ASP Basics 5 May 7th, 2004 02:08 AM





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