2015-05-20 56 views
1

我有以下XML的文件類型和使用XSLT模板我嘗試創建一個HTML的報告:XSLT刪除重複的兒童及父節點

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> 
<testmodule > 
    <testgroup> 
    <testgroup> 
     <testcase > 
     <teststep result="pass">=&gt; 
      <CRID Name="Cleese" CR-ID="CR#1234" TimeStamp="20150520 0122" Backcolor="White" /> 
     </teststep> 
     <teststep result="pass">=&gt;: 
      <CRID Name="Cleese" CR-ID="CR#123" TimeStamp="20150520 0122" Backcolor="White" /> 
     </teststep> 
     <verdict time="" timestamp="" endtime="" endtimestamp="" result="pass" /> 
     <ident>Testcase1</ident> 
     <CRID Name="Cleese" CR-ID="CR#123" TimeStamp="20150520 0123" Backcolor="White" /> 
     </testcase> 
     <testcase > 
     <teststep result="pass"> 
      <CRID Name="Cleese" CR-ID="CR#1234" TimeStamp="20150520 0123" Backcolor="White" /> 
     </teststep> 
     <teststep result="pass">=&gt; 
      <CRID Name="Cleese" CR-ID="CR#1234" TimeStamp="20150520 0123" Backcolor="White" /> 
     </teststep> 
     <teststep result="pass">=&gt; 
      <CRID Name="Cleese" CR-ID="CR#1234" TimeStamp="20150520 0122" Backcolor="White" /> 
     </teststep> 
     <verdict result="pass" /> 
     <ident>Testcase2</ident> 
     <CRID Name="Cleese" CR-ID="CR#1234" TimeStamp="20150520 0123" Backcolor="White" /> 
     </testcase> 
     <testcase > 
     <testlogfile file="" /> 
     <teststep result="pass">=&gt; Result: 
      <CRID Name="Cleese" CR-ID="CR#123" TimeStamp="20150520 0123" Backcolor="White" /> 
     </teststep> 
     <verdict result="pass" /> 
     <ident>Testcase3</ident> 
     <CRID Name="Cleese" CR-ID="CR#1234" TimeStamp="20150520 0123" Backcolor="White" /> 
     </testcase> 
     <title>1.3 Group</title> 
    </testgroup> 
    <title>1. Group</title> 
    </testgroup> 
    <verdict time="" timestamp="" endtime="" endtimestamp="" result="pass" /> 
    <title>Testmodul</title> 
</testmodule> 

我想實現的是去除在html文件中具有CR-ID屬性的重複條目。我試圖使用「Muenchian方法」,但字面上失敗了。

所以我看到另一個代碼片段,並試圖適應它,但它不工作。它刪除所有重複項,但我只想刪除每個測試用例的重複項。 這裏是.xslt - 模板的一部分:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> 
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
     <xsl:output method="html" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/> 
     <xsl:template match="testcase" mode="overview"> 
       <xsl:for-each select="descendant::CRID[not(@CR-ID=preceding::CRID/@CR-ID)]"> 
        <xsl:if test="@CR-ID !=''"> 
         <xsl:element name="br"/> 
         <xsl:value-of select="@CR-ID" /> 
        </xsl:if> 
       </xsl:for-each> 
      </xsl:template> 

我的問題是,屬性「CR-ID」可以如兒童中的「一步步測試」或「測試用例」中找到。所以一些條目被徹底刪除。

的希望輸出將只給我在我的HTML如下條目:

  • 測試用例1:只有CR#1234,CR#123
  • 測試用例2:只有CR#1234
  • 測試用例3:僅CR#1234,CR#123

HTML代碼應該是這樣的:

<td>1.1.1</td> 
      <td >Testcase1</td> 
      <td ><br>CR#1234<br>CR#123</td> 
      <td>-</td> 
      </tr> 
      <tr> 
      <td >1.1.2</td> 
      <td >Testcase2</td> 
      <td ><br>CR#1234</td> 
      <td >-</td> 
      </tr> 
      <tr> 
      <td >1.1.3</td> 
      <td >Testcase3</td> 
      <td ><br>CR#1234<br>CR#123</td> 
      <td ">-</td> 

從xml文件中使用「CR#1234」和「CR#123」。使用我發佈的xslt代碼只有testcase1獲得CR#,其他人是空白/空的。

任何人都可以幫助我嗎?

+2

沒有很好地形成的輸入。請更正它併發布預期的輸出HTML代碼。 –

+0

只是調整了代碼以更好地描述問題。 – Cleese

+0

你在使用XSLT 1.0嗎? –

回答

0

隨着Muenchian分組的需要與屬性Concat的IDENT的:

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

    <xsl:key name="group" match="testcase//CRID" use="concat(ancestor::testcase/ident, '|', @CR-ID)"/> 

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

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

    <xsl:template match="testcase"> 
     <li> 
      <xsl:value-of select="concat(ident, ': ')"/> 
      <xsl:apply-templates select=".//CRID[generate-id() = generate-id(key('group', concat(ancestor::testcase/ident, '|', @CR-ID))[1])]"/> 
     </li> 
    </xsl:template> 

    <xsl:template match="CRID"> 
     <xsl:if test="position() > 1">, </xsl:if> 
     <xsl:value-of select="@CR-ID"/> 
    </xsl:template> 
</xsl:transform> 

這轉變

<root> 
    <testcase> 
    <teststep> 
     <CRID Name="Cleese" CR-ID="CR#1234"/> 
    </teststep> 
     <teststep> 
     <CRID Name="Cleese" CR-ID="CR#123"/> 
    </teststep> 
     <teststep> 
     <CRID Name="Cleese" CR-ID="CR#1234"/> 
    </teststep> 
    <ident>Testcase1</ident> 
</testcase> 
<testcase> 
    <teststep> 
     <CRID Name="Cleese" CR-ID="CR#1234"/> 
    </teststep> 
     <teststep> 
     <CRID Name="Cleese" CR-ID="CR#123"/> 
    </teststep> 
    <CRID Name="Cleese" CR-ID="CR#1234"/> 
    <ident>Testcase2</ident> 
</testcase> 
<testcase> 
    <teststep> 
     <CRID Name="Cleese" CR-ID="CR#1234"/> 
    </teststep> 
     <teststep> 
     <CRID Name="Cleese" CR-ID="CR#123"/> 
    </teststep> 
     <teststep> 
     <CRID Name="Cleese" CR-ID="CR#1234"/> 
    </teststep> 
    <CRID Name="Cleese" CR-ID="CR#123"/> 
    <ident>Testcase3</ident> 
</testcase> 
</root> 

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 

     <title>Example</title> 
    </head> 
    <body> 
     <ul> 

     <li>Testcase1: CR#1234, CR#123</li> 

     <li>Testcase2: CR#1234, CR#123</li> 

     <li>Testcase3: CR#1234, CR#123</li> 

     </ul> 
    </body> 
</html> 
+0

最好謝謝!!!!我只是包括它,它似乎工作。現在我將仔細研究concat函數。 – Cleese