使用Altova Stylevision的Autocalc功能中的Xpath表达式将冗长的行分成多行

时间:2014-09-08 09:35:33

标签: 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 use. No faith in anything of that cheap kind!

我需要将句子分成更小的句子并在Altova Stylevision中以多行显示。

我使用Autocalc动态显示行内容。是否有任何Xpath表达式,我可以根据每行中的单词数或字符数将行拆分为多行。所以我可以将行显示为:

    Whatever universe a professor believes in must at any rate
    be a universe that lends itself to lengthy discourse.

等等。

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试将字符串输入分成多个部分(字/字符串长度)。我已经为您创建了一个样本,以简化操作。请仔细检查是否可以帮到您:

输入xml

<?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>

<强> XSLT

<?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>