0

I've got an XML with diferent nodes:

<INFORME>
  <Assignatura>
     <Nom_materia>Física</Nom_materia>
  </Assignatura>
  <Assignatura>
     <Nom_materia>Informàtica</Nom_materia>
  </Assignatura>
</INFORME>

and, inside of the for-each, I want to get access to the previous node. I want to do something like this, and logically it doesnt work.

<xsl:for-each select="Assignatura">
  <xsl if text="Nom_materia = Assignatura[position()-1]/Nom_materia">
    do something...
....

It would be something very simple I think, but i don't know exactly how to do it.

Yaroslav
  • 6,476
  • 10
  • 48
  • 89

1 Answers1

1

Assuming that your context node is INFORME:

<xsl:for-each select="Assignatura">
  <xsl:if test="Nom_materia = preceding-sibling::Assignatura[1]/Nom_materia">
    do something...
  </xsl:if>
</xsl:for-each>
raffazizzi
  • 685
  • 6
  • 13
  • Thanks raffazizzi; but, i don't know exactly what i'm getting from the preceding-sibling; it seems is getting random "nom_materia"; but: the for-each is sorted: may this affect on that? thank you! – user1669087 Sep 14 '12 at 09:40
  • Well, the preceding-sibling axis will get the node that precedes the current node *in the tree*, not in the for each order. Looking up the previous item in the iteration is a different matter, and I think it will require that you create a variable containing the ordered elements first. – raffazizzi Sep 14 '12 at 14:56