finding the correct segment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madshov
    New Member
    • Feb 2008
    • 20

    finding the correct segment

    Hi,

    I have the following:

    Code:
    <start>
        <segment Id="AAA">
            <element Id="id">1</element>
            <element Id="seq">122</element>
            <element Id="seq2" Composite="yes">
                <subelement Sequence="1">57002122338772</subelement>
           </element>
       </segment>
       <segment Id="BBB">
          <element Id="id">5</element>
          <element Id="BBB02" Composite="yes">
              <subelement Sequence="1">AS12121212</subelement>
              <subelement Sequence="2">SA</subelement>
          </element>
       </segment>
       <segment Id="BBB">
          <element Id="id">1</element>
          <element Id="BBB02" Composite="yes">
              <subelement Sequence="1">12121212</subelement>
              <subelement Sequence="2">HS</subelement>
          </element>
       </segment>
       <segment Id="XXX">
          <element Id="XXX01">C</element>
          <element Id="XXX02">35</element>
          <element Id="XXX03" Composite="yes">
             <subelement Sequence="1">090</subelement>
             <subelement Sequence="3">91</subelement>
          </element>
       </segment>
       <segment Id="XXX">
          <element Id="XXX01">C</element>
          <element Id="XXX02">98</element>
          <element Id="XXX03" Composite="yes">
            <subelement Sequence="1">36</subelement>
            <subelement Sequence="3">91</subelement>
          </element>
      </segment>
    ...
    </start>
    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]
    Code:
        <segment Id="AAA">
            <element Id="id">2</element>
            ...
    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?
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    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:
    Code:
    <xsl:variable name="segid" select="@Id"/>
    <xsl:variable name="elid" select="element[@Id='id']+1"/>
    <xsl:for-each select="../segment[@id = $segid][element[@Id='id'][.=$elid]]">
    ...
    Even if this isn't it, it should give you some sort of clue.

    Comment

    • madshov
      New Member
      • Feb 2008
      • 20

      #3
      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 :)

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

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

        Another possibility:
        Code:
        <xsl:key name="previousAAA" match="segment" use="preceding-sibling::segment[@Id='AAA'][1]"/>
        ...
        while in aaa Segment:
        
        <xsl:for-each select="key('previousAAA', .)[@Id='XXX']">
        maybe add [1] or [position()=last ()].

        Comment

        Working...