 |
| 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
|
|
|
|

April 13th, 2010, 04:41 PM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
Code To Use the Position Function
Hello.
I'll first show my XML code and will then place my question.
<RCDATALINE>
<TRC>90</TRC>
<OVERRIDE>Y</OVERRIDE>
</RCDATALINE>
<RCDATALINE>
<TRC>90</TRC>
<OVERRIDE>N</OVERRIDE>
</RCDATALINE>
<RCDATALINE>
<TRC>90</TRC>
<OVERRIDE>N</OVERRIDE>
</RCDATALINE>
I need to print the <TRC> value but only if it hasn't been printed before.
Also, if the OVERRIDE> value is Y, do not print <TRC>.
So, with the above XML, only the second <TRC> would print.
Within a for-each loop, I use preceding-sibling. My problem is that when I find a match for <TRC> in a previous sibling, that occurrence of the <OVERRIDE> needs to be N.
Here's the code that I currently have that needs to have to position() logic added to it.
<xsl:for-each select "RCDATALINE">
<xsl:when test="TRC[.!=''">
<xsl:if test="not (TRC[.=parent::*/preceding-sibling::*/TRC])
and OVERRIDE(position())[.!='Y']"> !!!!!HELP NEEDED HERE
<xsl:value-of select="TRC"/>
</xsl:if>
</xsl:when>
</xsl:for-each>
Any suggestions will be greatly appreciated!
Thanks,
Rita
|
|

April 13th, 2010, 05:55 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>I need to print the <TRC> value but only if it hasn't been printed before.
that is, if there is no preceding-sibling with the same TRC value and with OVERRIDE=N; or in XPath:
test="not(preceding-sibling::*[TRC = current()/TRC and OVERRIDE='N'])"
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

April 13th, 2010, 06:16 PM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
Thanks Michael for your quick response.
I tried the code per your example and this time I am getting the <TRC> printing out (before it was not) but it's coming out twice.
I'll play around with it to see if I can get it to work.
-- Rita
|
|

April 13th, 2010, 07:30 PM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
FYI
I tried several combinations and the following one worked for me:
<xsl:if test="not(TRC[.=parent::*/preceding-sibling::*/TRC] and OVERRIDE='')">.
I now have to create various scenarios and hopefully they will work as required 
|
|

April 14th, 2010, 12:01 PM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
Not Working Under Different Conditions
My previous example of code worked for the sample XML code that I had in my initial question. However, for the following example, I get 90 twice when it should only display once.
The difference between this code and the original example is in the second <RCDATALINE>, <OVERRIDE> is Y whereas the original one is N.
NOTE: <TRC> must only print if <OVERRIDE> = N.
<RCDATALINE>
<TRC>90</TRC> -------------------- Should not print as <OVERRIDE> is Y
<OVERRIDE>Y</OVERRIDE>
</RCDATALINE>
<RCDATALINE>
<TRC>90</TRC> -------------------- Should not print as <OVERRIDE> is Y
<OVERRIDE>Y</OVERRIDE>
</RCDATALINE>
<RCDATALINE>
<TRC>90</TRC> -------------------- Should print as <OVERRIDE> is N and there is no previous 90 printed
<OVERRIDE>N</OVERRIDE>
</RCDATALINE>
<RCDATALINE>
<TRC>90</TRC> -------------------- Should not print as 90 from previous <TRC> already printed
<OVERRIDE>N</OVERRIDE>
</RCDATALINE>
So, the following line of code does not give me the required results.
<xsl:if test="not(TRC[.=parent::*/preceding-sibling::*/TRC] and OVERRIDE='')">.
I'm wondering if I should go the way of variables?!
Thanks,
Rita
Last edited by ritagr; April 14th, 2010 at 12:15 PM..
|
|

April 14th, 2010, 12:55 PM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
Working Again
Well, after further playing around and using Michael's original suggestion I have some of my current test variations all working - except for the example above!!. Here's what's currently working for most of my examples (I added an OVERRIDE test to the initial test):
<xsl:when test="TRC[.!=''] and OVERRIDE[.!='Y']">
<xsl:if test="not(preceding-sibling::*[TRC = current()/TRC and OVERRIDE=''])">
<xsl:value-of select="TRC"/>
Last edited by ritagr; April 14th, 2010 at 01:12 PM..
|
|

April 14th, 2010, 01:25 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
I can't see why you are testing OVERRIDE="" (empty string) when in all your examples OVERRIDE is either Y or N.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

April 14th, 2010, 01:30 PM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
Oops - sorry.
It should be OVERRIDE='N'.
<xsl:when test="TRC[.!=''] and OVERRIDE[.!='Y']">
<xsl:if test="not(preceding-sibling::*[TRC = current()/TRC and OVERRIDE='N'])">
<xsl:value-of select="TRC"/>
|
|
 |