XPATH Selecting by position returns incoherent results
I need some help with an issue I can't figure out.
I have the following xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="prueba.xsl"?>
<ficha>
<titulo></titulo>
<bloque>
<texto></texto>
<pregunta id="1" tipo="checkSN">
<texto>Acredita curso bienestar animal minimo 20 h</texto>
</pregunta>
<pregunta id="2" tipo="texto">
<texto>Sistemática inspección</texto>
</pregunta>
<grupo>
<texto>trato adecuado enfermos</texto>
<pregunta id="3" tipo="desplegableSNP">
<texto>Recetas correspondientes</texto>
</pregunta>
<pregunta id="4" tipo="multiple">
<texto>Disponen de comida y bebida</texto>
</pregunta>
</grupo>
<grupo>
<texto>
Heridos/Enfermos
</texto>
<pregunta id="5" tipo="multiple">
<texto>Se aprecian heridos o enfermos momento inspeccion</texto>
</pregunta>
<pregunta id="6" tipo="multiple">
<texto>Separados del resto</texto>
</pregunta>
<pregunta id="7" tipo="multiple">
<texto>Disponen de comida y bebida</texto>
</pregunta>
<pregunta id="8" tipo="multiple">
<texto>Disponen de comida y bebida</texto>
</pregunta>
</grupo>
</bloque>
<bloque>
<texto>Condiciones específicas de alojamiento y manejo</texto>
</bloque>
</ficha>
And The folliwng XSL sheet:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" type="text/css" href="prueba.css" />
</head>
<body>
<h2><xsl:value-of select="/ficha/titulo"/></h2>
<h3>1: <xsl:value-of select="//pregunta[1]/@id"/></h3>
<h3>2: <xsl:value-of select="//pregunta[2]/@id"/></h3>
<h3>3: <xsl:value-of select="//pregunta[3]/@id"/></h3>
<h3>4: <xsl:value-of select="//pregunta[4]/@id"/></h3>
<h3>5: <xsl:value-of select="//pregunta[5]/@id"/></h3>
<h3>6: <xsl:value-of select="//pregunta[6]/@id"/></h3>
<h3>7: <xsl:value-of select="//pregunta[7]/@id"/></h3>
<h3>8: <xsl:value-of select="//pregunta[8]/@id"/></h3>
<h3>c: <xsl:value-of select="count(//pregunt开发者_Go百科a)"/></h3>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
When I load them I got this result:
1: 1 2: 2 3: 7 4: 8 5: 6: 7: 8: c: 8
I don't understand why it's ignoring some nodes . If I include new nodes or move them, it always shows 4 results, from node at position 5 to 8 it never shows anything. I need to use this type of selecting because it's from a Java application, the stylesheet is just for testing.
Put //pregunta
in parenthesis. Change your XPath expressions to (//pregunta)[1]/@id
, (//pregunta)[2]/@id
...
Without parenthesis e.g. //pregunta[4]
evaluates to all pregunta
elements which are at the fourth position of their parent element.
However (//pregunta)[4] first calculates the sequence of all pregunta
elements and then takes the fourth element of that sequence.
I can't fully explain why you're seeing the behaviour you are (I suspect it may be something to do with an odd 2d array being returned but that is just my own wild theory) but there does seem to be a simple workaround. If you use a foreach loop (which seems to fit what you need better anyway, although I could be wrong again!) it returns all of the values from the select, i.e.
<xsl:for-each select="//pregunta">
<h3><xsl:value-of select="position()" /> : <xsl:value-of select="@id" /></h3>
</xsl:for-each>
精彩评论