2017-04-26 60 views
1

我试图从XML中的两个节点集合创建一个HTML表格,然后通过@AD对它进行排序。创建后在xsl中对表格进行排序

我可以使用<xsl:sort select="@AD" order="ascending" />在单个for循环中进行排序,但是我想对整个表进行排序。

<xsl:template match="*/Sync/AP"> 
    <table border="1"> 
     <tr> 
      <th>AD</th> 
      <th>GCD</th> 
      <th>ClearAttribute</th> 
     </tr> 
     <xsl:for-each select="./*"> 
     <tr> 
      <td><xsl:value-of select="@AD"/></td> 
      <td><xsl:value-of select="@GCD"/></td> 
      <td><xsl:value-of select="@ClearAttribute"/></td> 
     </tr>  
     </xsl:for-each> 
     <!-- Also Append the Common attributes to each region --> 
     <xsl:for-each select="../Common/*"> 
     <tr> 
      <td><xsl:value-of select="@AD"/></td> 
      <td><xsl:value-of select="@GCD"/></td> 
      <td><xsl:value-of select="@ClearAttribute"/></td> 
     </tr>  
     </xsl:for-each> 
    </table> 
</xsl:template> 
+0

你能展示一个你的XML的样本,以及你期望的(排序的)输出吗?谢谢! –

+0

感谢您检查Tim,但Tomalak向我提供了我一直在寻找的信息。 – Snath

回答

1

请勿两次分开<xsl:for-each>。选择您想要显示的所有节点并一步完成排序。

union运算符|用于此:

<xsl:template match="Sync/AP"> 
    <table border="1"> 
     <tr> 
      <th>AD</th> 
      <th>GCD</th> 
      <th>ClearAttribute</th> 
     </tr> 
     <xsl:for-each select="./* | ../Common/*"> 
      <xsl:sort select="@AD" order="ascending" /> 
      <tr> 
       <td><xsl:value-of select="@AD"/></td> 
       <td><xsl:value-of select="@GCD"/></td> 
       <td><xsl:value-of select="@ClearAttribute"/></td> 
      </tr>  
     </xsl:for-each> 
    </table> 
</xsl:template> 

注意:虽然match表情看起来像XPath的,他们不是真正的XPath。这是不必要的:

<xsl:template match="*/Sync/AP"> 

您可以使用它代替:

<xsl:template match="Sync/AP"> 

,甚至这样的:

<xsl:template match="AP"> 

除非你明确要确保只有<AP><Sync>父母匹配。

+0

这就是我错过的。谢谢@Tomalak – Snath

相关问题