2011-02-23 52 views
2

我試圖使用xslt將xhtml文檔中的div部分提取到另一個xhtml文檔中。但是,我沒有成功。而是,xslt轉換導致了有線輸出。假設下面的XHTML文檔轉換:如何從一個xhtml文檔中提取div部分轉換爲另一個xhtml文檔

<?xml version="1.0" encoding="iso-8859-1"?> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="de"> 
<head> 
    <title>title test</title> 
</head> 
<body> 
    some blabla 
    <div> 
     <div id="testid" class="testclass"> 
     hello world! 
     </div> 
    </div> 
    some other blabla <p/> 
    test paragraph<p/> 
</body> 
</html> 

的XSLT應提取id爲「testid」的DIV部分,並將其寫入到一個新的XHTML文件,該文件應該如下所示:

<?xml version="1.0" encoding="iso-8859-1"?> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="de"> 
<head> 
    <title>title test</title> 
</head> 
<body> 
    <div id="testid" class="testclass"> 
     hello world! 
    </div> 
</body> 
</html> 

我XSLT代碼如下:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" indent="yes" omit-xml-declaration="yes"/> 
    <xsl:template match="/"> 
    <html xmlns="http://www.w3.org/1999/xhtml" lang="de"> 
     <head> 
     </head> 
     <body> 
     <xsl:apply-templates select="node()|text()"/> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="*"> 
    <xsl:if test="div[@id='testid']"> 
     <xsl:copy-of select="*"/> 
    </xsl:if> 
    <xsl:apply-templates select="node()|@*" /> 
    </xsl:template> 

</xsl:stylesheet> 

輸出實際上如下:

<html xmlns="http://www.w3.org/1999/xhtml" lang="de"> 
    <head/> 
    <body>de 

    title test 

    some blabla 


    testidtestclass 
    hello world! 


    some other blabla 
    test paragraph 

    </body> 
</html> 

我需要在xslt中更改以獲得正確結果?任何幫助表示讚賞。謝謝。

+0

問得好,+1。您的主要問題是您尚未在XSLT代碼中定義XHTML名稱空間。查看我的答案,獲得完整,簡短的解決方案。 :) – 2011-02-23 23:53:58

回答

2

這個樣式表(拉風):

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xhtml="http://www.w3.org/1999/xhtml" 
xmlns="http://www.w3.org/1999/xhtml" 
exclude-result-prefixes="xhtml"> 
    <xsl:template match="text()"/> 
    <xsl:template match="xhtml:div[@id='testid']"> 
     <html lang="de"> 
      <head></head> 
      <body> 
       <xsl:call-template name="identity"/> 
      </body> 
     </html> 
    </xsl:template> 
    <xsl:template match="node()|@*" mode="identity" name="identity"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*" mode="identity"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

有了這個輸入:

<?xml version="1.0" encoding="iso-8859-1"?> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="de"> 
    <head> 
     <title>title test</title> 
    </head> 
    <body>some blabla 
     <div> 
      <div id="testid" class="testclass">hello world!</div> 
     </div>some other blabla 
     <p>test paragraph</p> 
    </body> 
</html> 

輸出:

<html lang="de" xmlns="http://www.w3.org/1999/xhtml"> 
    <head></head> 
    <body> 
     <div id="testid" class="testclass">hello world!</div> 
    </body> 
</html> 

注意:從匹配節點Invoque身份規則。

這個樣式表(按風格):

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xhtml="http://www.w3.org/1999/xhtml" 
xmlns="http://www.w3.org/1999/xhtml" 
exclude-result-prefixes="xhtml"> 
    <xsl:template match="/"> 
     <html lang="de"> 
      <head></head> 
      <body> 
       <xsl:apply-templates select="//xhtml:div[@id='testid']"/> 
      </body> 
     </html> 
    </xsl:template> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

與 「磚」 模板:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xhtml="http://www.w3.org/1999/xhtml" 
xmlns="http://www.w3.org/1999/xhtml" 
exclude-result-prefixes="xhtml"> 
    <xsl:template match="/"> 
     <html lang="de"> 
      <head></head> 
      <body> 
       <xsl:copy-of select="//xhtml:div[@id='testid']"/> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 
+0

我注意到剛纔的xhtml命名空間問題。 +1 – 2011-02-23 23:48:32

0

這種轉變

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:x="http://www.w3.org/1999/xhtml" 
exclude-result-prefixes="x"> 
<xsl:output indent="yes" encoding="iso-8859-1"/> 


<xsl:template match="/"> 
    <html xmlns="http://www.w3.org/1999/xhtml" lang="de"> 
    <head/> 
    <body> 
    <xsl:apply-templates/> 
    </body> 
    </html> 
</xsl:template> 

<xsl:template match="x:div[@id='testid']"> 
    <xsl:copy-of select="."/> 
</xsl:template> 

<xsl:template match="text()"/> 
</xsl:stylesheet> 

時所提供的XML應用文件:

<html xmlns="http://www.w3.org/1999/xhtml" lang="de"> 
<head> 
    <title>title test</title> 
</head> 
<body> 
    some blabla 
    <div> 
    <div id="testid" class="testclass"> 
     hello world! 
    </div> 
    </div> 
    some other blabla <p/> 
    test paragraph<p/> 
</body> 
</html> 

產生想要的,正確的結果:

<?xml version="1.0" encoding="iso-8859-1"?> 
<html lang="de" xmlns="http://www.w3.org/1999/xhtml"> 
    <head /> 
    <body> 
    <div id="testid" class="testclass"> 
     hello world! 
    </div> 
    </body> 
</html> 
相關問題