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 December 5th, 2007, 03:25 PM
Authorized User
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Looping with When Condition

Any Help is much appreciated.

E1EDL37 contains one of the field value as RFID_PALLET and RFID_CASE
When Pallet comes, i need to find the relative cases and write below to the pallet. This is working fine with the below logic. Case variable will contains those RFID_CASE numbers and search and write them below the pallet. If i have any number of pallets and correspondent cases, it is taking care of.

<xsl:for-each select="E1EDL37[VHILM='RFID_PALL']">
<xsl:copy-of select="." />
<xsl:for-each select="E1EDL44">
<xsl:variable name="Case">
<xsl:value-of select="EXIDV" />
</xsl:variable>
<xsl:copy-of select="../../E1EDL37[EXIDV=$Case]" />
</xsl:for-each>
</xsl:for-each>


But when it comes to only cases with out Pallet's, i am trying to implement the code here and having some troubles. I am new to XSLT, so any logical and syntax is much appreciated

What i am trying to here is
First Loop the E1EDl37 and see if there is any RFID_PALL exists, if yes set the variable value to YES. So that when i use xsl:when i should check for YES other wise write all the cases

I am having some syntax errors and i am not sure if the logic works fine. So can any of GURU can help me. I really appreciate any input here

<xsl:for-each select="E1EDL37[VHILM='RFID_PALL']">
<xsl:variable name="RFIDPALLET" select='YES'/>
</xsl:for-each>

<xsl:choose>
<xsl:when test='$RFIDPALLET="YES"'>
<xsl:for-each select="E1EDL37[VHILM='RFID_PALL']">
<xsl:copy-of select="." />
<xsl:for-each select="E1EDL44">
<xsl:variable name="Case">
<xsl:value-of select="EXIDV" />
</xsl:variable>
<xsl:copy-of select="../../E1EDL37[EXIDV=$Case]" />
</xsl:for-each>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="../../E1EDL37" />
</xsl:otherwise>
</xsl:choose>

 
Old December 5th, 2007, 03:31 PM
Authorized User
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is there a way i can call template for E1EDL37 segment, If yes please help me as well.

