p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   for-each on result tree fragment (http://p2p.wrox.com/showthread.php?t=63974)

RoeZ November 22nd, 2007 03:55 AM

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



joefawcett November 22nd, 2007 04:37 AM

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)

mhkay November 22nd, 2007 04:55 AM

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

RoeZ November 22nd, 2007 05:05 AM

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


RoeZ November 22nd, 2007 05:10 AM

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.


joefawcett November 22nd, 2007 05:12 AM

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)

RoeZ November 22nd, 2007 05:29 AM

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

joefawcett November 22nd, 2007 05:57 AM

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

--

Joe (Microsoft MVP - XML)

RoeZ November 22nd, 2007 06:01 AM

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


joefawcett November 22nd, 2007 06:29 AM

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)


All times are GMT -4. The time now is 07:08 PM.

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