How to select each child node of a parent in a for-each xslt statement?

对着背影说爱祢 提交于 2020-06-09 18:18:13

问题


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

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

<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 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:


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.



来源:https://stackoverflow.com/questions/17919463/how-to-select-each-child-node-of-a-parent-in-a-for-each-xslt-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!