Connecting Tech Pros Worldwide Forums | Help | Site Map

finding the correct segment

Newbie
 
Join Date: Feb 2008
Posts: 18
#1: Mar 18 '09
Hi,

I have the following:

Expand|Select|Wrap|Line Numbers
  1. <start>
  2.     <segment Id="AAA">
  3.         <element Id="id">1</element>
  4.         <element Id="seq">122</element>
  5.         <element Id="seq2" Composite="yes">
  6.             <subelement Sequence="1">57002122338772</subelement>
  7.        </element>
  8.    </segment>
  9.    <segment Id="BBB">
  10.       <element Id="id">5</element>
  11.       <element Id="BBB02" Composite="yes">
  12.           <subelement Sequence="1">AS12121212</subelement>
  13.           <subelement Sequence="2">SA</subelement>
  14.       </element>
  15.    </segment>
  16.    <segment Id="BBB">
  17.       <element Id="id">1</element>
  18.       <element Id="BBB02" Composite="yes">
  19.           <subelement Sequence="1">12121212</subelement>
  20.           <subelement Sequence="2">HS</subelement>
  21.       </element>
  22.    </segment>
  23.    <segment Id="XXX">
  24.       <element Id="XXX01">C</element>
  25.       <element Id="XXX02">35</element>
  26.       <element Id="XXX03" Composite="yes">
  27.          <subelement Sequence="1">090</subelement>
  28.          <subelement Sequence="3">91</subelement>
  29.       </element>
  30.    </segment>
  31.    <segment Id="XXX">
  32.       <element Id="XXX01">C</element>
  33.       <element Id="XXX02">98</element>
  34.       <element Id="XXX03" Composite="yes">
  35.         <subelement Sequence="1">36</subelement>
  36.         <subelement Sequence="3">91</subelement>
  37.       </element>
  38.   </segment>
  39. ...
  40. </start>
  41.  
This code is repeated maybe 10 times and is defined by the id in element 'id' in segment 'AAA'. So the next lines of code will be
[code]
Expand|Select|Wrap|Line Numbers
  1.     <segment Id="AAA">
  2.         <element Id="id">2</element>
  3.         ...
  4.  
Now I have the Id, and I need to get a value from the proceeding XXX-segment, i.e. if my id is 4, I need to find the AAA-segment with id=4 and then a value in the following XXX-segment. But I'm wondering how I do this most correctly. I can probably use the position-function, but I'm thinking there must be a better way. Unfortunately my xpath skills are pretty limited. Can someone give me a hint?

Moderator
 
Join Date: Mar 2006
Posts: 1,103
#2: Mar 18 '09

re: finding the correct segment


When you say following, you mean the Id of that segment is 1 + the value of this id? eg id=5 in your example?

Assuming you're in a segment node:
Expand|Select|Wrap|Line Numbers
  1. <xsl:variable name="segid" select="@Id"/>
  2. <xsl:variable name="elid" select="element[@Id='id']+1"/>
  3. <xsl:for-each select="../segment[@id = $segid][element[@Id='id'][.=$elid]]">
  4. ...
  5.  
Even if this isn't it, it should give you some sort of clue.
Newbie
 
Join Date: Feb 2008
Posts: 18
#3: Mar 18 '09

re: finding the correct segment


Ok. I mean if I have id=4, I first find the AAA-segment with id=4. Then my job will be to find the first XXX-segment with element id=XXX02's value set to 98 beneath it.

Imagine that the block I have shown in the thread is repeated within the start-tags, and only the values change. Ex. the id of the AAA-segment is incremented.

Hope I make myself clear :)
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#4: Mar 19 '09

re: finding the correct segment


I think I know what you mean now.
<xsl:for-each select="following-sibling::segment[@Id='XXX'][1]">
I don't understand why you wouldn't get the segment with value 35 though.

Another possibility:
Expand|Select|Wrap|Line Numbers
  1. <xsl:key name="previousAAA" match="segment" use="preceding-sibling::segment[@Id='AAA'][1]"/>
  2. ...
  3. while in aaa Segment:
  4.  
  5. <xsl:for-each select="key('previousAAA', .)[@Id='XXX']">
  6.  
maybe add [1] or [position()=last()].
Reply