Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSLT-分隔符计数5(例如5“|”)后的字符串换行_Xslt_Xslt 1.0 - Fatal编程技术网

XSLT-分隔符计数5(例如5“|”)后的字符串换行

XSLT-分隔符计数5(例如5“|”)后的字符串换行,xslt,xslt-1.0,Xslt,Xslt 1.0,我是XSLT的新手。在出现一定数量的分隔符后,我需要包装一个长字符串。 此类字符串的示例如下:- 杰森|迈克尔|约翰|詹姆斯|里克|保罗|珍妮|雷|伊丽莎|希帕|阿披舍克|帕特里克|布伦特|凯文|吉姆 由于一些限制,我不想为此使用模板 然而,如果这是不可能的-我可以与模板 输出应如下所示: 第1行:Jason | Michael | John | James | Rick |第2行:Paul | JenYee | Ray | Eliza | Shilpa |第3行:Abhishek | Patr

我是XSLT的新手。在出现一定数量的分隔符后,我需要包装一个长字符串。 此类字符串的示例如下:-

杰森|迈克尔|约翰|詹姆斯|里克|保罗|珍妮|雷|伊丽莎|希帕|阿披舍克|帕特里克|布伦特|凯文|吉姆

由于一些限制,我不想为此使用模板

然而,如果这是不可能的-我可以与模板

输出应如下所示:
第1行:Jason | Michael | John | James | Rick |第2行:Paul | JenYee | Ray | Eliza | Shilpa |第3行:Abhishek | Patrick | Brent | Kevin | Jim

如果可以使用XSLT2.0,可以使用标记化

示例$input是问题中的字符串:

<xsl:value-of select="tokenize($input,'\|')[5 >= position()]" separator="|"/>

这将产生:Jason | Michael | John | James | Rick

使用以下递归模板:

<xsl:template name="beforeSeparators">
  <xsl:param name="start"/>
  <xsl:param name="rest"/>
  <xsl:param name="separator"/>
  <xsl:param name="count"/>
  <xsl:choose>
    <xsl:when test="$count &lt;= 0">
      <xsl:value-of select="$start"/>
    </xsl:when>
    <xsl:when test="contains($rest,$separator)">
      <xsl:call-template name="beforeSeparators">
        <xsl:with-param name="start" select="concat($start,substring-before($rest,$separator),$separator)"/>
        <xsl:with-param name="rest" select="substring-after($rest,$separator)"/>
        <xsl:with-param name="separator" select="$separator"/>
        <xsl:with-param name="count" select="$count - 1"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="concat($start,$rest)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="wrap">
  <xsl:param name="str"/>
  <xsl:param name="separator"/>
  <xsl:param name="separatorsPerLine"/>
  <xsl:variable name="line">
    <xsl:call-template name="beforeSeparators">
      <xsl:with-param name="start" select="''"/>
      <xsl:with-param name="rest" select="$str"/>
      <xsl:with-param name="separator" select="$separator"/>
      <xsl:with-param name="count" select="$separatorsPerLine"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:value-of select="concat($line,'&#x0d;&#x0a;')"/>
  <xsl:if test="string-length($line) &lt; string-length($str)">
    <xsl:call-template name="wrap">
      <xsl:with-param name="str" select="substring($str,string-length($line))"/>
      <xsl:with-param name="separator" select="$separator"/>
      <xsl:with-param name="separatorsPerLine" select="$separatorsPerLine"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>
