xml – xslt 1.0中的split函数

前端之家收集整理的这篇文章主要介绍了xml – xslt 1.0中的split函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何拆分XSLT 1.0中的节点值?
  1. <mark>1,2</mark>

我需要在for循环中执行一些操作,其中每个值都是split的输出.

< xsl:for-each select =“”>
< /的xsl:for-每个>

怎么办?

I. XSLT 1.0解决方案:

以下是XSLT 1.0中仅使用xxx:node-set()扩展功能的一种方法

  1. <xsl:stylesheet version="1.0"
  2. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
  4. <xsl:output omit-xml-declaration="yes" indent="yes"/>
  5.  
  6. <xsl:template match="mark">
  7. <xsl:variable name="vrtfSplit">
  8. <xsl:apply-templates/>
  9. </xsl:variable>
  10.  
  11. <xsl:for-each select="ext:node-set($vrtfSplit)/*">
  12. <processedItem>
  13. <xsl:value-of select="10 * ."/>
  14. </processedItem>
  15. </xsl:for-each>
  16. </xsl:template>
  17.  
  18. <xsl:template match="text()" name="split">
  19. <xsl:param name="pText" select="."/>
  20. <xsl:if test="string-length($pText) >0">
  21. <item>
  22. <xsl:value-of select=
  23. "substring-before(concat($pText,','),')"/>
  24. </item>
  25.  
  26. <xsl:call-template name="split">
  27. <xsl:with-param name="pText" select=
  28. "substring-after($pText,')"/>
  29. </xsl:call-template>
  30. </xsl:if>
  31. </xsl:template>
  32. </xsl:stylesheet>

当此转换应用于以下XML文档时:

  1. <mark>1,2,3,4,5</mark>

产生想要的,正确的输出(每个项目乘以10):

  1. <processedItem>10</processedItem>
  2. <processedItem>20</processedItem>
  3. <processedItem>30</processedItem>
  4. <processedItem>40</processedItem>
  5. <processedItem>50</processedItem>

II. XSLT 2.0解决方案:

  1. <xsl:stylesheet version="2.0"
  2. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:xs="http://www.w3.org/2001/XMLSchema"
  4. exclude-result-prefixes="xs">
  5. <xsl:output omit-xml-declaration="yes" indent="yes"/>
  6.  
  7. <xsl:template match="mark">
  8. <xsl:for-each select="tokenize(.,')">
  9. <processedItem>
  10. <xsl:sequence select="10*xs:integer(.)"/>
  11. </processedItem>
  12. </xsl:for-each>
  13. </xsl:template>
  14. </xsl:stylesheet>

猜你在找的XML相关文章