如何在xslt中删除空值?

时间:2017-05-31 17:15:22

标签: xml xslt xslt-1.0 xslt-grouping

我正在使用此algo

http://www.jenitennison.com/xslt/grouping/muenchian.html用于分组。我可以使用那个算法。但我面临一个问题。我有一个xml我在Over上应用分组。我能够显示我的数据。但有时候@over变成{{ 1}} ..我不想显示那些数据.. 这是我的代码 http://xsltransform.net/pNmBxZY/1

转型

blank

当前结果

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
   <xsl:key name="node-by-over" match="Node" use="@Over" />

    <xsl:template match="/Innings">
      <hmtl>
        <head>
          <title>New Version!</title>
        </head>
        <xsl:apply-templates select="Node[generate-id() = generate-id(key('node-by-over', @Over)[1])]"/>

      </hmtl>
    </xsl:template>

     <xsl:template match="Node">
   over::: <h1><xsl:value-of select="@Over"/></h1>
    <table id="{Over}">

      <xsl:for-each select="key('node-by-over', @Over)">
        <tr>
         <td>balls:::<xsl:value-of select="@Ball"/></td>
        <td>runs:::<xsl:value-of select="Runs"/></td>

         </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:transform>

预期输出

<!DOCTYPE html
  PUBLIC "XSLT-compat">
<hmtl>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>New Version!</title>
   </head>
   over::: 
   <h1>1</h1>
   <table id="">
      <tr>
         <td>balls:::1</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::2</td>
         <td>runs:::1</td>
      </tr>
      <tr>
         <td>balls:::3</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::4</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::5</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::6</td>
         <td>runs:::2</td>
      </tr>
   </table>
   over::: 
   <h1></h1>
   <table id="">
      <tr>
         <td>balls:::</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::</td>
         <td>runs:::0</td>
      </tr>
   </table>
   over::: 
   <h1>2</h1>
   <table id="">
      <tr>
         <td>balls:::1</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::2</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::3</td>
         <td>runs:::2</td>
      </tr>
      <tr>
         <td>balls:::4</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::5</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::6</td>
         <td>runs:::1</td>
      </tr>
   </table>
   over::: 
   <h1>3</h1>
   <table id="">
      <tr>
         <td>balls:::1</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::2</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::3</td>
         <td>runs:::1</td>
      </tr>
      <tr>
         <td>balls:::4</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::5</td>
         <td>runs:::1</td>
      </tr>
   </table>
</hmtl>

当前和预期产出之间的差异

<!DOCTYPE html
  PUBLIC "XSLT-compat">
<hmtl>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>New Version!</title>
   </head>
   over::: 
   <h1>1</h1>
   <table id="">
      <tr>
         <td>balls:::1</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::2</td>
         <td>runs:::1</td>
      </tr>
      <tr>
         <td>balls:::3</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::4</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::5</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::6</td>
         <td>runs:::2</td>
      </tr>
   </table>

   over::: 
   <h1>2</h1>
   <table id="">
      <tr>
         <td>balls:::1</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::2</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::3</td>
         <td>runs:::2</td>
      </tr>
      <tr>
         <td>balls:::4</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::5</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::6</td>
         <td>runs:::1</td>
      </tr>
   </table>
   over::: 
   <h1>3</h1>
   <table id="">
      <tr>
         <td>balls:::1</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::2</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::3</td>
         <td>runs:::1</td>
      </tr>
      <tr>
         <td>balls:::4</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::5</td>
         <td>runs:::1</td>
      </tr>
   </table>
</hmtl>

我不想遍历具有over::: <h1></h1> <table id=""> <tr> <td>balls:::</td> <td>runs:::0</td> </tr> <tr> <td>balls:::</td> <td>runs:::0</td> </tr> </table> @Over的Node ..请参阅xml中的示例

blank

我不想遍历id =“14”,因为它没有超值。但是当前代码正在这样做,为什么它显示在html部分之上?如何申请支票?

目前最后一个节点显示如此

 <Node Id="14" Over="" 

我们能表现得那样吗

over::: 
   <h1>3</h1>
   <table id="">
      <tr>
         <td>balls:::1</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::2</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::3</td>
         <td>runs:::1</td>
      </tr>
      <tr>
         <td>balls:::4</td>
         <td>runs:::0</td>
      </tr>
      <tr>
         <td>balls:::5</td>
         <td>runs:::1</td>
      </tr>
   </table>

@ over。@ ball (最后一个节点)

1 个答案:

答案 0 :(得分:1)

如果我理解正确,你想改变:

<xsl:apply-templates select="Node[generate-id() = generate-id(key('node-by-over', @Over)[1])]"/>

为:

<xsl:apply-templates select="Node[@Over>0][generate-id() = generate-id(key('node-by-over', @Over)[1])]"/>