以下是我的完整测试XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:template name="beforeSeparators">
    <xsl:param name="start"/>
    <xsl:param name="rest"/>
    <xsl:param name="separator"/>
    <xsl:param name="count"/>
    <xsl:choose>
      <xsl:when test="$count &lt;= 0">
        <xsl:value-of select="$start"/>
      </xsl:when>
      <xsl:when test="contains($rest,$separator)">
        <xsl:call-template name="beforeSeparators">
          <xsl:with-param name="start" select="concat($start,substring-before($rest,$separator),$separator)"/>
          <xsl:with-param name="rest" select="substring-after($rest,$separator)"/>
          <xsl:with-param name="separator" select="$separator"/>
          <xsl:with-param name="count" select="$count - 1"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="concat($start,$rest)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="wrap">
    <xsl:param name="str"/>
    <xsl:param name="separator"/>
    <xsl:param name="separatorsPerLine"/>
    <xsl:variable name="line">
      <xsl:call-template name="beforeSeparators">
        <xsl:with-param name="start" select="''"/>
        <xsl:with-param name="rest" select="$str"/>
        <xsl:with-param name="separator" select="$separator"/>
        <xsl:with-param name="count" select="$separatorsPerLine"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="concat($line,'&#x0d;&#x0a;')"/>
    <xsl:if test="string-length($line) &lt; string-length($str)">
      <xsl:call-template name="wrap">
        <xsl:with-param name="str" select="substring($str,string-length($line)+1)"/>
        <xsl:with-param name="separator" select="$separator"/>
        <xsl:with-param name="separatorsPerLine" select="$separatorsPerLine"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template match="/">
    <xsl:call-template name="wrap">
      <xsl:with-param name="str" select="'Jason|Michael|John|James|Rick|Paul|JenYee|Ray|Eliza|Shilpa|Abhishek|Patrick|Brent|Kevin|'"/>
      <xsl:with-param name="separator" select="'|'"/>
      <xsl:with-param name="separatorsPerLine" select="5"/>
    </xsl:call-template>
  </xsl:template>

</xsl:stylesheet>

它只是转换一个固定的字符串,所以它可以应用于任何XML

我有只使用XSLT1.0的限制。这就是问题所在。不清楚你说的收尾是什么意思。是否要截断5个分隔符后的字符串?您在上面给出了一个示例输入字符串;请显示该字符串的输出。第1行:Jason | Michael | John | James | Rick |第2行:Paul | JenYee | Ray | Eliza | Shilpa |第3行:Abhishek | Patrick | Brent | Kevin | JimOK,您想在5个定界符后包装该字符串。换句话说,您希望输出字符串的所有部分,不仅仅是前5行,而是分成多行。看来到目前为止回答的两个人都误解了你的意思。建议:编辑您的问题,这样您就可以以易于阅读的方式显示所需的输出。有没有办法不使用模板来实现这一点?@apratik:没有-您需要递归,因此,您需要一个可以自己调用的模板。我根据您的评论修复了我的答案。您是否检查了输出?我使用了相同的代码,得到的值为空:-是的,我确实检查了输出-它与我的答案一样。我发布了完整的测试XSLT。
Jason|Michael|John|James|Rick|
Paul|JenYee|Ray|Eliza|Shilpa|
Abhishek|Patrick|Brent|Kevin|
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:template name="beforeSeparators">
    <xsl:param name="start"/>
    <xsl:param name="rest"/>
    <xsl:param name="separator"/>
    <xsl:param name="count"/>
    <xsl:choose>
      <xsl:when test="$count &lt;= 0">
        <xsl:value-of select="$start"/>
      </xsl:when>
      <xsl:when test="contains($rest,$separator)">
        <xsl:call-template name="beforeSeparators">
          <xsl:with-param name="start" select="concat($start,substring-before($rest,$separator),$separator)"/>
          <xsl:with-param name="rest" select="substring-after($rest,$separator)"/>
          <xsl:with-param name="separator" select="$separator"/>
          <xsl:with-param name="count" select="$count - 1"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="concat($start,$rest)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="wrap">
    <xsl:param name="str"/>
    <xsl:param name="separator"/>
    <xsl:param name="separatorsPerLine"/>
    <xsl:variable name="line">
      <xsl:call-template name="beforeSeparators">
        <xsl:with-param name="start" select="''"/>
        <xsl:with-param name="rest" select="$str"/>
        <xsl:with-param name="separator" select="$separator"/>
        <xsl:with-param name="count" select="$separatorsPerLine"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="concat($line,'&#x0d;&#x0a;')"/>
    <xsl:if test="string-length($line) &lt; string-length($str)">
      <xsl:call-template name="wrap">
        <xsl:with-param name="str" select="substring($str,string-length($line)+1)"/>
        <xsl:with-param name="separator" select="$separator"/>
        <xsl:with-param name="separatorsPerLine" select="$separatorsPerLine"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template match="/">
    <xsl:call-template name="wrap">
      <xsl:with-param name="str" select="'Jason|Michael|John|James|Rick|Paul|JenYee|Ray|Eliza|Shilpa|Abhishek|Patrick|Brent|Kevin|'"/>
      <xsl:with-param name="separator" select="'|'"/>
      <xsl:with-param name="separatorsPerLine" select="5"/>
    </xsl:call-template>
  </xsl:template>

</xsl:stylesheet>