xml - 嵌套的 XML XSL for-each 循环

标签 xml xslt

我无法读出嵌套的 for-each 循环。由于某种原因,它不会在直接节点下循环。问题似乎出在标签上。我的第一个循环发生在我打开表格后,我循环遍历整个表格以获取管理报告,并将第二个节点嵌套在它下面。这很好用,但是当我在下面嵌套其他节点时,我能够提取值,但不是特定于父节点的。这次练习让我的眼睛布满血丝,有人可以帮忙吗?提前致谢。

XSL:

    <tr bgcolor="9acd32">
    <table><th>Data Source Name:</th></table>
    <table><th><xsl:value-of select="@Value"/>   </th></table>
    </tr>
    <tr> 
   <xsl:for-each select="*[name()='PartInformation']">  
       <table bgcolor="#99ff66"><th>Part Information:</th></table>
       <table bgcolor="#99ff66"><th><xsl:value-of select="@Value"/></th></table>       
   <tr> 
    <xsl:for-each select="*/*[name()='InspPrgInformation']">    
       <table bgcolor="#33ccff"><th>Inspection Program ID:</th></table>
       <table bgcolor="#33ccff"><th><xsl:value-of select="@Value"/></th></table>
    <table bgcolor="#33ccff"><th><xsl:value-of select="@NoOfTracefields"/></th></table>
    </xsl:for-each>
    </tr>
    </xsl:for-each>
    </tr>         
<tr> 
   <xsl:for-each select="*/*/*[name()='AreaInformation']">    
       <table bgcolor="#FFFF99"><th>Area Information:</th></table>
       <table bgcolor="#FFFF99"><th><xsl:value-of select="@Area"/></th></table>
       <table bgcolor="#FFFF99"><th><xsl:value-ofselect="@AreaCount"/>         
  </th></table>
   </xsl:for-each>
   </tr>

 </xsl:for-each>
</table>
</center>

XML:

 <AdminReports xmlns="30/11/2011 09:25:58">

  <AdminReport ID="1">
    <DataSourceInformation DataSourceID="2" Value="DCS_AERO_KINSTON_DCS350">
      <PartInformation PartID="8" Value="WithAreaInfo">
        <InspPrgInformation InspPrgID="10" Value="DCS350_Sec15Drill_Pannel1WithInfo"       NoOfTracefields="1">          
          <AreaInformation Area="L3" AreaCount="59"/>
          <AreaInformation Area="L4" AreaCount="45"/>
          <AreaInformation Area="LT4" AreaCount="54"/>
        </InspPrgInformation>
      </PartInformation>
      <PartInformation PartID="9" Value="NoAreaInfo">
        <InspPrgInformation InspPrgID="9" Value="DCS350_Sec15Trim_Pannel1" NoOfTracefields="0"/>
      </PartInformation>
    </DataSourceInformation>
  </AdminReport>

  <AdminReport ID="2">
    <DataSourceInformation DataSourceID="2" Value="DCS_AERO_KINSTON_DCS350">
      <PartInformation PartID="8" Value="NoAreaInfo">
        <InspPrgInformation InspPrgID="10" Value="WithInfo" NoOfTracefields="1">          

        </InspPrgInformation>
      </PartInformation>
      <PartInformation PartID="9" Value="AreaInfo">
        <InspPrgInformation InspPrgID="9" Value="DCS350_Sec15Trim_Pannel1" NoOfTracefields="0">
          <AreaInformation Area="L4" AreaCount="75"/>
          <AreaInformation Area="LT4" AreaCount="4"/>
        </InspPrgInformation>
      </PartInformation>
    </DataSourceInformation>
  </AdminReport>
</AdminReports>

最佳答案

你正在做的事情对于你想要实现的目标来说是错误的:

<xsl:for-each select="*[name()='PartInformation']">  
   <table bgcolor="#99ff66"><th>Part Information:</th></table>
   <table bgcolor="#99ff66"><th><xsl:value-of select="@Value"/></th></table>       
   <tr> 
   <xsl:for-each select="*/*[name()='InspPrgInformation']">    
       <table bgcolor="#33ccff"><th>Inspection Program ID:</th></table>
       <table bgcolor="#33ccff"><th><xsl:value-of select="@Value"/></th></table>
    <table bgcolor="#33ccff"><th><xsl:value-of select="@NoOfTracefields"/></th></table>
    </xsl:for-each>
    </tr>
</xsl:for-each>

每个的第二个与第一个无关。你的第三个也是如此。

current() 不会为您提供当前迭代的节点。

您可以像这样重写前两个 for-each :

<tr>
            <xsl:for-each select="*[name()='PartInformation']">
                <tr>
                    <xsl:for-each select="current()/*/InspPrgInformation">
                        <table bgcolor="#33ccff">
                            <th>Inspection Program ID:</th>
                        </table>
                        <table bgcolor="#33ccff">
                            <th>
                                <xsl:value-of select="@Value"/>
                            </th>
                        </table>
                        <table bgcolor="#33ccff">
                            <th>
                                <xsl:value-of select="@NoOfTracefields"/>
                            </th>
                        </table>
                    </xsl:for-each>
                </tr>
            </xsl:for-each>
        </tr>

第三个可用于您当前的设计。因为 current() 对于每个 for-each 都是本地的,所以您的第三个 for-each 不知道其他两个。此外,您的设计似乎使用 xslt 作为一种编程语言,这不是可行的方法。

最后,下次尝试提供一些完整/可编译的示例以及您的目标文档。

关于xml - 嵌套的 XML XSL for-each 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8432385/

相关文章:

c# - 如何在 C# 中使用 XQuery

xml - XSLT 和 XHTML 输出的默认命名空间

XSLT 样式表用空的配对标签替换自关闭标签

xml - XSLT 和 XSLFO 命名空间

xslt - XSLT 中的 string(string(.)) 有什么意义吗?

java - 如何根据设备有效地更改布局

sql - 在 TSQL 中搜索不存在节点的 xml 的更好方法

xml - 如何使用vba解析XML

html - 通用XSLT,可将任何XML转换为基于节点的许多HTML页面

ios - Xcode Storyboard XML 标签含义