2010-10-14 60 views
11

我需要顯示在評論的HTML元素(例如)輸出元件

<!-- <img src="path" width="100px" height="100px"/> --> 

我使用這種方法

<?xml version="1.0" encoding="windows-1251"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html" indent="no" encoding="windows-1251"/> 

    <xsl:template match="myNode"> 
     ... 
     <xsl:comment><xsl:apply-templates select="image" /></xsl:comment> 
     ... 
    </xsl:template> 

    <xsl:template match="image"> 
     <img src="{@src}" width="{@width}px" height="{@height}px" /> 
    </xsl:template> 

</xsl:stylesheet> 

結果:

<!----> 

那是元素xsl:comment中的代碼被忽略。

如何在評論中顯示項目?

+0

好問題,+1。根據具體任務,查看我的答案以獲得詳細的解釋和三種可能的方法。 – 2010-10-14 12:59:15

+0

我剛編輯我的答案,並認爲現在它有你想要的解決方案。這是嗎? – 2010-10-14 15:32:19

回答

10

可能可以與

<xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>
<xsl:apply-templates select="image" />
<xsl:text disable-output-escaping="yes">--&gt;</xsl:text>

有沒有試過,雖然取代

<xsl:comment><xsl:apply-templates select="image" /></xsl:comment>

1

http://www.w3.org/TR/xslt#section-Creating-Comments

了xsl:comment元素被實例化,以創建結果樹註釋節點。 xsl:comment元素的內容是註釋節點的字符串值的模板。

例如,這

<xsl:comment>This file is automatically generated. Do not edit!</xsl:comment>

將創建註釋

<!--This file is automatically generated. Do not edit!-->

這是一個錯誤,如果實例化 內容的xsl:評論創建一個比節點 其他文本節點。 XSLT 處理器可能發出錯誤信號;如果它 不表示錯誤,則必須通過忽略違規的 節點及其內容來恢復它。

這是一個錯誤,如果的 結果實例化 XSL的內容:註釋包含字符串--或 與-結束。 XSLT處理器可能會通知 錯誤;如果 沒有發出錯誤信號,則必須通過 來恢復,在發生任何事件後插入一個空格 的-,然後是另一個-或 ,以結束註釋。

所以,爲了做到你想要的,你需要使用DOE機制。

作爲例子,這個樣式表:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
exclude-result-prefixes="msxsl"> 
    <xsl:output method="html" indent="no" encoding="windows-1251"/> 
    <xsl:template match="img"> 
     <img src="{.}"/> 
    </xsl:template> 
    <xsl:template match="root"> 
     <xsl:variable name="vResult"> 
      <xsl:apply-templates/> 
     </xsl:variable> 
     <html> 
      <xsl:copy-of select="$vResult"/> 
      <xsl:comment> 
       <xsl:apply-templates select="msxsl:node-set($vResult)" 
            mode="encode"/> 
      </xsl:comment> 
     </html> 
    </xsl:template> 
    <xsl:template match="*" mode="encode"> 
     <xsl:value-of select="concat('&lt;',name())" 
         disable-output-escaping="yes"/> 
     <xsl:apply-templates select="@*" mode="encode"/> 
     <xsl:text>></xsl:text> 
     <xsl:apply-templates mode="encode"/> 
     <xsl:value-of select="concat('&lt;',name(),'>')" 
         disable-output-escaping="yes"/> 
    </xsl:template> 
    <xsl:template match="*[not(node())]" mode="encode"> 
     <xsl:value-of select="concat('&lt;',name())" 
         disable-output-escaping="yes"/> 
     <xsl:apply-templates select="@*" mode="encode"/> 
     <xsl:text>/></xsl:text> 
    </xsl:template> 
    <xsl:template match="@*" mode="encode"> 
     <xsl:value-of select="concat(' ',name(),'=&quot;',.,'&quot;')"/> 
    </xsl:template> 
</xsl:stylesheet> 

利用該輸入:

<root> 
    <img>http://example.org/image1.jpg</img> 
    <img>http://example.org/image2.jpg</img> 
    <img>http://example.org/image3.jpg</img> 
</root> 

輸出:

<html> 
    <img src="http://example.org/image1.jpg"> 
    <img src="http://example.org/image2.jpg"> 
    <img src="http://example.org/image3.jpg"> 
    <!--<img src="http://example.org/image1.jpg"/> 
     <img src="http://example.org/image2.jpg"/> 
     <img src="http://example.org/image3.jpg"/>--> 
