如何在for-each xslt语句中选择父节点的每个子节点?

时间:2021-01-14 13:38:53

I want to select all child nodes from the adviceRow element (see xml data hereunder) in a for each loop.

我想在for循环中从adviceRow元素中选择所有子节点(参见下面的xml数据)。

I want to avoid having to write an xslt file like this:

我想避免写这样的xslt文件:

<xsl:for-each select="Tables/routeAdviceTable/routeAdviceRows/adviceRow">
   <xsl:value-of select="cog" />
   <xsl:value-of select="current" />
   <xsl:value-of select="distance" />
   <xsl:value-of select="dtg" />
   etc..
</xsl:for-each>

Because a row can have many cells (and i have lots of similar but contentwise different tables)

因为一行可以有很多单元格(我有很多相似但内容不同的表格)

Can I work with a nested for-each loop? Something like:

我可以使用嵌套的for-each循环吗?就像是:

for-each advice row:
    for-each child of advicerow?

<Tables>
<routeAdviceTable>
    <routeAdviceRows>
        <adviceRow>
            <cog>313</cog>
            <current>0.3</current>
            <distance>58.8</distance>
            <dtg>1374786000000</dtg>
            <latitude>????</latitude>
            <longitude>????</longitude>
            <pressure>22.300001</pressure>
            <sea>0.2</sea>
            <sog>14.7</sog>
            <stw>???</stw>
            <swell>1.7</swell>
            <waves>1.711724324567694</waves>
            <wind>0.8</wind>
        </adviceRow>
    </routeAdviceRows>
</routeAdviceTable>

Thank you

谢谢

1 个解决方案

#1


10  

Yes, you can nest for-each loops.

是的,你可以为每个循环嵌套。

Try this

尝试这个

<xsl:for-each select="Tables/routeAdviceTable/routeAdviceRows/adviceRow">
  <xsl:for-each select="./*">
    <!-- 
      Your code i.e.
      <td class="{name(.)}">
        <xsl:value-of select="."></xsl:value-of>
      </td>
    -->
  </xsl:for-each>
</xsl:for-each>

The xpath "." refers to the current node (a.k.a the “context node”), "/*" select all the child nodes of the context node.

xpath“。”指当前节点(也就是“上下文节点”),“/ *”选择上下文节点的所有子节点。

#1


10  

Yes, you can nest for-each loops.

是的,你可以为每个循环嵌套。

Try this

尝试这个

<xsl:for-each select="Tables/routeAdviceTable/routeAdviceRows/adviceRow">
  <xsl:for-each select="./*">
    <!-- 
      Your code i.e.
      <td class="{name(.)}">
        <xsl:value-of select="."></xsl:value-of>
      </td>
    -->
  </xsl:for-each>
</xsl:for-each>

The xpath "." refers to the current node (a.k.a the “context node”), "/*" select all the child nodes of the context node.

xpath“。”指当前节点(也就是“上下文节点”),“/ *”选择上下文节点的所有子节点。