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 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


  #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)
  #3 (permalink)  
Old November 22nd, 2007, 04:55 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

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
  #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

  #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.

  #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)
  #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
  #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)
  #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 />
  #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)




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





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