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 1st, 2006, 08:54 PM
Registered User
 
Join Date: Mar 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Finding values from duplicated nodes

In the following XML example:

<ClientData>
<DTM>
        <DTM01>232</DTM01>
    <DTM02>20091226</DTM02>
</DTM>
<DTM>
    <DTM01>233</DTM01>
    <DTM02>20091231</DTM02>
</DTM>
<SVC>
    <SVC01>HC:XX</SVC01>
    <SVC02>224</SVC02>
    <SVC03>0</SVC03>
    <SVC04>0000</SVC04>
    <SVC05>16</SVC05>
</SVC>
<DTM>
    <DTM01>150</DTM01>
    <DTM02>20041226</DTM02>
</DTM>
<DTM>
    <DTM01>151</DTM01>
    <DTM02>20041231</DTM02>
</DTM>
<CAS>
    <CAS01>CO</CAS01>
    <CAS02>8</CAS02>
    <CAS03>224</CAS03>
    <CAS04>0</CAS04>
</CAS>
</ClientData>

I am looking for the DTM02 value where the proceeding DTM01 value is 150 and then the DTM02 value where the preceeding DTM01 value is 151. However, when looping though all the parent nodes proceeding these nodes:

<xsl:for-each select="ClientData">

I can only retrieve the DTM02 value from the first set of DTM nodes where the DTM01 value is 232 (see the top of my example). I get that value twice, using either choose or if:
<xsl:choose>
   <xsl:when test="DTM/DTM01 = '150'">
     <xsl:value-of select="substring(DTM/DTM02,5,2)"/>/<xsl:value-of select="substring(DTM/DTM02,7,2)"/>/<xsl:value-of select="substring(DTM/DTM02,1,4)"/>

or an if:

<xsl:if test="DTM/DTM01 = '151'">
  <xsl:value-of select="substring(DTM/DTM02,5,2)"/>/<xsl:value-of select="substring(DTM/DTM02,7,2)"/>/<xsl:value-of select="substring(DTM/DTM02,1,4)"/>
</xsl:if>

What is the best way to do this?
 
Old March 2nd, 2006, 05:21 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

First understand why your code doesn't work:

<xsl:when test="DTM/DTM01 = '150'">

returns true if the context node has a child DTM element that has a child DTM01 element whose string value is "150"

<xsl:value-of select="substring(DTM/DTM02,5,2)"/>

selects all the DTM02 children of all the DTM children of the context node, then takes the string value of the first node in this node-set (in document order). (In XPath 2.0 this will give you an error if there's more than one node in the node-set, to catch people making this common mistake).

You're making an assumption that the "DTM" in the second expression somehow means the particular "DTM" that you matched in the first expression, and of course it doesn't.

The way to do this kind of thing is with a single expression:

DTM[DTM01="150"]/DTM02

this selects all the DTM elements children of the context node with a child DTM01 element equal to "150", and then selects the DTM02 children of these selected DTM elements.

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
Compare values in different nodes fann XSLT 9 September 11th, 2008 06:25 AM
Finding identical values voskoue Access VBA 7 January 30th, 2007 08:43 AM
Get distinct values on *second* grouping of nodes. MissHenesy XSLT 4 October 23rd, 2006 10:34 AM
Finding Values and counting scattered through data Scootterp Access VBA 13 March 20th, 2006 03:54 PM
XPath not finding nodes, need help Alderian72 XSLT 1 June 16th, 2005 10:49 AM





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