2013-02-27 64 views
2

我在尋找將XSL嵌入到XML中的解決方案,因此只有1個XML文件發送到瀏覽器。我試圖通過Dimitre Novatchev這裏提出的解決方案:Embed xsl into an XML file如何將XSL嵌入到XML中

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes"/> <xsl:variable name="vEmbDoc"> 
    <doc> 
     <head></head> 
     <body> 
      <para id="foo">Hello I am foo</para> 
     </body> 
    </doc> 
</xsl:variable> 
<xsl:template match="para"> 
    <h1><xsl:value-of select="."/></h1> 
</xsl:template> 
<xsl:template match="xsl:template"/></xsl:stylesheet> 

的問題是,這種解決方案我無法找到一種方法,包括頭內部的風格元素。似乎在建議的解決方案中,head和body標記沒有任何效果,因爲瀏覽器會在解析過程中自動添加它們,即使沒有包含這些標記,解決方案也能正常工作。

所以現在的問題是:如何包括在上述方案中的頭型元素,將是這樣的:

<head><style>body {font-size:10pt;padding:20pt} </style></head> 
+0

那麼,什麼是確切想要的結果?請編輯問題並提供這些重要的缺失信息。 – 2013-02-28 03:14:32

+0

嗨Dimitre,謝謝你的回覆。我想包括在頭喜歡這種風格的元素: user2116792 2013-02-28 04:10:16

回答

1

此XML文檔

<?xml-stylesheet type="text/xsl" href="myEmbedded.xml"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
exclude-result-prefixes="xsl"> 
<xsl:output omit-xml-declaration="yes"/> 
    <xsl:variable name="vEmbDoc"> 
     <doc> 
      <head> 
       <style>body {font-size:10pt;padding:20pt}</style> 
       </head> 
      <body> 
       <para id="foo">Hello I am foo</para> 
      </body> 
     </doc> 
    </xsl:variable> 
    <xsl:template match="para"> 
     <h1><xsl:value-of select="."/></h1> 
    </xsl:template> 

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

<xsl:template match="doc"> 
    <html> 
    <xsl:apply-templates/> 
    </html> 
</xsl:template> 

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

    <xsl:template match="xsl:*"> 
     <xsl:apply-templates/> 
    </xsl:template> 
</xsl:stylesheet> 

包含一個XSLT樣式表。開始的PI指示瀏覽器在其本身上應用此樣式表。

這樣的轉變規定,產生想要的結果

<html> 

    <head xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 

       <style>body {font-size:10pt;padding:20pt}</style> 
       </head> 

    <body xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

     <h1>Hello I am foo</h1> 

    </body> 

</html> 
+0

非常感謝迪米特里·!這工作。 – user2116792 2013-02-28 14:22:20

+0

謝謝你讓我知道如何接受答案!這是我在這個論壇的第一個問題,所以我正在尋找一個按鈕,說「接受」,但無法找到它。它從來沒有跨過我的腦海,這個複選標記不只是一張圖片:) – user2116792 2013-02-28 18:14:39

+0

@ user2116792,不客氣。 – 2013-02-28 18:24:32