如何在for-each循环中基于元素值从一个分支返回元素值

时间:2014-09-27 02:35:08

标签: xslt

我需要更新下面的xsl,以便输出中的用户组不是" true",而是用户组名称的值。

例如:对于"摘要报告"对于网格名称:" grid01"我需要xsl到达gridmappings元素和报表并返回用户组名称:"经理"

这可能吗?

道歉,xml和xsl在预览窗格中没有正确显示,但应该足以传达我需要做的事情。

谢谢 杆

     <top>

enter code here            总结报告                           grid01                             grid02                                           grid01           经理                             grid02           办公室管理员                     

<report>
  <reportname>Detail Report</reportname>
  <grids>
    <grid>
      <gridname>grid01</gridname>
    </grid>
    <grid>
      <gridname>grid02</gridname>
    </grid>
  </grids>
  <gridmappings>
    <gridmap>
      <gridname>grid01</gridname>
      <usergroup>east coast managers</usergroup>
    </gridmap>
    <gridmap>
      <gridname>grid02</gridname>
      <usergroup>west coast managers</usergroup>
     </gridmap>
  </gridmappings>
   </report>
 </reports>
 </top>

xsl:

``           

<html>
  <body>
 <h2>Report Metadata</h2>

  <table border="1">
   <tr bgcolor="#9acd32">
     <th>Report Name</th>
      <th>Grid Name</th>
  <th>User Group</th>
   </tr>

    <xsl:for-each select="/top/reports/report/grids/grid">      
        <tr>
            <td><xsl:value-of select="ancestor::report/reportname"/></td>
            <td><xsl:value-of select="gridname"/></td>

            <!-- <td><xsl:value-of select="user_group_goes_here"/></td> -->
            <td><xsl:value-of select="ancestor::report/gridmappings/gridmap/gridname=gridname"  /></td>

        </tr>
    </xsl:for-each>


 </table>

  </body>
  </html>

 </xsl:template>
 </xsl:stylesheet> 

1 个答案:

答案 0 :(得分:0)

您可以使用父轴与不同层次结构中的元素联系:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="html"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
    <html>
        <body>
            <h2>Report Metadata</h2>
            <table border="1">
                <tr bgcolor="#9acd32">
                    <th>Report Name</th>
                    <th>Grid Name</th>
                    <th>User Group</th>
                </tr>
                <xsl:for-each select="top/reports/report/grids/grid">
                    <tr>
                        <td>
                            <xsl:value-of select="../../reportname"/>
                        </td>
                        <td>
                            <xsl:value-of select="gridname"/>
                        </td>
                        <td>
                            <xsl:value-of select="../../gridmappings/gridmap[gridname = current()/gridname]/usergroup"  />
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>