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 April 29th, 2007, 06:24 AM
Registered User
 
Join Date: Apr 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help with writing if condition

Hello all,
I am transforming my xml dokument (library.xml) via xsl to create a HTML table with first row containing columns : Category, Name of the book, Author. My document is sorted by Category.And the other rows are printed by for-each cycle,everything is OK except one thing:

My problem is that I am printing all rows, and don't want to print the attribut value of category every time, only once. I need to write "if condition" telling if actual element(book) with value of attribute Category has already occured (has been printed) in document. If not, then print the value (for ex. Sport, Education)in the other case, don't print it.
Here is a screenshot of what I mean : http://jendys.webz.cz/screen.jpg

I will be grategul if someone could help me. Thnx. Jendys

 
Old April 29th, 2007, 10:58 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

HI Jendy,

Welcome to the forum. Please provide a sample of the XML and your XSLT. This would make it easy for someone to answer your question.

Thanks
 
Old April 29th, 2007, 12:38 PM
Registered User
 
Join Date: Apr 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is my sample of library.xml :
<library>
            <book id="1" name="Fyzika" category="Hudebniny">
                <author>Jaroslava NeteÄ?n??</author>
                <publisher="adresa" place="NA" year="2000" ></publisher>
                <isbn>200399990000</isbn>
                <other new="NO" description="NA dfs"></other>
            </book>

            <book id="6" nazev="Matika" category="Hudebniny">
                <author>Petr Sladil</author>
                <publisher="Koldat" misto_vydani="NA" year="2000" ></punlisher>
                <isbn>2005</isbn>
                <other news="No" description="NA dfs"></other>
            </book>

</library>

And here is sample xsl:


<table width="70%" border="0" align="center">
    <tr bgcolor="#252836" valign="middle" height="30">

      <th align="center" style="color:#929AB6">Category</th>
      <th align="center" style="color:#929AB6">Name</th>
      <th align="center" style="color:#929AB6">Author</th>
    </tr>

<xsl:for-each select="library/book">
<xsl:sort select="@category"/>
  <tr>
  <th bgcolor="#4E5774" style="color:#AAB1C4">
         <xsl:choose>
         <xsl:when test="following-sibling::*/@category=library/@category">
          <h2 align="center" style="color:white">Something</h2>
        </xsl:when>
       <xsl:otherwise>
            <xsl:value-of select="@category"/><xsl:text> </xsl:text>
       </xsl:otherwise>
        </xsl:choose>
   </th>
    <xsl:choose>
         <xsl:when test="other/@news ='Yes'">
              <th bgcolor="#727E9E" align="center"><span style="color:white;font-style:normal" ><xsl:value-of select="@name"/><xsl:text></xsl:text></span></th>
              <th bgcolor="#727E9E" align="center"><span style="color:white;font-style:normal" ><xsl:value-of select="author"/></span></th>
           </xsl:when>
    <xsl:otherwise>
      <th bgcolor="#4E5774" align="center"><span style="color:white;font-style:normal" ><xsl:value-of select="@name"/><xsl:text></xsl:text></span></th>
      <th bgcolor="#4E5774" align="center"><span style="color:white;font-style:normal" ><xsl:value-of select="author"/></span></th>
    </xsl:otherwise>
        </xsl:choose>

        </tr>
</xsl:for-each>
</table>

As you can see i have been trying to solve it comparing following with actual value, but i learned that it is not possible

 
Old April 29th, 2007, 01:07 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Hi, You can use the position() function.

Code:
<xsl:choose>
    <xsl:when test="following-sibling::*/@category=library/@category">
        <h2 align="center" style="color:white">Something</h2>
            </xsl:when>
                <xsl:otherwise>
                    <xsl:if test="position() = 1">
                        <xsl:value-of select="@category"/>
                        <xsl:text> </xsl:text>
                    </xsl:if>
                </xsl:otherwise>
</xsl:choose>
 
Old April 29th, 2007, 01:13 PM
Registered User
 
Join Date: Apr 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, thank you very much! I was playing with the idea of using position, but I did not find so much information about that.But thanx anyway

 
Old April 29th, 2007, 01:46 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Here is a good site for XPATH and XSLT

http://www.w3schools.com/xpath/xpath...ns.asp#context



 
Old April 29th, 2007, 04:16 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Looks to me as if

<xsl:when test="following-sibling::*/@category=library/@category">

is wrong for four reasons:

(a) you only want to look at the immediately following sibling, which is following-sibling::*[1]/@category

(b) the path library/@category is wrong because (i) the library element doesn't have an @category attribute, and (ii) the context node in a book element which doesn't have a library child

(c) you don't want to do something different when the following sibling has the same value, you want to do something different when the preceding sibling doesn't have the same value.

So I think you want:

<xsl:if test="not(preceding-sibling::*[1]/@category=@category)">
  <xsl:value-of select="@category"/>
</xsl:if>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 30th, 2007, 01:46 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Alternatively you can treat it as a grouping problem. Group on category then for each category output the first book in that category in full then for the others the information except the category. There are links to examples of grouping in the first post in the XSLT forum.

--

Joe (Microsoft MVP - XML)
 
Old April 30th, 2007, 03:44 AM
Registered User
 
Join Date: Apr 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for replies. I was thinking of possibility to group, but i searched for simplified solution.I will try it. Thanx!

 
Old April 30th, 2007, 04:02 AM
Registered User
 
Join Date: Apr 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It works!
I also made a mistake in row */@category=library/@category"> there should be */@category=book/@category"> of course, because I am from Czech Republic and was rewriting the code to English :-)and made this mistake. Thanx to all!






Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT and the if condition thisby XSLT 7 April 3rd, 2008 06:02 PM
or condition kgoldvas XSLT 1 July 31st, 2007 03:44 AM
where condition...? mnr555 Crystal Reports 0 June 18th, 2006 05:32 PM
if then else condition mateenmohd Classic ASP Basics 0 April 16th, 2005 07:08 AM
Condition mateenmohd SQL Server 2000 6 May 6th, 2004 03:49 AM





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