2016-04-20 15 views
0

我想具體的改變TDXHTML文件,如果下一個元素的類是「XXX」,然後刪除該元素(帶班「XXX」)但前同輩是錯誤的模板,所以我必須錯過的東西:XSLT變化元件的名字 - 前同輩誤解

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="http://www.w3.org/1999/xhtml"> 
    <xsl:template match="node() | @*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node() | @*"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="//x:tr[@class='xxx']/preceding-sibling::x:tr/td"> 
     <th> 
      <xsl:apply-templates select="@* | node()"/> 
     </th> 
    </xsl:template> 
    <xsl:template match="//x:tr[@class='xxx']"/> 
</xsl:stylesheet> 

編輯: 樣本文檔:

<?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" lang="cs-CZ" xml:lang="cs-CZ"> 
<head> 
    <title>test</title> 
    <meta charset="UTF-8"/> 
    <link rel="stylesheet" type="text/css" href="xxx.css"/> 
</head> 
<body> 
    <table> 
     <tr> 
      <td>1</td> 
      <td>2</td> 
      <td>3</td> 
     </tr> 
     <tr class="xxx"> 
      <td></td> 
      <td></td> 
      <td></td> 
     </tr> 
     <tr> 
      <td>a</td> 
      <td>b</td> 
      <td>c</td> 
     </tr> 
     <tr> 
      <td>error row to remove if present</td> 
     </tr> 
    </table> 
</body> 
</html> 

我想是因爲如果存在刪除最後一個錯誤行並保持HTML5的doctype表的第一行:

<?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" lang="cs-CZ" xml:lang="cs-CZ"> 
<head> 
    <title>test</title> 
    <meta charset="UTF-8"/> 
    <link rel="stylesheet" type="text/css" href="xxx.css"/> 
</head> 
<body> 
    <table> 
     <tr> 
      <th>1</th> 
      <th>2</th> 
      <th>3</th> 
     </tr> 
     <tr> 
      <td>a</td> 
      <td>b</td> 
      <td>c</td> 
     </tr> 
    </table> 
</body> 
</html> 

我得到(與<xsl:template match="//x:tr[following-sibling::x:tr[1]/@class='xxx']/x:td">這是目前有效的XSLT文件中) :

<?xml version='1.0' encoding='UTF-8'?> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" lang="cs-CZ" xml:lang="cs-CZ"> 
<head> 
    <title>test</title> 
    <meta charset="UTF-8"/> 
    <link rel="stylesheet" type="text/css" href="xxx.css"/> 
</head> 
<body> 
    <table> 
     <tr> 
      <th xmlns="" xmlns:x="http://www.w3.org/1999/xhtml">1</th> 
      <th xmlns="" xmlns:x="http://www.w3.org/1999/xhtml">2</th> 
      <th xmlns="" xmlns:x="http://www.w3.org/1999/xhtml">3</th> 
     </tr> 

     <tr> 
      <td>a</td> 
      <td>b</td> 
      <td>c</td> 
     </tr> 
     <tr> 
      <td>error row to remove if present</td> 
     </tr> 
    </table> 
</body> 

所以沒有DOCTYPE和在這些單元格中,名稱空間不在那裏。我不知道如何刪除錯誤的行(它應該總是最後一行)。

+2

怎麼樣,結果你張貼的輸入採樣和你想要的結果,一起? –

回答

0

如果你想創建XHTML命名空間新th元素,那麼你需要添加它樣式表:

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:x="http://www.w3.org/1999/xhtml" 
    xmlns="http://www.w3.org/1999/xhtml" 
    exclude-result-prefixes="x"> 

    <xsl:output method="xml" doctype-system="about:legacy-compat"/> 

    <xsl:template match="node() | @*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node() | @*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="x:tr[following-sibling::x:tr[1][@class = 'xxx']]/x:td"> 
     <th> 
      <xsl:apply-templates select="@* | node()"/> 
     </th> 
    </xsl:template> 

    <xsl:template match="x:tr[@class = 'xxx'] | x:tr[last()]"/> 

</xsl:stylesheet> 

至於保持DOCTYPE,你需要創建一個新的,至少在XSLT 1.0(這是在1999年完成了15年是前HTML5)所有你能做的就是用<xsl:output method="xml" doctype-system="about:legacy-compat"/>輸出

<!DOCTYPE html 
    SYSTEM "about:legacy-compat"> 
1

有了一些客串,我認爲這樣的事情可以做:

<xsl:template match="//x:tr[following-sibling::x:tr[1]/@class='xxx']/x:td"> 

(未測試!)

+0

謝謝@ hr_117此修改XSLT文檔是有效的。我用樣本編輯了帖子。 – d3im