Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
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 November 22nd, 2007, 03:55 AM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default for-each on result tree fragment

I have this
Code:
<xsl:call-template name="select">
<xsl:with-param name="options">
<option><name>1</name><value>test1</value></option>
<option><name>2</name><value>test2</value></option>
</xsl:with-param>
</xsl:call-template>
i my select template i have:
Code:
<xsl:param name="options" />
<xsl:for-each select="exslt:node-set($options)/node()">
My question: how do i get the nodes called 'name' and 'value' in my for-each


Reply With Quote
  #2 (permalink)  
Old November 22nd, 2007, 04:37 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Code:
<xsl:template name="select">
    <xsl:param name="options" />
    <xsl:for-each select="exslt:node-set($options)/node()">
      <xsl:copy-of select="name"/>
      <xsl:copy-of select="value"/>
    </xsl:for-each>
  </xsl:template>
But I would use
Code:
exslt:node-set($options)/option">
or
Code:
exslt:node-set($options)/*">
in preference to node() as node() can pick up text nodes.
Alternatively embed the options into the stylesheet under the root element:
Code:
<my:options><option><name>1</name><value>test1</value></option>
<option><name>2</name><value>test2</value></option><my:options>
Then you can select them using
Code:
document('')/*/my:options/option
and dispense with exslt:node-set altogether. Or move to XSLT 2.0 :)

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #3 (permalink)  
Old November 22nd, 2007, 04:55 AM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 Posts
Default

Try

<xsl:for-each select="exslt:node-set($options)/option">
  <xx><xsl:value-of select="name"/></xx>
  <yy><xsl:value-of select="value"/></yy>
</xsl:for-each>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #4 (permalink)  
Old November 22nd, 2007, 05:05 AM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If i use /* instead of /node() how can i call the 'value' and the 'name' in the for-each then? i have tried to use
Code:
<xsl:value-of select="name"/>
but that won't work

Reply With Quote
  #5 (permalink)  
Old November 22nd, 2007, 05:10 AM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried michaels code but also no succes, this is what i get.
When i use exslt:node-set($options)/option i get 0 options in my selectlist, when i use exslt:node-set($options)/* or /node() i get the right amount of options, but no values / names.

Reply With Quote
  #6 (permalink)  
Old November 22nd, 2007, 05:12 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Can you show a failing example, it works fine with:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <data>
      <xsl:call-template name="select">
        <xsl:with-param name="options">
          <option>
            <name>1</name>
            <value>test1</value>
          </option>
          <option>
            <name>2</name>
            <value>test2</value>
          </option>
        </xsl:with-param>
      </xsl:call-template>
    </data>
  </xsl:template>

  <xsl:template name="select">
    <xsl:param name="options" />
    <xsl:for-each select="msxsl:node-set($options)/*">
      <xsl:copy-of select="name"/>
      <xsl:copy-of select="value"/>
    </xsl:for-each>
  </xsl:template>

(I'm using the XslCompiledTransform in .NET)

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #7 (permalink)  
Old November 22nd, 2007, 05:29 AM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I really don't understand what i am doing wrong, i get the right amount of options in the optionlist but copy-of select="name" is empty and also copy-of select="value" is empty. nor value-of or copy-of seems to work here. Btw i am using php5 with the built in xslt processor (i believe it s sablotron)

i now tried joe's example, i copied it EXACTLY, all i did different was instead of msxsl i used exslt in front of my node-set function, still same results, the right amount of options, no names / values visible
Reply With Quote
  #8 (permalink)  
Old November 22nd, 2007, 05:57 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

As I wrote earlier, post the stylesheet you are using.

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #9 (permalink)  
Old November 22nd, 2007, 06:01 AM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exslt="http://exslt.org/common"
    extension-element-prefixes="exslt"
>


<xsl:output
    indent="yes"
    method="xml"
    encoding="UTF-8"
    omit-xml-declaration="yes"
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
/>


<xsl:template match="/">
    <html>
        <head>
            <title>
                test
            </title>
        </head>
        <body>
      <xsl:call-template name="select">
        <xsl:with-param name="options">
          <option>
            <name>1</name>
            <value>test1</value>
          </option>
          <option>
            <name>2</name>
            <value>test2</value>
          </option>
        </xsl:with-param>
      </xsl:call-template>
        </body>
    </html>
</xsl:template>

<xsl:template name="select">
<xsl:param name="options" />
<xsl:for-each select="exslt:node-set($options)/*">
  <p><xsl:copy-of select="name"/></p>
  <b><xsl:copy-of select="value"/></b>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>
my result is :

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>
                test
            </title>
  </head>
  <body>
    <p></p>
    <b></b>

    <p></p>
    <b></b>
  </body>
</html>
<br />
Reply With Quote
  #10 (permalink)  
Old November 22nd, 2007, 06:29 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The good old default namespace strikes again.
Code:
<xsl:stylesheet    version="1.0" xmlns:x="http://www.w3.org/1999/xhtml"   xmlns="http://www.w3.org/1999/xhtml"    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    xmlns:exslt="http://exslt.org/common"  extension-element-prefixes="exslt">

  <xsl:output    indent="yes"    method="xml"    encoding="UTF-8"    omit-xml-declaration="yes"    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>                test            </title>
      </head>
      <body>
        <xsl:call-template name="select">
          <xsl:with-param name="options">
            <option>
              <name>1</name>
              <value>test1</value>
            </option>
            <option>
              <name>2</name>
              <value>test2</value>
            </option>
          </xsl:with-param>
        </xsl:call-template>
      </body>
    </html>
  </xsl:template>
  <xsl:template name="select">
    <xsl:param name="options" />
    <xsl:for-each select="exslt:node-set($options)/*">
      <p>
        <xsl:copy-of select="x:name"/>
      </p>
      <b>
        <xsl:copy-of select="x:value"/>
      </b>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
See first post in this forum category for more information on default namespaces if you need it.

I think you also want
Code:
exclude-result-prefixes="exslt"
rather than
Code:
extension-element-prefixes="exslt"
--

Joe (Microsoft MVP - XML)
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
how to exclude elements in the result tree output ntmt XSLT 0 May 25th, 2006 10:33 AM
Preventing entities in result-tree rudolf XSLT 2 April 24th, 2006 03:47 PM
Transforming a result tree with yet another Xslt ballo XSLT 2 March 7th, 2006 09:42 AM
un escape text to use as fragment brenchley XSLT 1 December 13th, 2005 10:25 AM



All times are GMT -4. The time now is 12:05 AM.


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