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 January 10th, 2007, 04:31 AM
Registered User
 
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default using for each and going back in the xml tree

Hello,

I am trying to go back in an xml tree within a for each loop and this is not giving the right output. Maybe someone can give me a clou of what i am doing wrong and how i can get the right output.

XML:
Code:
- <root>
  <pagetype /> 
  <name>AdvertisementManagementGrid</name> 
  <id>-1</id> 
  <pageid>178</pageid> 
  <detailpageid>179</detailpageid> 
- <data>
- <data>
  <row intID="17" LastChange="2007-01-10T08:54:39.560" intEmployerID="114" strTitle="Marketing Manager" intResultID="1" /> 
  <row intID="16" LastChange="2007-01-10T08:54:32.107" intEmployerID="114" strTitle="Webdesigner" intResultID="2" /> 
  <row intID="15" LastChange="2007-01-10T08:54:22.593" intEmployerID="114" strTitle="Cameraman" intResultID="3" /> 
  </data>
- <searchfields>
  <row intPrimaryKeyID="17" strSearchNode="/Root/Information/Category" strSearchValue="20" intLanguageID="0" /> 
  <row intPrimaryKeyID="16" strSearchNode="/Root/Information/Category" strSearchValue="10" intLanguageID="0" /> 
  <row intPrimaryKeyID="15" strSearchNode="/Root/Information/Category" strSearchValue="10" intLanguageID="0" /> 
  </searchfields>
  <pageindex>1</pageindex> 
  <recordcount>3</recordcount> 
  </data>
</root>

XSL code:

Code:
<xsl:for-each select="root/data/data/row/row">
        <xsl:variable name="Category" select="../../searchfields/row/@strSearchValue" />
        <xsl:variable name="rowPID" select="../../searchfields/row/@intPrimaryKeyID" />
        <xsl:variable name="rowID" select="@intID" />
        <tr>
          <td>
            <a>
              <xsl:attribute name="href">
                default.aspx?pageid=176&amp;id=<xsl:value-of select="@intID"/>
              </xsl:attribute>
              <xsl:value-of select="@strTitle"/>
            </a>
          </td>
          <td>
            <xsl:value-of select="$Category[$rowPID = $rowID]"/>
          </td>
          <td></td>
        </tr>
      </xsl:for-each>
This is the output from the above xslt:
Marketing Manager 20
Webdesigner 20
Cameraman 20

But the output has to be:

Marketing Manager 20
Webdesigner 10
Cameraman 10

Hope someone can help me!

Thanks

Smiter

 
Old January 10th, 2007, 05:12 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There's clearly something wrong with this expression

<xsl:value-of select="$Category[$rowPID = $rowID]"/>

because the value of the predicate doesn't depend on the context node.

But I can't see how you get your current output. There are six <row> elements in the input, so I would expect six <tr> elements in the output.

I would do something like this:

<xsl:key name="k" match="searchfields/row" use="intPrimaryKeyID"/>

then

<xsl:for-each select="/root/data/row">
  <tr>
    <td>
      <a href="default.aspx?pageid=176&amp;id={@intID}"/>
        <xsl:value-of select="@strTitle"/>
      </a>
    </td>
    <td>
      <xsl:value-of select="key('k', @intId)/@strSearchValue"/>
    </td>
  </tr>
</xsl:for-each>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 10th, 2007, 05:57 AM
Registered User
 
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your quick reply! It all works fine now thanks to your solution.

Smiter








Similar Threads
Thread Thread Starter Forum Replies Last Post
Flat XML to tree Dia XSLT 1 November 16th, 2006 05:11 AM
tree xml to flat xml whiteadi XSLT 2 November 14th, 2006 02:36 PM
Tree View and XML bmains Pro VB.NET 2002/2003 0 October 30th, 2003 12:46 PM





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