<xsl:for-each select="E1EDL37[VHILM='RFID_PALL']"> (((Need to call template for this))
<xsl:copy-of select="." />
<xsl:for-each select="E1EDL44">
<xsl:variable name="Case">
<xsl:value-of select="EXIDV" />
</xsl:variable>
<xsl:copy-of select="../../E1EDL37[EXIDV=$Case]" />(((Need to call template for this))
</xsl:for-each>
</xsl:for-each>

 
Old December 5th, 2007, 03:38 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:for-each select="E1EDL37[VHILM='RFID_PALL']">
  <xsl:variable name="RFIDPALLET" select='YES'/>
</xsl:for-each>

This doesn't work because the variable goes out of scope as soon as the xsl:for-each exits. You can set a boolean variable like this:

<xsl:variable name="RFIDPALLET" select="boolean(E1EDL37[VHILM='RFID_PALL'])"/>

and then test it using

xsl:when test="$RFIDPALLET"

But it would probably be better to have a variable containing the node-set itself:

<xsl:variable name="RFIDPALLET" select="E1EDL37[VHILM='RFID_PALL']"/>

and then do

<xsl:when test="$RFIDPALLET">
  <xsl:for-each select="$RFIDPALLET">

remembering that tests when applied to a node-set return true if the node-set is non-empty.

Incidentally, don't do this:

<xsl:variable name="Case">
  <xsl:value-of select="EXIDV" />
</xsl:variable>

when you mean

<xsl:variable name="Case" select="EXIDV" />

The latter form is a third the length and can be ten times faster, because it doesn't create any new nodes.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 5th, 2007, 03:46 PM
Authorized User
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I made those changes, still unable to get RFID_CASE when no pallet exists. Can you help me

<xsl:variable name="RFIDPALLET" select="boolean(E1EDL37[VHILM='RFID_PALL'])"/>
<xsl:choose>
<xsl:when test="$RFIDPALLET">
<xsl:for-each select="E1EDL37[VHILM='RFID_PALL']">
<xsl:copy-of select="." />
<xsl:for-each select="E1EDL44">
<xsl:variable name="Case" select="EXIDV" />
<xsl:copy-of select="../../E1EDL37[EXIDV=$Case]" />
</xsl:for-each>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="../../E1EDL37" />
</xsl:otherwise>
</xsl:choose>


 
Old December 5th, 2007, 03:55 PM
Authorized User
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Took the boolean out

I am new to XSLT, so far i made through reading the forums etc... I really appreciate your help Mike

    <xsl:choose>
        <xsl:when test"some condition">
            Execution statements...
        </xsl:when>
        <xsl:when test="some other condition">
            Execution statements...
        </xsl:when>
        <xsl:otherwise>
            Default statements...
        </xsl:otherwise>
    </xsl:choose>




<xsl:variable name="RFIDPALLET" select="E1EDL37[VHILM='RFID_PALL']"/>
<xsl:choose>
<xsl:when test="$RFIDPALLET">
<xsl:for-each select="E1EDL37[VHILM='RFID_PALL']">
<xsl:copy-of select="." />
<xsl:for-each select="E1EDL44">
<xsl:variable name="Case" select="EXIDV" />
<xsl:copy-of select="../../E1EDL37[EXIDV=$Case]" />
</xsl:for-each>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="../../E1EDL37" />
</xsl:otherwise>
</xsl:choose>


















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

I can't help you find the bugs in your code with only partial information. In particular, you haven't showed any examples of your source document structure, and you haven't explained what the context node for this fragment of XSLT is.

The most likely reason that the otherwise branch is showing nothing is that the path expression select="../../E1EDL37" is wrong. Intrinsically it seems slightly that the context node has a child called E1EDL37 and that its grandparent also has such a child, but without seeing the source, how can I tell?

Also, it make it much easier to spot bugs if you take the trouble to indent your source code properly.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 5th, 2007, 04:47 PM
Authorized User
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the code with indents

                            <xsl:variable name="RFIDPALLET" select="E1EDL37[VHILM='RFID_PALL']"/>
                            <xsl:choose>
                            <xsl:when test="$RFIDPALLET">
                                <xsl:for-each select="E1EDL37[VHILM='RFID_PALL']">
                                    <xsl:copy-of select="." />
                                    <xsl:for-each select="E1EDL44">
                                        <xsl:variable name="Case" select="EXIDV" />

                                      <xsl:copy-of select="../../E1EDL37[EXIDV=$Case]" />
                                    </xsl:for-each>
                                </xsl:for-each>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:copy-of select="../../E1EDL37" />
                            </xsl:otherwise>
                            </xsl:choose>

The only difference between these two inputs are taking out 2 E1EDL37 segements which belongs to RFID_PALL

Scenario 1 Input XML. Pallets and Cases
 
Old December 5th, 2007, 04:47 PM
Authorized User
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

N/A
 
Old December 5th, 2007, 04:48 PM
Authorized User
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

N/A
 
Old December 5th, 2007, 05:27 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's very hard to work out whether you have any conceptual misunderstandings here or whether you are just bogged down in the complexity of the data. I suspect it's somewhere in between. You should really be learning on something a bit easier. I don't think you've chosen a hard problem, just one with so much detail that small mistakes aren't easily diagnosed.

You haven't answered the question about what the context node is when this fragment of XSLT is executed. That might suggest that you haven't fully grasped the importance of context in XSLT programming. Let's assume that it's the parent node of these E1EDL37 nodes. If that's the case, then the variable RFIDPALLET selects the set of RFID_PALL nodes, the when clause fires because this set isn't empty, and it processes the RFID_PALL nodes.

Within the when clause you have

<xsl:copy-of select="../../E1EDL37[EXIDV=$Case]" />

Well, you haven't shown the parent of the E1EDL37 elements, let along the grandparent, so I have no idea what you're going to find when you go two levels up. I suspect nothing.

The otherwise clause would only fire if there were no RFID_PALL nodes. Perhaps you were expecting the otherwise clause to process the E1EDL37 elements that aren't RFID_PALL nodes? I can't tell. As written, it isn't going to be executed on this data, and if it were, I doubt it would do anything, because it's again doing ../../ which seems pretty meaningless.

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping with When Condition along with Variables pixelheart XSLT 1 December 6th, 2007 11:52 AM
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.