Xslt accesses the previous for-each loop element

Suppose we have the following xml source.

<Data Key="SS_001PG"
      OC:DataId="001PG"
      OC:UniqueIdentifier="01-003"
      OC:Status="available"
      OC:DateOfBirth="2010-06-29"
      OC:Sex="m">
    <Event EventOID="123"
           OC:EventLocation="we"
           OC:StartDate="2010-07-12"
           OC:Status="started"
           OC:Age="0"
           EventRepeatKey="1"></Event>
    <Event EventOID="123"
           OC:StartDate="2010-07-14"
           OC:Status="started"
           OC:Age="0"
           EventRepeatKey="2"></Event>
</Data>
<Data Key="SS_1"
      OC:DataId="1"
      OC:UniqueIdentifier="1"
      OC:Status="available"
      OC:DateOfBirth="2010-07-14"
      OC:Sex="m">
    <Event EventOID="123"
           OC:StartDate="2010-07-16"
           OC:EndDate="2010-07-14"
           OC:Status="started"
           OC:Age="-1"
           EventRepeatKey="1"></Event>
</Data>

We have the following xslt code to process it.

<xsl:variable name="repeatedEvents" select="//Event[@EventOID='123']"/>
<xsl:for-each select="$repeatedEvents">
    <xsl:sort select="@EventRepeatKey" data-type="number"/>
    <xsl:variable name="prevIndex" select="position()-1"/>
    <xsl:variable name="prevEvent"
                  select="$repeatedEvents[position()=$prevIndex]"/>
    <xsl:choose>
        <xsl:when test="position()=1">
            <xsl:value-of select="@EventRepeatKey"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:if test="$prevEvent/@EventRepeatKey != @EventRepeatKey">
                <xsl:value-of select="@EventRepeatKey"/>
            </xsl:if>
        </xsl:otherwise>
    </xsl:choose>
</xsl:for-each>

Now, as you can see, we select all events that have the same EventOID, and then sort the elements using the EventRepeatkey. So, after sorting the Event under the second Data occurs between the events of the first Data. Inside the loop, while the second element is being processed, we can access the first element using the previous index, but when the third element is being processed, we cannot access the second element using the previous index. This is because the second element is in the lower position of the tree, than the third element? Any suggestion how we could solve the problem?

Can anyone help?

+3
1

, .

Muenchian :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kEvByRepK" match="Event[@EventOID='123']"
          use="@EventRepeatKey"/>

 <xsl:template match=
   "Event[@EventOID='123'
             and
             generate-id()
             =
              generate-id(key('kEvByRepK', @EventRepeatKey)[1])
          ]">
  <xsl:value-of select="@EventRepeatKey"/>
  <xsl:text>&#xA;</xsl:text>
 </xsl:template>
</xsl:stylesheet>

, XML- ( ):

<t xmlns:OC="my:OC" >
    <Data Key="SS_001PG" OC:DataId="001PG" OC:UniqueIdentifier="01-003"
 OC:Status="available" OC:DateOfBirth="2010-06-29" OC:Sex="m">
        <Event EventOID="123" OC:EventLocation="we" OC:StartDate="2010-07-12"
 OC:Status="started" OC:Age="0" EventRepeatKey="1"/>
        <Event EventOID="123" OC:StartDate="2010-07-14" OC:Status="started"
 OC:Age="0"
        EventRepeatKey="2"/>
    </Data>
    <Data Key="SS_1" OC:DataId="1" OC:UniqueIdentifier="1" OC:Status="available"
 OC:DateOfBirth="2010-07-14"  OC:Sex="m">
        <Event EventOID="123" OC:StartDate="2010-07-16" OC:EndDate="2010-07-14" 
OC:Status="started" OC:Age="-1" EventRepeatKey="1"/>
    </Data>
</t>

, :

1
2

: Muenchian .

+3

Source: https://habr.com/ru/post/1788232/


All Articles