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 October 28th, 2008, 12:07 PM
Authorized User
 
Join Date: Sep 2008
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default how to traverse and than print result

<?xml version="1.0"?>
<root>
    <details>
        <list>A</list>
        <categ/>
        <title>Title A1</title>
        <link>linkA1</link>
    </details>
    <details>
        <list>A</list>
        <categ/>
        <title>Title A2</title>
        <link>linkA2</link>
    </details>
    <details>
        <list>B</list>
        <categ/>
        <title>TitleB1</title>
        <link>linkB1</link>
    </details>
    <details>
        <list>B</list>
        <categ/>
        <title>Title B2</title>
        <link>linkB2</link>
    </details>
        <details>
        <list>C</list>
        <categ>fan</categ>
        <title>Title C1</title>
        <link>linkC1</link>
    </details>
        <details>
        <list>C</list>
        <categ>fan</categ>
        <title>Title C2</title>
        <link>linkC2</link>
    </details>
    </details>
        <details>
        <list>C</list>
        <categ>pen</categ>
        <title>Title C3</title>
        <link>linkC2</link>
    </details>
</root>

based on <title> I need to first check to see if <categ> exists if it does than I need to print all title based on the current <categ>
else I need to print all the <title> based on the <list> so for instance user provides Title A1 which results in printing out Title A1 and Title A2 since they both have
<list>A</list>. If the user provides Title C2 than it should only print Title C2 and Title C1 but not Title C3 since it has a seperate <categ> I need to know what the statement would be based on this requirement?

 
Old October 28th, 2008, 12:43 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your requirements statement is terribly hard to read, but it seems to be saying "Select the <details> with the given <title>. If there is a <categ>, select all <details> elements with the same <categ>. Otherwise, select all <details> elements with the same <list>."

That is

<xsl:variable name="d" select="/root/details[title=$inputTitle]"/>

then

select="/root/details[($d/categ != '' and $d/categ = categ)
        or ($d/categ = '' and $d/list = list]"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old October 28th, 2008, 12:44 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is an example stylesheet, you simply need to use xsl:choose/xsl:when to check:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="text"/>

  <xsl:param name="title"/>

  <xsl:template match="/">
    <xsl:variable name="item" select="root/details[title = $title]"/>
    <xsl:choose>
      <xsl:when test="$item">
        <xsl:choose>
          <xsl:when test="$item/categ/text()">
            <xsl:apply-templates select="root/details[categ = $item/categ]/title"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="root/details[list = $item/list]/title"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>No element found.</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="title">
    <xsl:value-of select="concat(., '#10;')"/>
  </xsl:template>

</xsl:stylesheet>
--
  Martin Honnen
  Microsoft MVP - XML
 
Old October 28th, 2008, 12:59 PM
Authorized User
 
Join Date: Sep 2008
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default

Im trying this and it doesnt seem to be working.
Am I doing this right?

    <xsl:variable name="d" select="/root/details[title='Title A1']"/>
<select="/root/details[($d/categ != '' and $d/categ = categ) or ($d/categ = '' and $d/list = list]"/>

 
Old October 28th, 2008, 01:15 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There's a missing right bracket, but I can't see anything else wrong with it.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
traverse the whole tree without knowing the node dipsut XSLT 3 June 17th, 2011 07:49 AM
C# to traverse through a ChechBoxList using LINQ Helios BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 4 September 26th, 2008 04:37 PM
How to traverse Directed Cyclic Graph talhas Java Basics 1 March 20th, 2008 05:33 AM
GridView and SQL More than one result = one result DarkForce ASP.NET 2.0 Basics 0 July 20th, 2007 04:29 AM
Using a Horizontal Scrollbar 2 traverse link list conundrum C# 0 January 13th, 2004 04:17 AM





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