2013-07-22 18 views
0

我有XML轉義雙引號時,空間和允許額外的前斜線

<?xml version="1.0" encoding="UTF-8"?> 
<icestats> 
    <stats_connections>0</stats_connections> 
    <source mount="/live"> 
     <bitrate>Some data</bitrate> 
     <server_description>This is what I want to return</server_description> 
    </source> 
</icestats> 

而且我有XSL

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="/"> 
     <xsl:copy-of select="/icestats/source mount="/live"/server_description/node()" /> 
    </xsl:template> 
</xsl:stylesheet> 

我想輸出

This is what I want to return 

如果我刪除雙引號,空格和正斜槓來源於它的工作原理,但我還沒有能夠成功地逃脫非標準字符在其他帖子中使用建議的方法。


爲清楚起見,下面是解決由於樂高Stormtroopr

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="/"> 
     <xsl:copy-of select="/icestats/source[@mount='/live']/server_description/node()" /> 
    </xsl:template> 
</xsl:stylesheet> 

回答

1

在你的處理器產生你正在尋找的輸出之前,你需要解決幾個問題。

1)您的XML輸入必須格式良好。 source元素的結束標記不應包含在開始標記上指定的mount屬性。

<source mount="/live"> 
    ... 
</source> 

2)中的XPath您xsl:copy-of元素必須有效。 (幸運的是)XPath表達式的語法不像XML元素和屬性的語法。指定source元素,以匹配其被斷言一個屬性值,像你這樣做,除了你需要用方括號完成:

/icestats/source[@mount="/live"]/server_description 

爲了在XSLT select語句中使用此XPath表達式,你將需要確保將整個select屬性值與一種類型的引號括起來,並在屬性值中使用其他類型的引號,例如:

<xsl:value-of select="/icestats/source[@mount='/live']/server_description" /> 

與該輸入

<?xml version="1.0" encoding="UTF-8"?> 
<icestats> 
    <stats_connections>0</stats_connections> 
    <source mount="/live"> 
     <bitrate>Some data</bitrate> 
     <server_description>This is what I want to return</server_description> 
    </source> 
</icestats> 

和這個樣式表

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="text"/> 
    <xsl:template match="/"> 
     <xsl:value-of select="/icestats/source[@mount='/live']/server_description" /> 
    </xsl:template> 
</xsl:stylesheet> 

我獲取文本從在xsltproc和薩克森以下行:

This is what I want to return 

xsl:value-of元素無線將返回一個元素的字符串值(這裏是一個文本節點)。如果你真的想要server_description元素,那麼你可以使用xsl:copy-of來獲取整個事物,標籤和所有內容。 (您必須更新xsl:output。)

+0

非常感謝。 'copy-of'正在返回正確的輸出,儘管我有一種'value-of'的感覺更適合。雖然我不完全瞭解它們之間的區別(除了你所說的......以某種方式包含標籤)。 –

+0

最終答案中的「XPath副本」包含一個額外的節點測試node(),它會導致選擇「server_description」的文本節點子節點。 XPath的'value-of'只導致'server_description'被選中,但'value-of'返回該元素的字符串值。在這個例子中給定'server_description'的內容,那兩個將產生相同的輸出。 – 2013-07-23 04:40:46

0

我認爲,所有你需要做的就是逃避屬性字符串中的引號與&quot;

<xsl:copy-of select="/icestats/source mount=&quot;/live&quot;/server_description/node()" /> 
+0

這不是解析。我確實嘗試過。難道是節點標題中的額外正斜槓混淆了「copy-of select」? –

+0

@square_eyes - 啊。如果你已經解釋了你實際嘗試過的內容(而不是模糊的「其他帖子中的建議方法」),那將會很有幫助。 –

+0

沒有傷害。感謝您的幫助:) –

1

看起來您正在根據屬性進行選擇,因此您只需在XPath中正確捕獲屬性即可。您的文檔和XPath的使用中的引號並不需要匹配,因此可以將其切換到單引號('):

<xsl:copy-of select="/icestats/source[@mount='/live']/server_description/node()" /> 

(編輯以從mount屬性缺失/

此外,您的原始文檔不是有效的XML,因爲XML不允許結束標記中的屬性。

+0

好的謝謝,好像它在正確的軌道上。我沒有想到這一點。它是解析,但結果是空白的。 抱歉關閉標籤。不像那樣。我已經修改了這個問題。 –

+0

明白了,你忘記了正斜槓。謝謝您的幫助。我會在問題中發佈完整的解決方案。 –

+0

對不起,我的壞。 – 2013-07-23 00:02:47