24
A
回答
32
在XSLT 1.0中,您必須構建一個遞歸模板。這個樣式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text/text()" name="tokenize">
<xsl:param name="text" select="."/>
<xsl:param name="separator" select="','"/>
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<item>
<xsl:value-of select="normalize-space($text)"/>
</item>
</xsl:when>
<xsl:otherwise>
<item>
<xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
</item>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
輸入:
<root>
<text>Item1, Item2, Item3</text>
</root>
輸出:
<root>
<text>
<item>Item1</item>
<item>Item2</item>
<item>Item3</item>
</text>
</root>
在XSLT 2.0你有tokenize()
核心功能。所以,這個樣式表:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text/text()" name="tokenize">
<xsl:param name="separator" select="','"/>
<xsl:for-each select="tokenize(.,$separator)">
<item>
<xsl:value-of select="normalize-space(.)"/>
</item>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
結果:
<root>
<text>
<item>Item1</item>
<item>Item2</item>
<item>Item3</item>
</text>
</root>
2
沒有split
功能,但你可以使用遞歸模板與substring-before
和substring-after
寫你自己的。
查看this文章的詳細信息。
3
1
謝謝user357812。我用你的漂亮的模板很少定製,使它通用:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Main template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="tokenize-children" />
</xsl:copy>
</xsl:template>
<!-- Split child nodes -->
<xsl:template match="*" mode="tokenize-children">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="*" mode="tokenize" />
</xsl:copy>
</xsl:template>
<!-- Tokenize text node of child nodes -->
<xsl:template match="*/text()" name="tokenize" mode="tokenize">
<xsl:param name="text" select="."/>
<xsl:param name="separator" select="','"/>
<xsl:variable name="item" select="name(..)" />
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<xsl:element name="{$item}">
<xsl:value-of select="normalize-space($text)"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{$item}">
<xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
</xsl:element>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
0
取決於你所使用的XSL處理器,您可以訪問的擴展功能str:tokenize()。
因此要拆分Topic1,Topic2,Topic3
在,
做;
<xsl:copy-of select="str:tokenize('Topic1,Topic2,Topic3', ',')"/>
這會給出結果;
<token>Topic1</token>
<token>Topic2</token>
<token>Topic3</token>
1
XSLT 1.0
我需要一個輕微變種相比,這裏給出的答案。
輸入:
1,2,3
輸出繼電器:
1,2和3
輸入:
輸出
如果分隔符是空間,而不是逗號,它仍然會工作。
輸入:
輸出繼電器:
1,2和3
我剛剛創建了一個稍微修改過的模板。
<xsl:template name="tokenizeString">
<xsl:param name="list"/>
<xsl:param name="delimiter"/>
<xsl:choose>
<xsl:when test="contains($list, $delimiter)">
<xsl:variable name="listLength" select="string-length($list)" />
<xsl:variable name="listLengthWithoutDelimiters" select="string-length(translate($list, $delimiter,''))" />
<xsl:variable name="noOfDelimiters" select="($listLength - $listLengthWithoutDelimiters)" />
<xsl:value-of select="substring-before($list,$delimiter)"/>
<xsl:if test="$noOfDelimiters > 1">, </xsl:if>
<xsl:if test="$noOfDelimiters = 1"> and </xsl:if>
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list" select="substring-after($list,$delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="$list = ''">
<xsl:text/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$list"/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
該模板可以被稱爲當分隔符是逗號
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list">1, 2, 3</xsl:with-param>
<xsl:with-param name="delimiter">
<xsl:value-of select="','" />
</xsl:with-param>
</xsl:call-template>
該模板可以被稱爲當分隔符是空間
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list">1 2 3</xsl:with-param>
<xsl:with-param name="delimiter">
<xsl:value-of select="' '" />
</xsl:with-param>
</xsl:call-template>
以下的
相關問題
- 1. XSLT是否具有Split()函數?
- 2. Split函數2008
- 3. ASP.NET是否具有VB的InputBox函數?
- 4. VB6類是否具有析構函數?
- 5. Split函數在JavaScript
- 6. 的JavaScript Split函數
- 7. 正則表達式實現是否需要split()函數?
- 8. Javascript:.split返回:'不是函數錯誤'
- 9. 是否有可能在XSLT
- 10. 想不通split()函數
- 11. java函數split()的用法
- 12. LINQ的Split函數查詢
- 13. 是否有像XSLT的doxygen這樣的工具?
- 14. idris是否有模函數?
- 15. python是否有「causes_exception()」函數?
- 16. 是否有Cells()。Formula函數?
- 17. 在WebLogic中調用具有相對路徑的xslt document()函數
- 18. 檢測類是否具有帶簽名的構造函數
- 19. Rust是否具有與Python的unichr()函數等效的功能?
- 20. SQL Server是否具有Excel的KURT函數的等效功能?
- 21. 是否可以使共享函數具有通用性?
- 22. SQL是否具有與dax中的datevalue()等效的函數?
- 23. 如何檢測Delphi類是否具有虛擬構造函數?
- 24. 鍵入traits以檢查類是否具有成員函數
- 25. pyodbc是否具有執行標量函數
- 26. SQL彙總函數,顯示組中是否具有同類值
- 27. Codename是否支持Split Pane UI組件?
- 28. 檢查php中的split()是否工作
- 29. XSLT中是否有用於userCSharp的已定義C#函數列表?
- 30. 是否有XSLT名稱元素?
可能[XSLT是否具有Split()函數?](http://stackoverflow.com/questions/136500/does-xslt-have-a-split-function) – 2014-08-14 17:25:19