简体   繁体   English

XSLT 2.0:将字符串拆分为逗号分隔值

[英]XSLT 2.0 : Split a string into comma separated values

I am new to XSLT and have a requirement where in i have to manipulate a string as below.我是 XSLT 的新手,并且有一个要求,我必须如下操作字符串。

Input string = "12345"输入字符串 = "12345"

Output expected ="12345,1234,123,12"预期输出="12345,1234,123,12"

Can anybody help me to achieve this in XSLT 2.0任何人都可以帮助我在 XSLT 2.0 中实现这一点

This should do the trick:这应该可以解决问题:

<xsl:template match="/">
  <xsl:call-template name="minus-one">
    <xsl:with-param name="input" select="'12345'"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="minus-one">
  <xsl:param name="input"/>

  <xsl:value-of select="$input"/>

  <xsl:if test="string-length($input) gt 2"><xsl:text>,</xsl:text>
    <xsl:call-template name="minus-one">
      <xsl:with-param name="input" select="substring($input, 1, string-length($input) - 1)"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

Here is some XSLT/XPath 2.0 approach:这是一些 XSLT/XPath 2.0 方法:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:mf="http://example.com/mf"
  exclude-result-prefixes="xs mf"
  version="2.0">

  <xsl:function name="mf:sub-sequences" as="xs:string*">
    <xsl:param name="input" as="xs:string"/>
    <xsl:param name="min-length" as="xs:integer"/>
    <xsl:sequence select="reverse(
                            for $length in $min-length to string-length($input)
                            return substring($input, 1, $length)
                          )"/>
  </xsl:function>

  <xsl:template name="main">
    <xsl:variable name="s" select="'12345'"/>
    <xsl:value-of select="mf:sub-sequences($s, 2)" separator=","/>
  </xsl:template>

</xsl:stylesheet>

Here is a more efficient solution than the currently accepted one that doesn't use the reverse() function :这是一种比当前接受的不使用reverse()函数的解决方案更有效的解决方案:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:my="my:my">
    <xsl:output method="text"/>

 <xsl:function name="my:subSequences" as="xs:string*">
   <xsl:param name="pString" as="xs:string"/>
   <xsl:param name="pstartLength" as="xs:integer"/>
   <xsl:sequence select=
     "for $totalLength in string-length($pString),
          $length in 1 to $totalLength -$pstartLength +1,
          $revLength in $totalLength -$length +1
               return
                  substring($pString, 1, $revLength)"/>
 </xsl:function>

 <xsl:template match="/">
  <xsl:value-of select="my:subSequences('12345', 2)" separator=","/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is executed, the wanted, correct result is produced :执行此转换时,会产生所需的正确结果

12345,1234,123,12

Explanation :说明

The XPath 2.0 W3C Spec defines that if the first argument of the to operator is greater than the second argument, then the resulting sequence is the empty sequense. XPath 2.0 W3C 规范定义如果to运算符的第一个参数大于第二个参数,则结果序列是空序列。

It is still possible to avoid this limitation and to construct a decreasing integer sequence, like this:仍然可以避免这种限制并构造一个递减的整数序列,如下所示:

 for $k in 0  to $big - $small
  return  
     $big - $k

Using such expression is more efficient, especially for large sequences, than first constructing an increasing sequence and then reversing it with the reverse() function.使用这样的表达式比首先构造一个递增序列然后使用reverse()函数反转它更有效,特别是对于大序列。

on

<string>12345</string>

the following xslt will produce the result 12345,1234,123,12以下 xslt 将产生结果 12345,1234,123,12

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>

    <xsl:template match="string">
        <xsl:call-template name="doTheFunkeyMonkey">
            <xsl:with-param name="data" select="."/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="doTheFunkeyMonkey">
        <xsl:param name="data"/>
        <xsl:value-of select="$data"/>
        <xsl:if test="string-length($data) &gt; 2">
            <xsl:text>,</xsl:text>
            <xsl:call-template name="doTheFunkeyMonkey">
                <xsl:with-param name="data" select="substring($data,1,string-length($data)-1)"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM