XSL:从分隔字符串构造一个数组

Bug*_*ude 13 arrays xslt

我有一个字符串,其中数据由分隔符分隔"|",并且存在于变量中.我想在XSL中创建一个数组,通过基于分隔符划分上面的字符串,并希望在for循环中访问相同的数组.

请帮助我这方面.如果有人需要更多信息,请告诉我.

String是"Test1|Test2|Test3|Test4"并且想要获得一个变量TEMP,该变量将是来自字符串的数据数组并且想要访问TEMP[index].

我试图在论坛成员的输入后使用tokenize函数来获取字符串中的值,但是没有成功.我没有在循环中获取字符串值.

<xsl:variable name="temp" xmlns:str="http://exslt.org/strings" select="str:tokenize(normalize-space(' Test1$,$Test2$,$Test3$,$Test4 '),'$,$')"/>
<xsl:for-each xmlns:str="http://exslt.org/strings" select="str:split(normalize-space(' 1$,$2$,$3$,$4$,$5$,$6 '),'$,$')">
    <xsl:variable name="index" select="position()"/>
    <xsl:value-of select="$temp[$index]"/>
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)

此致,拉克什曼

Dim*_*hev 14

String是"Test1|Test2|Test3|Test4"并且想要获得一个变量TEMP,该变量 将是来自字符串的数据数组并且想要访问TEMP[index].

给一个好问题+1.

这个XSLT 1.0转换:

<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:variable name="vrtfTokens">
  <xsl:apply-templates/>
 </xsl:variable>

 <xsl:variable name="vTokens" select=
   "ext:node-set($vrtfTokens)/*"/>
 <xsl:template match="/">
  <xsl:for-each select=
   "document('')//node()[not(position() > count($vTokens))]
   ">
   <xsl:variable name="vPos" select="position()"/>
    <xsl:copy-of select="$vTokens[$vPos+0]"/>
  </xsl:for-each>
 </xsl:template>

 <xsl:template match="text()" name="split">
  <xsl:param name="pText" select="."/>

  <xsl:if test="string-length($pText)">
   <xsl:variable name="vToken" select=
    "substring-before(concat($pText,'|'), '|')"/>
   <s><xsl:value-of select="$vToken"/></s>

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

应用于此XML文档时:

<t>Test1|Test2|Test3|Test4</t>
Run Code Online (Sandbox Code Playgroud)

创建一个vTokens包含named元素的变量s,每个元素都有一个来自'|'分隔字符串的标记作为其唯一的文本子元素"Test1|Test2|Test3|Test4".

然后,转换s使用"索引" 输出这些元素中的每一个.

产生了想要的正确结果:

<s>Test1</s>
<s>Test2</s>
<s>Test3</s>
<s>Test4</s>
Run Code Online (Sandbox Code Playgroud)

如果我们只需要令牌(字符串)本身,我们将使用:

string($vTokens[someIndex])
Run Code Online (Sandbox Code Playgroud)


Vin*_*net 9

它不是一个数组而是一个序列,你必须拥有一个XSLT 2.0处理器.你可以使用这个tokenize()功能:

<xsl:variable name="temp" as="xs:string*" select="tokenize('Test1|Test2|Test3|Test4','\|')"/>
Run Code Online (Sandbox Code Playgroud)

您还可以将字符串变量作为tokenize的第一个参数传递.

然后你使用:

<xsl:value-of select="$temp[$index]"/>
Run Code Online (Sandbox Code Playgroud)

编辑:除非你使用一些扩展名,否则在xslt 1.0中实现这一点是不可能的.