XML:
<mode>
<submode>1</submode>
<submode>2</submode>
<submode>3</submode>
<submode>4</submode>
<submode>5</submode>
<submode>6</submode>
<submode>7</submode>
</mode>
<mode>
<submode>7</submode>
<submode>8</submode>
<submode>9</submode>
<submode>10</submode>
<submode>11</submode>
<submode>12</submode>
<submode>13</submode>
</mode>
<mode>
<submode>14</submode>
<submode>15</submode>
<submode>16</submode>
<submode>17</submode>
<submode>18</submode>
<submode>19</submode>
20</submode>
</mode>
How to test first <submode> from each <mode> (i need get numbers: 1, 7, 14) in such construction:
<xsl:template match="submode">
<xsl:if test="(parent::mode) and (...what?...)">
...
</xsl:if>
...
</xsl:template>
I do not understand how use position() here.
It is not generally true that
position() = 1evaluates to
true()if the current node has a parent mode and the current node is the firstsubmodechild of its parent.position()specifies the position of the current node-list and this is defined in a different way, depending on how theselectattribute of<xsl:apply-templates>is specified.For example (assuming that the provided XML has a top element that is the parent of the
modeelements), if the template was selected when processingthe following:<xsl:apply-templates select="/*/mode/submode[. = 3]"/>then
position() = 1is true only for the 3rd
submodechild of the firstmodeelement.One correct answer:
parent::mode and not(preceding-sibling::submode)Or, recommended:
Have a separate template:
<xsl:template match="mode/submode[1]">In this case no code within template is necessary to check if the current node is the first
submodechild — this is already known to be so.