如何通過逗號分隔的字符串進行循環,這是我在XSLT 1.0中作爲參數傳遞的字符串? EX-逗號分隔的字符串解析XSLT
<xsl:param name="UID">1,4,7,9</xsl:param>
我需要從循環中的每個UID的上述UID參數和collectd節點在我的XML文件
如何通過逗號分隔的字符串進行循環,這是我在XSLT 1.0中作爲參數傳遞的字符串? EX-逗號分隔的字符串解析XSLT
<xsl:param name="UID">1,4,7,9</xsl:param>
我需要從循環中的每個UID的上述UID參數和collectd節點在我的XML文件
這是一個使用str-split-to-words
模板FXSL的XSLT 1.0解決方案。
注意,該模板允許分割在多個定界符(作爲單獨的參數字符串傳遞),因此,即使將1,4 7;9
不使用此解決方案的任何問題進行拆分。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
>
<xsl:import href="strSplit-to-Words.xsl"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select="/"/>
<xsl:with-param name="pDelimiters"
select="', ;	 '"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
當下面的XML文檔應用這種轉變:
<x>1,4,7,9</x>
想要的,正確的結果產生:
<word>1</word>
<word>4</word>
<word>7</word>
<word>9</word>
謝謝。循環這個逗號分隔參數的語法是什麼?試過上面的解決方案,但出現錯誤「找不到執行前綴'http://fxsl.sf.net/'的腳本或外部對象。」 我想我可能沒有正確下載文件。 那麼,我只需要匹配這個逗號分隔的字符串參數中的每個id值與XML節點id,如果發現匹配收集每個節點的文本顯示在輸出XML中。請注意,我必須在XSLT 1.0中執行此操作。 – contactkx 2010-05-18 11:55:50
@contactx:你的意思是你不能運行我的轉換?你下載了FXSL 1.x嗎? '
香草XSLT 1.0可以通過遞歸解決這個問題。
<xsl:template name="split">
<xsl:param name="list" select="''" />
<xsl:param name="separator" select="','" />
<xsl:if test="not($list = '' or $separator = '')">
<xsl:variable name="head" select="substring-before(concat($list, $separator), $separator)" />
<xsl:variable name="tail" select="substring-after($list, $separator)" />
<!-- insert payload function here -->
<xsl:call-template name="split">
<xsl:with-param name="list" select="$tail" />
<xsl:with-param name="separator" select="$separator" />
</xsl:call-template>
</xsl:if>
</xsl:template>
有預先內置的擴展庫,可以做字符串標記化(EXSLT有一個模板,例如),但我懷疑,這是真的有必要在這裏。
你碰巧有'string-join'的遞歸模板嗎? – n611x007 2015-04-21 17:56:36
很好的問題(+1)。查看我的答案,找到不涉及顯式遞歸的解決方案。 – 2010-05-17 16:37:22