0

テキスト ノードを持つ XML があり、XSLT 2.0 を使用してこの文字列を複数のチャンクに分割する必要があります。例えば:

<tag>
    <text>This is a long string 1This is a long string 2This is a long string 3This is a long string 4</text>
</tag>

出力は次のようになります。

<tag>
    <text>This is a long string 1</text>
    <text>This is a long string 2</text>
    <text>This is a long string 3</text>
    <text>This is a long string 4</text>
</tag>

チャンク サイズを意図的に各ステートメントの長さに設定して、例を読み書きしやすくしましたが、変換は任意の値を受け入れる必要があることに注意してください (この値をハードコーディングしてもかまいません)。

4

1 に答える 1

1

この XSLT 1.0 変換:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pChunkSize" select="23"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

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

  <xsl:if test="string-length($pText) >0">
   <text><xsl:value-of select=
   "substring($pText, 1, $pChunkSize)"/>
   </text>
   <xsl:call-template name="chunk">
    <xsl:with-param name="pText"
    select="substring($pText, $pChunkSize+1)"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

提供された XML ドキュメントに適用した場合:

<tag>
    <text>This is a long string 1This is a long string 2This is a long string 3This is a long string 4</text>
</tag>

必要な正しい結果が生成されます

<tag>
    <text>
        <text>This is a long string 1</text>
        <text>This is a long string 2</text>
        <text>This is a long string 3</text>
        <text>This is a long string 4</text>
        <text/>
    </text>
</tag>

Ⅱ.XSLT 2.0 ソリューション (非再帰的):

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pChunkSize" select="23"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="text/text()">
  <xsl:variable name="vtheText" select="."/>
  <xsl:for-each select=
      "0 to string-length() idiv $pChunkSize">
   <text>
    <xsl:sequence select=
     "substring($vtheText, . *$pChunkSize +1, $pChunkSize) "/>
   </text>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
于 2011-10-14T14:36:25.270 に答える