xslt 1.0 中的拆分功能 [英] split function in xslt 1.0

查看:32
本文介绍了xslt 1.0 中的拆分功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 XSLT 1.0 中拆分节点值?

how to split a node value in XSLT 1.0?

<mark>1,2</mark>

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

i need to perform some operations in the for loop with each value of the output of split.

</xsl:for-each>

如何做到这一点?

推荐答案

I.XSLT 1.0 解决方案:

这是在 XSLT 1.0 中仅使用 xxx:node-set() 扩展函数来执行此操作的一种方法:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="mark">
  <xsl:variable name="vrtfSplit">
   <xsl:apply-templates/>
  </xsl:variable>

  <xsl:for-each select="ext:node-set($vrtfSplit)/*">
   <processedItem>
    <xsl:value-of select="10 * ."/>
   </processedItem>
  </xsl:for-each>
 </xsl:template>

 <xsl:template match="text()" name="split">
  <xsl:param name="pText" select="."/>
   <xsl:if test="string-length($pText) >0">
    <item>
     <xsl:value-of select=
      "substring-before(concat($pText, ','), ',')"/>
    </item>

    <xsl:call-template name="split">
     <xsl:with-param name="pText" select=
     "substring-after($pText, ',')"/>
    </xsl:call-template>
   </xsl:if>
 </xsl:template>
</xsl:stylesheet>

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

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

产生想要的、正确的输出(每一项乘以 10):

<processedItem>10</processedItem>
<processedItem>20</processedItem>
<processedItem>30</processedItem>
<processedItem>40</processedItem>
<processedItem>50</processedItem>

二.XSLT 2.0 解决方案:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="mark">
  <xsl:for-each select="tokenize(., ',')">
   <processedItem>
    <xsl:sequence select="10*xs:integer(.)"/>
   </processedItem>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

这篇关于xslt 1.0 中的拆分功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