2010-12-06 110 views
0

问题是,在下面的XSLT文档中,我所有的hrow标签都位于底部,我希望它保持它们出现的顺序,我该怎么做?XSLT在格式化xml文档时维护标签顺序

我的XML文档

<log> 
<hrow time="45:43:2343">A heading</hrow> 
<row type="e">An error</row> 
<row type="w">An warn</row> 
<row type="i">An info</row> 
<row type="d">An debug</row> 
<row type="t">unknown</row> 
<hrow time="45:43:2343">Another heading</hrow> 
<row type="t">more rows</row> 
</log> 

的XSLT文档

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <table width="100%"> 
      <xsl:apply-templates /> 
     </table> 
    </xsl:template> 

    <xsl:template match="log"> 
      <xsl:apply-templates select="row" /> 
      <xsl:apply-templates select="hrow" /> 
    </xsl:template> 

    <xsl:template match="row"> 
     <xsl:variable name="type" select="@type" /> 
     <xsl:choose> 
      <xsl:when test="$type = 'd'"> 
       <tr> 
        <td style="background-color:#C6F98B"> 
         <xsl:value-of select="." /> 
        </td> 
       </tr> 
      </xsl:when> 
      <xsl:when test="$type = 'i'"> 
       <tr> 
        <td style="background-color:#8B8BF9"> 
         <xsl:value-of select="." /> 
        </td> 
       </tr> 
      </xsl:when> 
      <xsl:when test="$type = 'e'"> 
       <tr> 
        <td style="background-color:#F9555D"> 
         <xsl:value-of select="." /> 
        </td> 
       </tr> 
      </xsl:when> 
      <xsl:when test="$type = 'w'"> 
       <tr> 
        <td style="background-color:#F8F781"> 
         <xsl:value-of select="." /> 
        </td> 
       </tr> 
      </xsl:when> 
      <xsl:otherwise> 
       <tr> 
        <td style="background-color:#E4E4E4"> 
         <xsl:value-of select="." /> 
        </td> 
       </tr> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

    <xsl:template match="hrow"> 
     <tr> 
      <td style="background-color:#DBC5FF;font-size: 16px;"> 
       <xsl:value-of select="." /> 
       [ 
       <xsl:value-of select="@time" /> 
       ] 
      </td> 
     </tr> 
    </xsl:template> 

</xsl:stylesheet> 

回答

2

与所述所张贴的XML文档,而不是

<xsl:template match="log"> 
     <xsl:apply-templates select="row" /> 
     <xsl:apply-templates select="hrow" /> 
</xsl:template> 

根本就

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

按文档顺序处理日志元素的所有子节点。

或者,如果您有其他子节点未显示,但你不想处理,然后用

<xsl:template match="log"> 
     <xsl:apply-templates select="row | hrow"/> 
</xsl:template> 

这样选择的元素(即行和HROW)以文档顺序进行处理了。

+0

+1好答案。如果需要进行推式处理,也可以选择'select ='row'hrow'。 – 2010-12-06 14:32:40