2014-02-18 81 views
0

我有一个XML,其中有n个GRTSource下的GRTSource节点。我只需要GRTSource的前5个节点,并且我想访问表的详细信息,而不管表是否是immidiate子表。XSL将所选节点的子节点与模板相关联

这里是演示文件:

<GRTReport> 
<GRTSource> 
     <title>Unified CM Cluster Name</title> 
     <comment>Lists the cluster name from the Enterprise Parameter and the publisher server name/IP.</comment> 
     <title></title> 
     <comment></comment> 
     <table summary='Cluster Information'> 
      <row> 
       <cell style='header'>Cluster Name</cell> 
       <cell style='header'>Publisher Name/IP</cell> 
      </row> 
      <row> 
       <cell>xyzw</cell> 
       <cell>xyz1234</cell> 
      </row> 
     </table> 
    </GRTSource> 
    <GRTSource> 
     <div> 
     <title>Unified CM Cluster Name</title> 
     <comment>Lists the cluster name from the Enterprise Parameter and the publisher server name/IP.</comment> 
     <title></title> 
     <comment></comment> 
     <table summary='Cluster Information'> 
      <row> 
       <cell style='header'>Cluster Name</cell> 
       <cell style='header'>Publisher Name/IP</cell> 
      </row> 
      <row> 
       <cell>xyzw</cell> 
       <cell>xyz1234</cell> 
      </row> 
     </table> 
    </div> 
    </GRTSource> 
    </GRTReport> 

这里是我的xsl:

<xsl:template match="/"> 
     <html> 
      <body> 
       <xsl:apply-templates/> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="GRTReport/GRTSource[position() &lt; 6]"> 

     <fieldset class="reportSourceFieldset"> 
      <legend> 
       <xsl:value-of select="title[1]"/> 
      </legend> 
      <xsl:apply-templates select="descendant::table"/> 
     </fieldset> 
     <br /> 

    </xsl:template> 

    <xsl:template match="table"> 
     <table class="reportData"> 
      <tr class="cuesTableBg">  
       <xsl:for-each select="row[position() = 1]/cell"> 
        <th> 
         <pre> 
          <xsl:value-of select="current()"/> 
         </pre> 
        </th> 
       </xsl:for-each> 
      </tr> 

      <xsl:for-each select="row[position() &gt; 1]"> 
       <tr> 
        <xsl:for-each select="cell"> 
         <xsl:choose> 
         <xsl:when test="current()[contains(@style,'fixedFormat')]"> 
         <td> 
          <pre><xsl:value-of select="current()"/></pre> 
         </td> 
         </xsl:when> 
         <xsl:otherwise> 
         <td> 
         <span> 
          <xsl:value-of select="current()"/></span> 
         </td> 
         </xsl:otherwise> 
         </xsl:choose> 
        </xsl:for-each> 
       </tr> 
      </xsl:for-each> 
     </table> 
     <br /> 
    </xsl:template> 

不知怎的,它正在计数GRTSources高达5,但该查询接受外界的5 GRSource算作表好。据逸岸考虑网页上的所有表,而不仅仅是前五名内GRTSource

回答

2

当你这样做:

<xsl:template match="/"> 
    <html> 
     <body> 
      <xsl:apply-templates/> 
     </body> 
    </html> 
</xsl:template> 

你申请模板不分青红于全部当前节点的子节点(本例中为根节点)。 built-in template rules“允许递归处理在没有通过样式表中的显式模板规则进行的成功模式匹配的情况下继续”。这意味着模板从这里被应用到<GRTReport>,从那里到<GRTSource>元素,这些元素之前没有被更明确的规则匹配,并且从那里到他们的子女 - 突然间,您有一个匹配的expicit模板规则。

尝试改变上面:

<xsl:template match="/"> 
     <html> 
      <body> 
       <xsl:apply-templates select="GRTReport/GRTSource[position() &lt; 6]"/> 
      </body> 
     </html> 
</xsl:template> 

,然后你可以改变这一点:

<xsl:template match="GRTReport/GRTSource[position() &lt; 6]"> 

到:

<xsl:template match="GRTReport/GRTSource"> 
0

也许你要考虑:

<xsl:template match="GRTReport/GRTSource"> 
     <xsl:choose> 
      <xsl:when test="count(preceding-sibling::GRTSource) + 1 &lt; 6"> 
       <fieldset class="reportSourceFieldset"> 
        <legend> 
         <xsl:value-of select="descendant::title[1]"/> 
        </legend> 
        <xsl:apply-templates select="descendant::table"/> 
       </fieldset> 
       <br /> 
      </xsl:when> 
      <xsl:otherwise/> 
     </xsl:choose> 
</xsl:template> 
+0

这是可能的,但不建议由于内的两个复制模板。更好:使用一个''和''/'',分别。 – Tomalak

+0

@Tomalak,修改后的模板好吗? –

+0

不,这是一样的事情:你会*需要第二个模板来输出位置5之后的''元素。这是一个重复性和不清晰的方法。 - 使用一个可输出''元素的模板。它一定不在乎他们的立场。然后使用''来只输出你想看到的元素。 – Tomalak