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 May 25th, 2011, 05:37 PM
Authorized User
 
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
Default Checking an Element Against Multiple Elements

Hello.

I have an XML file as follows:
Code:
<TRCINFO>
  <RCLINE>
    <RCCODE>ABC1</RCCODE>
  </RCLINE> 
  <RCLINE>
    <RCCODE>ABC2</RCCODE>
  </RCLINE> 
</TRCINFO>

<TRCINFO2>
  <RCLINE>
    <RCCODE>ABC2</RCCODE>
  </RCLINE> 
</TRCINFO2>
I only want to print "ABC2" in TRCINFO2/RCLINE/RCCODE if it's not equal to any TRCINFO/RCLINE/RCCODE values.

I tried the following code but it's not working.
[code]
<xsl:template match="TRCINFO2">
<xsl:if test="RCLINE/RCCODE=TRCINFO/descendant::RCLINE/RCCODE" >

Any suggestions will be greatly appreciated.

Thanks,
Rita
 
Old May 25th, 2011, 06:46 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You seem to be muddled about XPath navigation and context.

Try

Code:
<xsl:if test="not(RCLINE/RCCODE=../TRCINFO/RCLINE/RCCODE)" >
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old May 25th, 2011, 07:17 PM
Authorized User
 
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
Default Checking an Element Against Multiple Elements

Thanks Michael for your quick response.
Yes, at this point I'm definitely muddled! I'm relatively new to XSLT.

I tried your code but am still getting "ABC2" printing out.
I need it not to print out from the <TRCINFO2> template because "ABC2" will be printed out when I run the template for the <TRCINFO> element.

That's why I thought using "descendant::" it would find all the
/TRCINFO/RCLINE/RCCODE values (ABC1 and ABC2) so I could compare them to
/TRCINFO2/RCLINE/RCCODE (ABC2) and not print the
/TRCINFO2/RCLINE/RCCODE (ABC2) value since it was found in
/TRCINFO/RCLINE/RCCODE.

Thanks,
Rita
 
Old May 26th, 2011, 05:33 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You need to show a complete stylesheet and a complete source document, plus the output you are getting and the output that you want.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old May 26th, 2011, 10:44 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try the below:
Code:
<xsl:template match="root">
<xsl:apply-templates select="TRCINFO"/>
</xsl:template>
 
<xsl:template match="TRCINFO">
<xsl:variable name="rcccode_1" select="../TRCINFO2/RCLINE/RCCODE"/>
<xsl:if test="RCLINE/RCCODE[. != $rcccode_1]">
<xsl:value-of select="$rcccode_1"/>
</xsl:if>
</xsl:template>
I included a root element <root> to your xml input.
__________________
Rummy
 
Old May 26th, 2011, 01:34 PM
Authorized User
 
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
Default Checking an Element Against Multiple Elements

Thanks Rummy for your suggestion.

Your code works when there's just one TRCINFO/RCLINE/RCCODE.
However, when there are multiple TRCINFO/RCLINE/RCCODE lines, it does not work as it only checks the first occurrence of TRCINFO/RCLINE/RCCODE.
 
Old May 26th, 2011, 01:54 PM
Authorized User
 
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
Default

Hi Michael.

Here's the XML file (I've removed the non pertinent elements).
Code:
<?xml version='1.0'?><!DOCTYPE doc SYSTEM 'xml_med7_dtd_2.0.54.dtd'>
<doc>
    <EOB>
        <VERSION>2.0.54</VERSION>
        <TRCINFO>
            <RCHEADER>FL Reduction Explanations:</RCHEADER>
            <RCLINE>
                <RCCODE>FL 90</RCCODE>
                <RCEXPLANATION>
                    <LINE>Paid: no modification to the information</LINE>
                    <LINE>provided on the medical bill: payment made</LINE>
                    <LINE>pursuant to Florida Workers&apos; Compensation Health</LINE>
                    <LINE>Care Provider Reimbursement Manual.</LINE>
                </RCEXPLANATION>
            </RCLINE>
        </TRCINFO>
        <CRCSTATERCINFO>
            <RCLINE>
                <RCCODE>FL 92</RCCODE>
                <RCEXPLANATION>
                    <LINE>Paid: no modification to the information</LINE>
                    <LINE>provided on the medical bill: payment made</LINE>
                    <LINE>pursuant to Florida Workers&apos; Compensation</LINE>
                    <LINE>Reimbursement Manual for Hospitals.</LINE>
                </RCEXPLANATION>
            </RCLINE>
            <RCLINE>
                <RCCODE>FL 90</RCCODE>
                <RCEXPLANATION>
                    <LINE>Paid: no modification to the information</LINE>
                    <LINE>provided on the medical bill: payment made</LINE>
                    <LINE>pursuant to Florida Workers&apos; Compensation Health</LINE>
                    <LINE>Care Provider Reimbursement Manual.</LINE>
                </RCEXPLANATION>
            </RCLINE>
        </CRCSTATERCINFO>
    </EOB>