</html> 

:用於兩遍變換node-set擴展功能。 xsl:value-of指令的disable-output-escaping屬性。

+0

@Alejandro:我不能在我們的項目中使用'node-set'(我無法激活此功能) – Kalinin 2010-10-15 12:32:48

+0

@Kalinin:使用'node-set'擴展函數大概是兩遍轉換,它不是關於什麼你需要:禁用輸出轉義,所以'<'字符不作爲實體引用'<'輸出。當然,如果你不使用雙通道變換,你必須爲我們的「僞元素」構建模板,就像我在這些模式下的「編碼」模板中一樣。另外,我確定你的處理器具有'node-set'擴展功能,但在不同的命名空間下。你使用哪種XSLT處理器? – 2010-10-15 12:50:23

7
<xsl:comment><xsl:apply-templates select="image" /></xsl:comment> 

結果:

<!----> 

那就是在元素 XSL代碼:發表評論忽略

The XSLT 1.0 Spec

如果實例化xsl:comment的 內容會創建除文本節點之外的節點 ,則會發生錯誤。 XSLT 處理器可能發出錯誤信號;如果它 不表示錯誤,則必須通過忽略違規的 節點及其內容來恢復它。

如何顯示 評論中的項目?

這要看是什麼意思爲「顯示」:在瀏覽器中:

&lt;-- <xsl:apply-templates select="image" /> --> 

可能是有用的,提供的<xsl:apply-templates/> aboveis只是簡單的文本(未標記)的結果。

如果以「顯示」是指以提供結果的文本,然後DOE,如果允許的XSLT處理器,可以給我們想要的結果:

< - 一些文本 - >

最後,如果要求「評論」內應該是標記,並且應該顯示爲標記,那麼這是相當具有挑戰性的。在這種情況下,一個具有使用:

<xsl:output method="text"/> 

和應提供每XML詞項與其期望的序列化(即,逸出)。

這是XPath Visualizer構造其輸出的方式。

這裏是一個小變換演示前兩種方法:當在任何XML文檔(未使用)施加

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="/"> 
     &lt;-- Hello, World --> 

    <xsl:text disable-output-escaping="yes">&lt;--</xsl:text> 
    Hello,world! --<xsl:text disable-output-escaping="yes">&gt;</xsl:text> 
</xsl:template> 
</xsl:stylesheet> 

這個變換,產生

 &lt;-- Hello, World --&gt; 

    <-- 
    Hello,world! --> 

這兩個「評論」都可以在瀏覽器中看作評論,而只有第二個評論以自由文本形式發表。

第三種方法(最可能是你想要1)表示如下

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="/"> 
    &lt;-- <xsl:apply-templates select="image"/> --> 

</xsl:template> 

<xsl:template match="image"> 
    &lt;img src="<xsl:value-of select="@src"/>" 
     width="<xsl:value-of select="@width"/>px" 
     height="<xsl:value-of select="@height"/>px"/> 
</xsl:template> 
</xsl:stylesheet> 

當下面的XML文檔應用這種轉變:

<image src="http://example.com/yyy.jpg" width="200" height="300"/> 

通緝結果產生

&lt;-- 
    &lt;img src="http://example.com/yyy.jpg" 
     width="200px" 
     height="300px"/&gt; 
    --&gt; 

在瀏覽器中被視爲

< - < IMG SRC = 「http://example.com/yyy.jpg」 寬度= 「200像素」 高度= 「300像素」/ > - >

0

如Dimitri所述,您不能使用xsl:comment指令。

如果你的目的只是爲了評樹的片段,最簡單的辦法就是把註釋標記文本(轉義)像這樣的:

<xsl:text disable-output-escaping="yes">&lt;!--</xsl:text><xsl:apply-templates select="image" /><xsl:text disable-output-escaping="yes">--&gt;</xsl:text> 

代替:

<xsl:comment><xsl:apply-templates select="image" /></xsl:comment> 

你就會得到這個

<!-- <img src="path" width="100px" height="100px"/> --> 

與msxml和撒克遜一起使用

+0

我無法看到這個答案與http://stackoverflow.com/a/3933175/272735 – user272735 2014-07-25 06:15:52