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 March 11th, 2008, 01:49 AM
Registered User
 
Join Date: Feb 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Comparing with blank value using xsl:if

Hi

My xml is as follows
<?xml version="1.0" encoding="UTF-8"?>
<Rels>
<Rel>
<RelName>Report Causes</RelName>
<CPName>Common Core </CPName>
<DestName>Dst</DestName>
<UserID>JPM9216</UserID>
<AttrName>Position</AttrName>
<AttrValue>ALL</AttrValue>
<Version>P</Version>
</Rel>
<Rel>
<RelName>Causes Mainte</RelName>
<CPName>link update</CPName>
<DestName>ge_Number</DestName>
<UserID>AC2793</UserID>
<AttrName></AttrName>
<AttrValue></AttrValue>
<Version></Version>
</Rel>
</Rels>


My XSLT is as follows

<?xml version='1.0' encoding='utf-8' ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<!--
Step 1: Define the primary key to be used in the Muenchian grouping. :-->
<xsl:key name="Rels" match="Rel" use="RelName"/>

<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="Rels">
    <h3 > History</h3>

    <xsl:for-each select="//Rel[generate-id(.)=generate-id(key('Rels',RelName))]">

           <center ><h4><xsl:value-of select="RelName"/> </h4></center>

          <table border="1" width='100%' >
              <tr>
                <th bgcolor='#0033CC'><b>Name</b></th>
                <th bgcolor='#0033CC'><b>Destination Name</b></th>
                <th bgcolor='#0033CC'><b>User Name</b></th>
                <th bgcolor='#0033CC'><b>Attributes</b></th>
                  <th bgcolor='#0033CC'><b>Version</b></th>
        </tr>

             <xsl:for-each select="key('Rels',RelName)">
             <tr bgcolor='BIEGE'>
                    <td width="20%" ><xsl:value-of select="CPName"/></td>
                    <td width="20%" ><xsl:value-of select="DestName"/></td>
                    <td width="20%" ><xsl:value-of select="UserID"/></td>
                    <xsl:variable name="test" ><xsl:value-of select="AttrName"/> </xsl:variable>
                    <xsl:if test="$test = ' ' ">
                        <td width="20%" >No Value</td>
                    </xsl:if>
                    <xsl:if test="$test != ' ' "><td width="20%" ><xsl:value-of select="$test"/></td> </xsl:if>
                    <td width="20%" ><xsl:value-of select="Version"/></td>
            </tr>
            </xsl:for-each> </table>
        <br/><br/>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>



if the value in node AttrName is blank i want to display"No value" string in table else it should display the value in that node, but my xsl:if part is not working. for all it shows only blank rows :(
Plz help.
 
Old March 11th, 2008, 04:00 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I would do something like:
Code:
<xsl:variable name="attrName" >
              <xsl:choose>
                <xsl:when test="normalize-space(AttrName) != ''">
                  <xsl:value-of select="AttrName"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:text>No Value</xsl:text>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <td width="20%" >
              
                <xsl:value-of select="$attrName"/>
              
            </td>

Your effort to group also looks a bit suspect, I think you need to change this:
Code:
<xsl:for-each select="Rel[generate-id(.)=generate-id(key('Rels',RelName))]">
to this:
Code:
<xsl:for-each select="Rel[generate-id(.)=generate-id(key('Rels',RelName)[1])]">
The full XSLT:
Code:
<?xml version='1.0' encoding='utf-8' ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">
  <xsl:output method="html"/>

  <xsl:key name="Rels" match="Rel" use="RelName"/>

  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="Rels">
    <h3 > History</h3>

    <xsl:for-each select="Rel[generate-id(.)=generate-id(key('Rels',RelName)[1])]">

      <center >
        <h4>
          <xsl:value-of select="RelName"/>
        </h4>
      </center>

      <table border="1" width='100%' >
        <tr>
          <th bgcolor='#0033CC'>

              <b>Name</b>

          </th>
          <th bgcolor='#0033CC'>

              <b>Destination Name</b>

          </th>
          <th bgcolor='#0033CC'>

              <b>User Name</b>

          </th>
          <th bgcolor='#0033CC'>

              <b>Attributes</b>

          </th>
          <th bgcolor='#0033CC'>

              <b>Version</b>

          </th>
        </tr>

        <xsl:for-each select="key('Rels',RelName)">
          <tr bgcolor='BIEGE'>
            <td width="20%" >

                <xsl:value-of select="CPName"/>

            </td>
            <td width="20%" >

                <xsl:value-of select="DestName"/>

            </td>
            <td width="20%" >

                <xsl:value-of select="UserID"/>

            </td>
            <xsl:variable name="attrName" >
              <xsl:choose>
                <xsl:when test="normalize-space(AttrName) != ''">
                  <xsl:value-of select="AttrName"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:text>No Value</xsl:text>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <td width="20%" >

                <xsl:value-of select="$attrName"/>

            </td>
            <td width="20%" >

                <xsl:value-of select="Version"/>

            </td>
          </tr>
        </xsl:for-each>
      </table>
      <br/>
      <br/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
As it happens both work as you only have two distinct entries in your XML. Perhaps look at the examples here: http://www.jenitennison.com/xslt/gro...muenchian.html

--

Joe (Microsoft MVP - XML)
 
Old March 11th, 2008, 04:01 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Let's look at the variable declaration:

<xsl:variable name="test" >
  <xsl:value-of select="AttrName"/>
</xsl:variable>

This is a very convoluted way of writing:

<xsl:variable name="test" select="AttrName" />

What you want is a string, but instead of defining the variable as a string you are evaluating the string, putting the result into a text node, attaching that text node to a document node, and then computing the string value of the document node every time you reference the variable. If you don't care about performance and if you are bonused on the number of lines of code you write, this is fine, but otherwise it's horrible. Can't blame you though, it's an incredibly common blunder: yours is the second post this morning on this forum to do it.

Now your test. In your sample data you have <AttrName></AttrName>. The string value of that element is a zero-length string. But your test is

<xsl:if test="$test = ' ' ">

which tests for a string containing a single space. You need

<xsl:if test="$test = '' ">

Or if you want to test for all strings containing no characters other than whitespace, use

<xsl:if test="not(normalize-space($test))">

Of course you could also do this in the variable:

<xsl:variable name="test" select="normalize-space(AttrName)" />



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 11th, 2008, 07:16 AM
Registered User
 
Join Date: Feb 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot for your valuable suggestions.
That is really helping in my work.





Similar Threads
Thread Thread Starter Forum Replies Last Post
copying the value of xsl in variable and comparing amhicraig XSLT 1 December 4th, 2007 07:58 PM
Pass link values as xsl:parameter to php5 then xsl pauljr8 XSLT 1 July 2nd, 2007 10:32 PM
Comparing two variables by using = operator in xsl saurabh_inblore XSLT 1 February 2nd, 2007 05:22 AM
xsl advise needed - Replacing UID through XSL Billyl XSLT 6 February 28th, 2006 05:46 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





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