</doc>
Here's my XSLT code that processes the <CRCSTATERCINFO> element.
I've included Rummy's suggestion which only works if there is one TRCINFO/RCLINE/RCCODE. I've also left out the table formatting code.
Code:
      <xsl:if test="EOB/CRCSTATERCINFO/RCLINE[descendant::RCEXPLANATION/LINE!='']">
        <fo:block>
          <xsl:apply-templates select="EOB/CRCSTATERCINFO"/>
        </fo:block>
      </xsl:if>

  <xsl:template match="CRCSTATERCINFO">
      <fo:table-body>
        <xsl:for-each select="RCLINE">
          <xsl:variable name="trccodes" select="/doc/EOB/TRCINFO/RCLINE/RCCODE" />
           <xsl:if test="RCCODE[.!=$trccodes]"> 
             <xsl:value-of select="RCCODE"/>
Here's the current output.
FL 90 Paid: no modification to the information etc.
(from TRCINFO/RCLINE/RCEXPLANATION)
FL 92 Paid: no modification to the information etc.
(from CRCSTATERCINFO/RCLINE/RCEXPLANATION)
FL 90 Paid: no modification to the information etc.
(from CRCSTATERCINFO/RCLINE/RCEXPLANATION)

Here's what I need to output.
FL 90 Paid: no modification to the information etc.
(from TRCINFO/RCLINE/RCEXPLANATION)
FL 92 Paid: no modification to the information etc.
(from CRCSTATERCINFO/RCLINE/RCEXPLANATION)

Don't print the second "FL 90" line (from CRCSTATERCINFO/RCLINE/RCEXPLANATION) since it's the same as that from TRCINFO/RCLINE/RCEXPLANATION.

I'm not showing the code to print the TRCINFO/RCLINE/RCEXPLANATION code since it's pretty straight foreward and justs prints whateve is in the RCEXPLANATION lines.

Just a quick note - for the XML info supplied here, my code works but if there are two <TRCINFO> elements and the "FL 90" was in the second one, it does not work!

Thanks for all your help!

Rita

Last edited by ritagr; May 26th, 2011 at 02:04 PM..
 
Old May 26th, 2011, 06:50 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Don't use [.!=$trccodes], use [not(.=$trccodes)]. The first returns true if $trccodes contains a value that is not equal to "."; the second returns true if $trccodes does not contain a value that is equal to ".". The second is almost invariably what you want.

I haven't checked whether this is the only error in your code.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
ritagr (May 27th, 2011)
 
Old May 26th, 2011, 07:18 PM
Authorized User
 
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
Default

Wow! I made the change per your suggestion and it's now working!
Thanks so much for your help.





Similar Threads
Thread Thread Starter Forum Replies Last Post
<a> element in within block-level elements? steddie1 BOOK: Beginning Web Programming with HTML, XHTML, and CSS, 2nd Edition ISBN: 978-0-470-25931-3 2 March 3rd, 2009 04:43 AM
Optional element exists if has inner elements 2BOrNot2B XML 0 May 9th, 2008 02:11 PM
Checking for valid child elements. Chamkaur XSLT 0 January 25th, 2007 06:51 PM
Access specific Element and its Child elements! suersh79 XML 3 November 22nd, 2006 12:58 AM
putting <element> round group of same elements jefke XSLT 0 May 24th, 2004 10:18 AM





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