2

このコードを使用して、150文字ごとにXML文字列を中断しています。

<xsl:variable name="val_cr_ln" select=" '&#xA;' " />

この変数が発生するたびにカウントを行う必要があります。

誰かが私にそれをどのように行うか教えてもらえますか?

ありがとう

4

2 に答える 2

0

XML ドキュメント内で特定の文字列が出現する回数を調べるには、次のようにします。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exslstr="http://exslt.org/strings"
>

    <xsl:template match="/">
        <xsl:variable name='xml_as_str'>
            <xsl:apply-templates select='*' />
        </xsl:variable>
        <p>There are <xsl:value-of select='count(exslstr:split($xml_as_str, "string")) - 1' /> occurrences of "string" in there source XML.</p>
    </xsl:template>

</xsl:stylesheet>

ここで実行できます: http://www.xmlplayground.com/7QAvNp

于 2012-06-08T18:58:51.607 に答える
0

XPath ワンライナーは次のとおりです。

string-length(/) - string-length(translate(string(/), $vNL, ''))

これは、XML ドキュメント内のすべてのテキスト ノードの連結における NL 文字の総数に評価されます

説明:

translate(string(/), $vNL, '')ドキュメント ノード (XML ドキュメント内のすべてのテキスト ノードの連結) の文字列値から、NL 文字が空の文字列に置き換えられた (削除された) 別の文字列を生成します。

次に、これを文字列の長さの合計から差し引くと、(削除された) NL 文字の正確な数が得られます

完全なコード例を次に示します。

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

 <xsl:variable name="vNL" select="'&#x0A;'"/>

 <xsl:template match="/">
  <xsl:value-of select=
  "string-length(/)
  - string-length(translate(string(/), $vNL, ''))
  "/>
 </xsl:template>
</xsl:stylesheet>

この変換が次の XML ドキュメントに適用された場合:

<text>
Dec. 13 — As always for a presidential inaugural, security and surveillance were
extremely tight in Washington, DC, last January. But as George W. Bush prepared to
take the oath of office, security planners installed an extra layer of protection: a
prototype software system to detect a biological attack. The U.S. Department of
Defense, together with regional health and emergency-planning agencies, distributed
a special patient-query sheet to military clinics, civilian hospitals and even aid
stations along the parade route and at the inaugural balls. Software quickly
analyzed complaints of seven key symptoms — from rashes to sore throats — for
patterns that might indicate the early stages of a bio-attack. There was a brief
scare: the system noticed a surge in flulike symptoms at military clinics.
Thankfully, tests confirmed it was just that — the flu.
</text>

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

12
于 2012-06-09T02:19:12.577 に答える