2011-11-25 129 views
2

我是xsl xml轉換的新手。現在,我有一個包含以下信息的XML文件:xml,用CDATA進行xsl轉換

<bio> 
<published>Tue, 7 Oct 2008 14:47:26 +0000</published> 
<summary><![CDATA[ 
    Dream Theater is an American <a 
    href="http://www.last.fm/tag/progressive%20metal" class="bbcode_tag" 
    rel="tag">progressive metal</a> band formed in 1985 under the name 
    &quot;<a href="http://www.last.fm/music/Majesty" 
    class="bbcode_artist">Majesty</a>&quot; by <a 
    href="http://www.last.fm/music/John+Myung" 
    class="bbcode_artist">John Myung</a>, 
    <a href="http://www.last.fm/music/John+Petrucci" 
    class="bbcode_artist">John Petrucci</a> 
]]> 
</summary> 
</bio> 

我的XSL文件包含此:

<h3><xsl:value-of select="lfm/artist/bio/published"/></h3> 
<p> 
    <xsl:value-of select="lfm/artist/bio/summary" disable-output-escaping="yes"/> 
</p> 
<html> 
    <body> 
     <xsl:value-of select="lfm/artist/bio/content"/> 
    </body> 
</html> 

我試圖現在要做的是提取結構化標籤數據出<summary><[CDATA[]]></summary>的,並顯示在瀏覽器如本例:

<a href="http://www.last.fm/tag/progressive%20metal" class="bbcode_tag" rel="tag">progressive metal</a> 
<a href="http://www.last.fm/music/Majesty" class="bbcode_artist">Majesty</a> 
<a href="http://www.last.fm/music/John+Myung" class="bbcode_artist">John Myung</a> 
<a href="http://www.last.fm/music/John+Petrucci" class="bbcode_artist">John Petrucci</a> 

現在當我打開XML頁面,它顯示了所有CDATA內容,甚至與那些HTM l標籤...我想讓這些標籤以html的形式正確地完成他們的工作。

遺憾的可怕description..hope你們能明白我的意思在這裏...

+0

-1對於可怕的描述:你並沒有真正努力去更好地描述它,是嗎? +1對於這個問題本身(如果我沒有誤解你)= 0.順便說一句,我沒有簡化你原來的'xml'的內容,因爲已經有一個答案提及它。你可以簡化它,只是縮短網址,並刪除不相關的信息! – Alberto

回答

4

的CDATA只是一個文本節點(的一部分),什麼好像標記裏面是單(嚴重破壞的標記),這不能在不調用擴展函數的情況下完成(在XSLT 1.0和XSLT 2.0中)。

<p><xsl:copy-of select="my:parse(lfm/artist/bio/summary)"></p>

在XSLT 3.0有可能是一個新的標準功能parse-xml()認爲正是這一點

更新

下面是一個完整的代碼示例,假設您在.NET使用XslCompiledTransform

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
xmlns:my="my:my"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="summary/text()"> 
    <xsl:copy-of select="my:parse(.)/*/*"/> 
</xsl:template> 

<msxsl:script language="c#" implements-prefix="my"> 
    public XmlDocument parse(string text) 
    { 
    XmlDocument doc = new XmlDocument(); 
    doc.LoadXml("&lt;t>"+text+"&lt;/t>"); 

    return doc; 
    } 
</msxsl:script> 
</xsl:stylesheet> 

當這種轉化應用所提供的XML文檔

<bio> 
<published>Tue, 7 Oct 2008 14:47:26 +0000</published> 
<summary><![CDATA[Dream Theater is an American <a href="http://www.last.fm/tag/progressive%20metal" class="bbcode_tag" rel="tag">progressive metal</a> band formed in 1985 under the name &quot;<a href="http://www.last.fm/music/Majesty" class="bbcode_artist">Majesty</a>&quot; by <a href="http://www.last.fm/music/John+Myung" class="bbcode_artist">John Myung</a>, <a href="http://www.last.fm/music/John+Petrucci" class="bbcode_artist">John Petrucci</a>]]> 
</summary> 
</bio> 

的希望,正確的結果(CDATA由重組標記代替)產生

<bio> 
    <published>Tue, 7 Oct 2008 14:47:26 +0000</published> 
    <summary> 
    <a href="http://www.last.fm/tag/progressive%20metal" class="bbcode_tag" rel="tag">progressive metal</a> 
    <a href="http://www.last.fm/music/Majesty" class="bbcode_artist">Majesty</a> 
    <a href="http://www.last.fm/music/John+Myung" class="bbcode_artist">John Myung</a> 
    <a href="http://www.last.fm/music/John+Petrucci" class="bbcode_artist">John Petrucci</a> 
    </summary> 
</bio> 
+0

實際上,通過你給我的代碼...我仍然無法獲得

內的那些html標籤以正確顯示在瀏覽器上...還有什麼建議嗎? thx – sefirosu

+0

@sefirosu:我想你還沒有編碼擴展功能。創建分機有不同的規則。函數,具體取決於您使用的特定XSLT處理器。如果您讓我們知道您使用的XSLT處理器/操作系統,人們將能夠指向您的資源,描述如何爲該特定的XSLT處理器編寫擴展函數。把這個問題作爲一個單獨的問題來看可能是個好主意。例如,如果您使用.NET和XslCompiledTransform,請閱讀以下內容:http://msdn.microsoft.com/en-us/library/system.xml.xsl.xsltargumentlist.addextensionobject.aspx –

+0

@sefirosu:我更新了我的回答完整的代碼,可用於.NET XslCompiledTransform XSLT處理器。 –