我想通過使用xsl:foreach的xml文檔進行迭代,但我需要select =「」纔是動態的,所以我使用變量作爲源。以下是我已經試過:在xsl:foreach select語句中使用xsl:變量
...
<xsl:template name="SetDataPath">
<xsl:param name="Type" />
<xsl:variable name="Path_1">/Rating/Path1/*</xsl:variable>
<xsl:variable name="Path_2">/Rating/Path2/*</xsl:variable>
<xsl:if test="$Type='1'">
<xsl:value-of select="$Path_1"/>
</xsl:if>
<xsl:if test="$Type='2'">
<xsl:value-of select="$Path_2"/>
</xsl:if>
<xsl:template>
...
<!-- Set Data Path according to Type -->
<xsl:variable name="DataPath">
<xsl:call-template name="SetDataPath">
<xsl:with-param name="Type" select="/Rating/Type" />
</xsl:call-template>
</xsl:variable>
...
<xsl:for-each select="$DataPath">
...
在foreach拋出一個錯誤,指出:「XslTransformException - 要使用一個路徑表達式中的結果樹片段,首先使用msxsl:node-set()函數將其轉換爲節點集。「
但是,當我使用msxsl:node-set()函數時,我的結果爲空。
我知道我將$ DataPath設置爲一個字符串,但不應該從node-set()函數創建節點集嗎?我錯過了什麼嗎?當我不使用變量時:
<xsl:for-each select="/Rating/Path1/*">
我得到了正確的結果。
下面是我使用的XML數據文件:
<Rating>
<Type>1</Type>
<Path1>
<sarah>
<dob>1-3-86</dob>
<user>Sarah</user>
</sarah>
<joe>
<dob>11-12-85</dob>
<user>Joe</user>
</joe>
</Path1>
<Path2>
<jeff>
<dob>11-3-84</dob>
<user>Jeff</user>
</jeff>
<shawn>
<dob>3-5-81</dob>
<user>Shawn</user>
</shawn>
</Path2>
</Rating>
我的問題很簡單,你怎麼跑2條不同路徑的foreach?
很難在不說至少看到一個XML片段。 – Oded 2010-03-15 17:15:20