Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old August 5th, 2005, 10:08 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default Simple XSLT Setup w/Strange Result

I'm not sure why this is happening, but here it goes.
I have a source XML doc, like this:
[code]<?xml version="1.0" encoding="UTF-8"?>
<mostrequested>
    <request>
        <mr_title>...</mr_title>
        <mr_url>...</mr_url>
        <mr_target>...</mr_target>
        <mr_display>yes</mr_display>
    </request>
</mostrequested>[code]

...and an XSLT stylesheet like this:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:param name="par_mr" select="document('mostrequested.xml')" />
<xsl:template match="/">[list]
    <xsl:for-each select="$par_mr">
        <xsl:if test="mostrequested/request/mr_display = 'yes'">
            <li><xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="mostrequested/request/mr_url" /></xsl:attribute><xsl:attribute name="target">_blank</xsl:attribute><xsl:value-of select="mostrequested/request/mr_title" /></xsl:element></li>
        </xsl:if>
    </xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
...which should result a list of elements where the node's <mr_display> value = "yes".

But instead, it's resulting in only the first element displaying. I've also tried this setup:
Code:
<xsl:for-each select="$par_mr[mr_display = 'yes'">
...to replace the <xsl:if> statement, but it worked exactly the same.

And finally, if I change it to be:
Code:
<xsl:for-each select="$par_mr">
    Most Requested: <xsl:value-of select="*" />
</xsl:for-each>
..all of the node data DOES display properly. So I know that it's pulling in the data correctly. It's just not displaying that node for all of the elements for some reason.

I've reviewed several online sources concerning what should be a simple setup, but I'm stumped as to why this is not working. If anyone can tell me what I'm doing wrong, that would be great. Thanks.

KWilliams
 
Old August 5th, 2005, 10:41 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The document function returns the root node so xsl:for-each will only find one node, the root.
If you want to process each request then use:
Code:
<xsl:for-each select="$par_mr/mostrequested/request">
Personally I'd have thought if you want all the requests where the mr_display equals "yes" then I'd do:
Code:
<xsl:apply-templaes select="$par_mr/mostrequested/request[mr_display = 'yes']"/>
and then catch them with:
Code:
<xsl:template match="request">



</xsl:template>
--

Joe (Microsoft MVP - XML)
 
Old August 5th, 2005, 11:18 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Joe,

You wrote:
Quote:
quote:The document function returns the root node so xsl:for-each will only find one node, the root.
If you want to process each request then use:
<xsl:for-each select="$par_mr/mostrequested/request">
Personally I'd have thought if you want all the requests where the mr_display equals "yes" then I'd do:
<xsl:apply-templaes select="$par_mr/mostrequested/request[mr_display = 'yes']"/>
and then catch them with:
<xsl:template match="request">

</xsl:template>
Thanks for the info, as I was able to create this working solution:
Code:
[list]
<xsl:for-each select="$par_mr/mostrequested/request">
    <xsl:if test="mr_display = 'yes'">
    <li><xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="mr_url" /></xsl:attribute><xsl:attribute name="target">_blank</xsl:attribute><xsl:value-of select="mr_title" /></xsl:element></li>
    </xsl:if>
</xsl:for-each>
</ul>
Concerning the other option that you mentioned using <xsl:apply-templates, what would be the benefit of using that method vs. what I'm using? To use that method with other code within my stylesheet, I'd have to do this:
<xsl:template match="/">

</xsl:template>
<xsl:template match="request">
...
</xsl:template>
<xsl:template match="/">

</xsl:template>

Wouldn't this setup be more complicated than what I'm using. Thanks for your help and advice on this subject.

KWilliams
 
Old August 5th, 2005, 11:20 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Firstly, why do you do this:

<xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="mostrequested/request/mr_url" /></xsl:attribute><xsl:attribute name="target">_blank</xsl:attribute><xsl:value-of select="mostrequested/request/mr_title" /></xsl:element>

when you could do this:

<a href="{mostrequested/request/mr_url}" target="_blank">
  <xsl:value-of select="mostrequested/request/mr_title" />
</a>

Secondly, document() selects one node, so your for-each loop is going to be executed exactly once. I'm not sure what you wanted to happen, so I can't correct it for you.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 5th, 2005, 11:36 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Firstly, why do you do this:
<xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="mostrequested/request/mr_url" /></xsl:attribute><xsl:attribute name="target">_blank</xsl:attribute><xsl:value-of select="mostrequested/request/mr_title" /></xsl:element>

when you could do this:

<a href="{mostrequested/request/mr_url}" target="_blank">
  <xsl:value-of select="mostrequested/request/mr_title" />
</a>
Good point. I'll change that.

Quote:
quote:Secondly, document() selects one node, so your for-each loop is going to be executed exactly once. I'm not sure what you wanted to happen, so I can't correct it for you.
Actually, with joefawcett's help, I was able to create this set of code that pull all results using the document() method:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:param name="par_mr" select="document('mostrequested.xml')" />
<xsl:template match="/">[list]
<xsl:for-each select="$par_mr/mostrequested/request">
    <xsl:if test="mr_display = 'yes'">
        <li><xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="mr_url" /></xsl:attribute><xsl:attribute name="target">_blank</xsl:attribute><xsl:value-of select="mr_title" /></xsl:element></li>
    </xsl:if>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
And the resulting XHTML is:[list]
<li><a href="URL" target="_blank">LINKTITLE1</a></li>
<li><a href="URL2" target="_blank">LINKTITLE2</a></li>
<li><a href="URL3" target="_blank">LINKTITLE3</a></li>
<li><a href="URL4" target="_blank">LINKTITLE4</a></li>
<li><a href="URL5" target="_blank">LINKTITLE5</a></li>
</ul>

From this XML doc:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<mostrequested>
    <request>
        <mr_title>LINKTITLE1</mr_title>
        <mr_url>URL1</mr_url>
        <mr_display>yes</mr_display>
    </request>
    <request>
        <mr_title>LINKTITLE2</mr_title>
        <mr_url>URL2</mr_url>
        <mr_display>yes</mr_display>
    </request>
    <request>
        <mr_title>LINKTITLE3</mr_title>
        <mr_url>URL3</mr_url>
        <mr_display>yes</mr_display>
    </request>
    <request>
        <mr_title>LINKTITLE4</mr_title>
        <mr_url>URL4</mr_url>
        <mr_display>yes</mr_display>
    </request>
    <request>
        <mr_title>LINKTITLE5</mr_title>
        <mr_url>URL5</mr_url>
        <mr_display>yes</mr_display>
    </request>
</mostrequested>
KWilliams
 
Old August 5th, 2005, 11:47 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

I just tried your simpler link method of:
Code:
<a href="{mostrequested/request/mr_url}" target="_blank">
  <xsl:value-of select="mostrequested/request/mr_title" />
</a>
..but no results were displayed. And when I use my method on that same page, they are displayed. I'm not sure why, but thanks again for your input.

KWilliams





Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange Problem - Missing of Formula Field Result r_taduri Crystal Reports 0 July 9th, 2006 05:51 AM
Strange mapping result in orchestration in Biz2004 egotronic Biztalk 1 April 12th, 2006 07:31 AM
Strange Behavior With Anchor in XSLT kwilliams XSLT 6 July 21st, 2005 01:52 PM
Strange XSLT problem... dakkar XSLT 5 May 26th, 2005 06:37 AM
XSLT: unexpected result saurabh0 XSLT 4 November 2nd, 2004 01:21 PM





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