在Altova Stylevision的Autocalc功能中使用Xpath表达式将长行拆分为多行

在Altova Stylevision的Autocalc功能中使用Xpath表达式将长行拆分为多行,xpath,xslt-2.0,altova,Xpath,Xslt 2.0,Altova,我有一个XML行,它有一个很长的句子,例如: <ROW>4.)Whatever universe a professor believes in must at any rate be a universe that lends itself to lengthy discourse. A universe definable in two sentences is something for which the professorial intellect has no u

我有一个XML行,它有一个很长的句子,例如:

    <ROW>4.)Whatever universe a professor believes in must at any rate be a universe that lends itself to lengthy discourse. A universe definable in two sentences is something for which the professorial intellect has no use. No faith in anything of that cheap kind!

以此类推。

您似乎试图将字符串输入分成多个单词/字符串长度。我已经为您创建了一个示例,使其变得简单。请仔细阅读并检查它是否能帮助您:

输入xml

XSLT

输出:

    Whatever universe a professor believes in must at any rate
    be a universe that lends itself to lengthy discourse.
<?xml version="1.0" encoding="UTF-8"?>
<ROW>4.)Whatever universe a professor believes in must at any rate be a universe that lends itself to lengthy discourse. A universe definable in two sentences is something for which the professorial intellect has no use. No faith in anything of that cheap kind!11</ROW>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">

  <xsl:output indent="yes" method="xml"/>
  <xsl:template match="ROW">
    <output>
    <xsl:variable name="Length" select="string-length(.)"/>
    <xsl:variable name="Words" select="count(tokenize(.,'\s'))"/>
    <xsl:variable name="ByLength" select="50"/>
    <xsl:variable name="ByWord" select="5"/>
    <!-- To get ouput by words -->
      <xsl:if test="$ByWord">
        <xsl:variable name="Result">
            <xsl:call-template name="GetBreakingWords">
              <xsl:with-param name="Value" select="."/>
              <xsl:with-param name="ByWord" select="$ByWord"/>
              <xsl:with-param name="Words" select="$Words"/>
          </xsl:call-template>
        </xsl:variable>
        <p>By word example</p>
        <xsl:for-each select="$Result/row">
          <xsl:sort select="@id" data-type="number"/>
          <!--<row><xsl:value-of select="."/></row>-->
          <xsl:copy-of select="."/>
        </xsl:for-each>
      </xsl:if>
      <!-- To get ouput by string length -->
      <xsl:if test="$ByLength">
        <xsl:variable name="Result">
          <xsl:call-template name="GetBreakingStrings">
            <xsl:with-param name="Value" select="."/>
            <xsl:with-param name="ByLength" select="$ByLength"/>
            <xsl:with-param name="Length" select="$Length"/>
          </xsl:call-template>
        </xsl:variable>
        <p>By String example</p>
        <xsl:for-each select="$Result/row">
          <xsl:sort select="@id" data-type="number"/>
          <!--<row><xsl:value-of select="."/></row>-->
          <xsl:copy-of select="."/>
        </xsl:for-each>
      </xsl:if>

    </output>
  </xsl:template>

  <xsl:template name="GetBreakingWords">
    <xsl:param name="ByWord"/>
    <xsl:param name="Value"/>
    <xsl:param name="Words"/>
    <xsl:param name="counter" select="1"/>
    <xsl:choose>
      <xsl:when test="($Words gt $ByWord)">
      <xsl:variable name="CurrentRow" select="normalize-space(string-join(for $x in tokenize($Value, '\s')[position() ge 1 and position() le $ByWord] return $x, ' '))"/>
      <xsl:call-template name="GetBreakingWords">
        <xsl:with-param name="ByWord" select="$ByWord"/>
        <xsl:with-param name="Words" select="count(tokenize(substring-after($Value, $CurrentRow),'\s'))"/>
        <xsl:with-param name="Value" select="substring-after($Value, $CurrentRow)"/>
        <xsl:with-param name="counter" select="sum($counter + 1)"></xsl:with-param>
      </xsl:call-template>
        <row id="{$counter}"><xsl:value-of select="$CurrentRow"/></row>
      </xsl:when>
      <xsl:when test="not($Words gt $ByWord) and $Value">
        <row id="{$counter}"><xsl:value-of select="$Value"/></row>
      </xsl:when>
      </xsl:choose>
  </xsl:template>

  <xsl:template name="GetBreakingStrings">
    <xsl:param name="ByLength"/>
    <xsl:param name="Value"/>
    <xsl:param name="Length"/>
    <xsl:param name="counter" select="1"/>
    <xsl:choose>
      <xsl:when test="($Length gt $ByLength)">
        <xsl:variable name="CurrentRow" select="normalize-space(string-join(substring($Value,0,$ByLength),' '))"/>
        <xsl:call-template name="GetBreakingStrings">
          <xsl:with-param name="ByLength" select="$ByLength"/>
          <xsl:with-param name="Length" select="string-length(substring-after($Value, $CurrentRow))"/>
          <xsl:with-param name="Value" select="substring-after($Value, $CurrentRow)"/>
          <xsl:with-param name="counter" select="sum($counter + 1)"></xsl:with-param>
        </xsl:call-template>
        <row id="{$counter}"><xsl:value-of select="$CurrentRow"/></row>
      </xsl:when>
      <xsl:when test="not($Length gt $ByLength) and $Value">
        <row id="{$counter}"><xsl:value-of select="$Value"/></row>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<output>
   <p>By word example</p>
   <row id="1">4.)Whatever universe a professor believes</row>
   <row id="2">in must at any</row>
   <row id="3">rate be a universe</row>
   <row id="4">that lends itself to</row>
   <row id="5">lengthy discourse. A universe</row>
   <row id="6">definable in two sentences</row>
   <row id="7">is something for which</row>
   <row id="8">the professorial intellect has</row>
   <row id="9">no use. No faith</row>
   <row id="10">in anything of that</row>
   <row id="11"> cheap kind!11</row>
   <p>By String example</p>
   <row id="1">4.)Whatever universe a professor believes in must</row>
   <row id="2">at any rate be a universe that lends itself to l</row>
   <row id="3">engthy discourse. A universe definable in two sen</row>
   <row id="4">tences is something for which the professorial in</row>
   <row id="5">tellect has no use. No faith in anything of that</row>
   <row id="6"> cheap kind!11</row>
</output>