XSLT - comparing previous element elements with the current node element

I have this xml file:

<recursos> <recurso url="http://w3c.com"> <descripcion>Consorcio W3C</descripcion> <tipo>externo</tipo> <idioma>ingles</idioma> <contenido>General</contenido> <unidad>Unidad 2</unidad> </recurso> <recurso url="http://html.com"> <descripcion>Especificación HTML</descripcion> <tipo>externo</tipo> <idioma>castellano</idioma> <contenido>HTML</contenido> <version>4.01</version> <unidad>Unidad 3</unidad> </recurso> </recursos> 

I want to compare one "recurso" preceding sibling "unidad" element with the "unidad" of the current "recurso" to see if they are different.

I have tried:

 <xsl:if test="preceding-sibling::recurso[position()=1]::unidad != unidad"> </xsl:if> 

But I know this is terribly wrong :( I hope you could help me, thank you very much.

+16
xml xpath xslt
Jun 07 2018-10-06T00:
source share
1 answer

Almost correct.

 <xsl:if test="preceding-sibling::recurso[1]/unidad != unidad"> </xsl:if> 

:: is for axes, not for moving along a path ("creating a location step"). In XPath terminology:

 preceding-sibling :: recurso [1] / unidad! = unidad
 '' '' '' '' '' '' '' '' '' +++++++++++ ++++++
                           ###
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 '= axis name (optional, defaults to "child")
 + = node test (required)
 # = predicate (optional, for filtering)
 ~ = location step (required at least once per select expression)

[1] is short for [position()=1] .

The child axis is implicit in the location step, so this

 preceding-sibling::recurso[1]/unidad != unidad 

equivalent to this:

 preceding-sibling::recurso[1]/child::unidad != unidad 
+35
Jun 07 2018-10-06T00:
source share



All Articles