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 June 12th, 2003, 09:32 AM
Registered User
 
Join Date: Jun 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Predicate not being checked properly

Hi,

I've been having this problem for a couple of weeks now. I have a Predicate (conditional parameter) that is not checked properly by the XSLT. I've compiled with both Xalan and Saxon and always get the same problem.

Basicaly, I have an XML containing many Users. Each time I recurse, I get a different User (stored in the 'CurrentEmployee' parameter). For that User, I calculate a value depending if he's been hired on the current month or not (ie, if the 'HiredWorkDays' field is present in XML for that User).

The thing is that I still find that Value'HiredWorkDays' even if doesn't exist for that User!

In the example below, I only have 2 users - both XML and XSLT are stripped down to the bare minimum - and the code still doesn't work.

(Note: I'm not using XSL:For-Each because I need to recurse over a Summation Parameter and make particular calculations depending on who the user is).

Thanks for any help provided,

JF

Normally, my result should show:
-----------------------------------------------------
dgs (Daniel Gelinas) worked days
tpahls (Terry Palhs) worked 12 days
End of loop.
-----------------------------------------------------

Yet, it shows:
-----------------------------------------------------
dgs (Daniel Gelinas) worked 12 days
tpahls (Terry Palhs) worked 12 days
End of loop.
-----------------------------------------------------

Here's the XSLT:
-----------------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<head><title>Test</title></head>
<body>
    <xsl:call-template name="WorkableHoursTotal" />
</body>
</html>
</xsl:template>

<xsl:template name="WorkableHoursTotal">
    <xsl:param name="CurrentMonth" select="1" />
    <xsl:param name="EmployeePile" select="''" />
    <xsl:param name="CurrentEmployee" select="//Resources/ProjectRole/User/UserName[not(preceding::UserName = .)][not(contains($EmployeePile,.))]" />
    <xsl:choose>
        <xsl:when test="$CurrentEmployee != ''">

            <xsl:value-of select="$CurrentEmployee" /> worked <xsl:value-of select="//Resources/ProjectRole/User/HiredWorkDays[preceding-sibling::UserName[1] = $CurrentEmployee]" /> days<br />
            <xsl:call-template name="WorkableHoursTotal">
                <xsl:with-param name="CurrentMonth" select="$CurrentMonth" />
                <xsl:with-param name="EmployeePile" select="concat($EmployeePile,$CurrentEmployee)" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            End of loop.
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>
-----------------------------------------------------

And here's my XML
-----------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<Organization>
  <Org>
    <UDL>NAT-ENT-SYS-601</UDL>
    <Project>
      <Resources>
        <ProjectRole>
          <User>
            <UserName><![CDATA[dgs (Daniel Gelinas)]]></UserName>
            <FirstName><![CDATA[Daniel]]></FirstName>
            <LastName><![CDATA[Gelinas]]></LastName>
            <Phone></Phone>
            <Email></Email>
            <Parameters>
              <Name><![CDATA[HIRINGDATE]]></Name>
            </Parameters>
          </User>
        </ProjectRole>
      </Resources>
      <Task>
        <Resources>
          <ProjectRole>
            <User>
              <UserName><![CDATA[tpahls (Terry Palhs)]]></UserName>
              <FirstName><![CDATA[Terry]]></FirstName>
              <LastName><![CDATA[Palhs]]></LastName>
              <Phone></Phone>
              <Email></Email>
              <HiredWorkDays>12</HiredWorkDays>
              <Parameters>
                <Name><![CDATA[HIRINGDATE]]></Name>
                <Value>2003-5-15T0:0:0</Value>
              </Parameters>
            </User>
          </ProjectRole>
        </Resources>
      </Task>
    </Project>
  </Org>
</Organization>
-----------------------------------------------------
 
Old June 14th, 2003, 04:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Hi,
The reason of misfunctionality of the line:

<xsl:value-of select="$CurrentEmployee" /> worked <xsl:value-of select="//Resources/ProjectRole/User/HiredWorkDays[preceding-sibling::UserName[1] = $CurrentEmployee]" /> days<br />

is the following(both Xalan and Saxon behave properly):
In the first recursive step the variable $CurrentEmployee has a nodeset as its value which contains 2 UserName nodes, since $EmployeePile is empty yet. So, xsl:value-of outputs the first node's value(as stated in XSLT's spec for xsl:value-of when 'select' attribute's value is a nodeset). And when you want to output the 'days' part(the equality predicate is tested), the processor takes all HiredWorkDays elements which are in your XML file, and here the equality operator makes the trick: you are comparing 2 nodesets; the first nodeset contains only the second UserName element(the path //Resources/ProjectRole/User/HiredWorkDays takes all HiredWorkDays elements in your XML, and hence in the predicate each one will became a context node, and preceding-sibling::UserName[1] will contain the second UserName element in document order) and the second nodeset is contained in $CurrentEmployee which is a set of all UserName nodes.
The equality predicate for nodeset operands returns true iff there is at least one pair of nodes from that 2 nodesets with matching string values! This holds in this case, since the second UserName element(in document order) is both in your left and right operands of the equality operator. That's why the stylesheet outputs "12 days" in the first step of the recursion.

I changed the template "WorkableHoursTotal", so it now works(other parts are the same). Here the position() function doesn't work, since the nodeset $SetOfEmployees contains nodes which are not siblings, that's why I use $NewPosition parameter.

    <xsl:template name="WorkableHoursTotal">
        <xsl:param name="CurrentMonth" select="1" />
        <xsl:param name="SetOfEmployees" select="//Resources/ProjectRole/User/UserName[not(preceding::UserName = .)]" />
        <xsl:param name="NewPosition" select="1"/>
        <xsl:choose>
            <xsl:when test="not($NewPosition &gt; count($SetOfEmployees))">
                <xsl:variable name="CurrentEmployee" select="$SetOfEmployees[$NewPosition]"/>
                <xsl:value-of select="$CurrentEmployee" /> worked <xsl:value-of select="$CurrentEmployee/../HiredWorkDays" /> days<br />
                <xsl:call-template name="WorkableHoursTotal">
                    <xsl:with-param name="CurrentMonth" select="$CurrentMonth" />
                    <xsl:with-param name="NewPosition" select="$NewPosition + 1"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                End of loop.
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>


Regards,
Armen
 
Old June 16th, 2003, 09:04 AM
Registered User
 
Join Date: Jun 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks armmarti!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Complex predicate question dcheslow XSLT 4 February 25th, 2007 02:21 PM
A problem with predicate in XSLT1.0:( sureshk XSLT 1 March 8th, 2006 01:13 PM
SUM function with DISTINCT predicate saturdave SQL Server ASP 1 April 8th, 2004 10:44 PM
Predicate question pdamer XSLT 3 November 19th, 2003 11:30 AM





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