有没有办法使用xPath从第一个'row'到第二个和前面'row'节点的每个节点的正确节点?
XML数据:
<extracted>
<row>
<item>Hour1</item>
<item>Hour2</item>
<item>Hour3A</item>
<item>Hour3B</item>
<item>Hour4</item>
<item>Hour5</item>
<item>Hour6</item>
<item>Hour7</item>
<item>Hour8</item>
<item>Hour9</item>
<item>Hour10</item>
<item>Hour11</item>
<item>Hour12</item>
<item>Hour13</item>
<item>Hour14</item>
<item>Hour15</item>
<item>Hour16</item>
<item>Hour17</item>
<item>Hour18</item>
<item>Hour19</item>
<item>Hour20</item>
<item>Hour21</item>
<item>Hour22</item>
<item>Hour23</item>
<item>Hour24</item>
<item>Aver./Sum</item>
</row>
<row>
<item>12,43</item>
<item>12,40</item>
<item>12,40</item>
<item/>
<item>12,40</item>
<item>12,40</item>
<item>12,48</item>
<item>14,35</item>
<item>15,48</item>
<item>22,79</item>
<item>24,16</item>
<item>24,35</item>
<item>28,25</item>
<item>24,68</item>
<item>23,30</item>
<item>21,93</item>
<item>16,76</item>
<item>15,08</item>
<item>15,06</item>
<item>14,89</item>
<item>14,79</item>
<item>16,06</item>
<item>15,45</item>
<item>14,57</item>
<item>14,57</item>
<item>17,13</item>
</row>
<row>
<item>12,43</item>
<item>12,40</item>
<item>12,40</item>
<item/>
<item>12,40</item>
<item>12,40</item>
<item>12,48</item>
<item>14,35</item>
<item>15,48</item>
<item>22,79</item>
<item>24,16</item>
<item>24,35</item>
<item>28,25</item>
<item>24,68</item>
<item>23,30</item>
<item>21,93</item>
<item>16,76</item>
<item>15,08</item>
<item>15,06</item>
<item>14,89</item>
<item>14,79</item>
<item>16,06</item>
<item>15,45</item>
<item>14,57</item>
<item>14,57</item>
<item>17,13</item>
</row>
</extracted>
例如:12,43匹配“Hour1”。只有这一行有“小时”但有几个有值。
我想做的事情:
以某种方式获取节点的位置并在第一个“行”节点的谓词中使用位置。
答案 0 :(得分:1)
答案可能涉及position()
函数,但魔鬼在细节中,这一切都取决于您当时正在操作的上下文。以XSLT为例,这有效:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="extracted">
<ex>
<xsl:apply-templates select="row[position() > 1]" />
</ex>
</xsl:template>
<xsl:template match="row">
<r>
<xsl:apply-templates select="item" />
</r>
</xsl:template>
<xsl:template match="item">
<!-- the current node is an item, and the current node list is the set
of item children of the parent row element. Therefore position()
is 1 for the first item, 2 for the second item, etc. -->
<xsl:variable name="myPosition" select="position()" />
<!-- so here we select the nth item from the first row of the table -->
<entry key="{../../row[1]/item[$myPosition]}" value="{.}" />
</xsl:template>
</xsl:stylesheet>
但这不是
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="extracted">
<ex>
<xsl:apply-templates select="row[position() > 1]" />
</ex>
</xsl:template>
<xsl:template match="row">
<r>
<xsl:apply-templates select="node()" />
</r>
</xsl:template>
<xsl:template match="item">
<!-- the current node is an item, and the current node list is the set
of all child nodes of the parent row element, including text nodes.
Therefore position() is 1 for the whitespace before the first item,
2 for the first item itself, 3 for the whitespace between the first and
second items, 4 for the second item, etc. -->
<xsl:variable name="myPosition" select="position()" />
<!-- so here we select the wrong item from the first row of the table -->
<entry key="{../../row[1]/item[$myPosition]}" value="{.}" />
</xsl:template>
</xsl:stylesheet>
如果您无法确定是否在一个有效position()
的上下文中执行XPath表达式,那么您将不得不使用其他方法,例如
count(preceding-sibling::item) + 1
通过计算此前item
个元素的数量来计算正确的位置。