2009-12-07 62 views
5

請幫幫我吧。我只是試圖聲明一個簡單的結果樹片段並對其進行迭代。XSL msxsl:節點集問題


...

<xsl:variable name="rtf"> 
    <item-list> 
    <item id="1">one</item> 
    <item id="2">two</item> 
    <item id="3">three</item> 
    <item id="4">four</item> 
    </item-list> 
</xsl:variable> 

<xsl:for-each select="msxsl:node-set($rtf)/item-list/item"> 
    <xsl:value-of select="@id"/> 
</xsl:for-each> 

...


我是完全弄錯了這是如何工作?


編輯: 我使用.NET XslCompiledTransform,並有正確的msxsl命名空間聲明 - 的xmlns:msxsl = 「甕:架構 - 微軟COM:XSLT」

的transformating執行罰款 - 問題是什麼都沒有輸出

+0

我不認爲多數民衆贊成在足夠的代碼,並關閉自身實際輸出什麼... – Murph 2009-12-07 15:04:38

+0

我米並不確定你的意思,但這是整個xslt文件的一個片段。一切高於和低於輸出就好了。它只是rtf和節點集,它們不像預期的那樣運行。 – Maleks 2009-12-07 17:06:04

回答

8

我懷疑是你在你的樣式表聲明的默認命名空間。這將有效地將< item-list>和< item>元素放置到名稱空間中。要使用XPath 1.0選擇符合命名空間限定的元素,您必須始終在表達式中使用前綴。

所以,如果你在你的樣式表的頂部有這樣的事情:

<xsl:stylesheet xmlns="http://example.com"...> 

然後,你還需要補充一點:

<xsl:stylesheet xmlns="http://example.com" xmlns:x="http://example.com"...> 

然後使用「X」字頭在您的XPath表達式中:

<xsl:for-each select="msxsl:node-set($rtf)/x:item-list/x:item"> 
    <xsl:value-of select="@id"/> 
</xsl:for-each> 

讓我知道這是否有竅門。我只是在這裏猜測。

+0

天才。作品一種享受。 – Maleks 2009-12-08 13:22:43

+0

你的猜測是對的! – Safor 2014-05-21 13:56:40

1

這對我來說很合適。

您是否正確地爲擴展函數聲明瞭msxsl名稱空間?事情是這樣的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"> 

我假設你正在使用Microsoft XSLT處理器這裏

+0

是的,使用MS技術並註冊了正確的名稱空間。 – Maleks 2009-12-07 13:25:45

4

不像MSXSL,XslCompiledTransform提供node-set()功能它是supposed to be - 在EXSLT公共的命名空間:

<xsl:stylesheet xmlns:exslt="http://exslt.org/common"> 
    ... 
    <xsl:for-each select="exslt:node-set($rtf)/item-list/item"> 
    ... 
</xsl:stylesheet> 
+0

謝謝你的擡頭。 – Maleks 2009-12-08 13:23:24

+0

@Pavel Minaev非常感謝!我使用xslt和python,在我的情況下,這是一個很好的解決方案。 – daikini 2012-01-03 03:16:11