[英]Test first subnode from node
XML: 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: 在这样的构造中,如何从每个<mode>
测试第一个<submode>
(我需要获取数字: <submode>
):
<xsl:template match="submode">
<xsl:if test="(parent::mode) and (...what?...)">
...
</xsl:if>
...
</xsl:template>
I do not understand how use position() here. 我不明白如何在这里使用position()。
It is not generally true that 通常不是真的
position() = 1
evaluates to true()
if the current node has a parent mode and the current node is the first submode
child of its parent. 计算结果为true()
如果当前节点有一个父模式和当前节点的第一submode
其父的孩子。
position()
specifies the position of the current node-list and this is defined in a different way, depending on how the select
attribute of <xsl:apply-templates>
is specified. position()
指定当前节点列表的位置,并且以不同的方式定义此位置,具体取决于如何指定<xsl:apply-templates>
的select
属性。
For example (assuming that the provided XML has a top element that is the parent of the mode
elements), if the template was selected when processingthe following: 例如(假设提供的XML的top元素是mode
元素的父元素),如果在处理以下内容时选择了模板:
<xsl:apply-templates select="/*/mode/submode[. = 3]"/>
then 然后
position() = 1
is true only for the 3rd submode
child of the first mode
element. 仅第三是真submode
的第一个儿童mode
元素。
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 submode
child -- this is already known to be so. 在这种情况下,不需要模板中的代码来检查当前节点是否是第一个子模式子节点-众所周知,这是submode
节点的submode
。
To count the number of submode
s in the previous mode
if and only if this is the first submode
of the current mode
, and avoid duplicating code between <xsl:template match="submode">
and <xsl:template match="submode[1]">
: 要计算的数量submode
S IN之前的mode
当且仅当这是第一submode
的电流的mode
,并避免重复之间代码<xsl:template match="submode">
和<xsl:template match="submode[1]">
:
<!-- Special processing for first submode -->
<xsl:template match="submode[1]">
<xsl:variable name="previousSubmodes"
select="count(../preceding-sibling::mode/submode)"/>
<!-- ... Do stuff with count ... -->
<!-- Perform regular submode processing -->
<xsl:call-template name="submode"/>
</xsl:template>
<!-- Regular processing for submodes -->
<xsl:template match="submode" name="submode">
<!-- ... Do whatever ... -->
</xsl:template>
Alternatively, you can do the count processing from the template for mode
instead. 或者,您可以改为从mode
模板进行计数处理。 That way, you will not need any special processing for the first submode
. 这样,您就不需要对第一submode
进行任何特殊处理。
<xsl:template match="mode">
<!-- ... Other processing ... -->
<xsl:variable name="previousSubmodes"
select="count(preceding-sibling::mode/submode)"/>
<!-- ... Do stuff with count ... -->
<!-- Handle submodes; could use select="node()|@*" instead to process
everything, not just submodes -->
<xsl:apply-templates select="submode"/>
</xsl:template>
<xsl:template match="submode">
<!-- ... Do whatever ... -->
</xsl:template>
Your <xsl:template match=
should read "mode/submode[1]"
. 您的<xsl:template match=
应该显示为"mode/submode[1]"
。
Then you'd have the first submode
of every mode
. 然后,您将拥有每种mode
的第一submode
mode
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.