2011-10-21 15 views
5

XSLT 2.0を使用してXMLからデータを抽出しています。データには長い行があり、自動的に行を破ることでウィンドウサイズに合わせたいと思っています。XSLTのウィンドウに合わせてテキストを折り返す方法

XSLTでも可能ですか?

+1

対象のフォーマット(xsl:出力方法)は何ですか?それがHTMLまたはXHTMLの場合、ブラウザはコンテンツがオーバーフローしないようにして、何もする必要はないと思います。もちろん、XSLT自体はウィンドウやウィンドウのサイズを知らず、XMLをテキストや(X)HTMLやその他のXMLに変換します。 –

+0

@MartinHonnen私のXML出力はテキストであり、XHTMLではありません。それをテキスト形式で出力できますか? – smandape

答えて

6

標準のXSLT 2.0関数unparsed-text()を使用して、XSLT 2.0コードで直接テキストファイルを読み取ることができます。

そしてだけを使用:

replace(concat(normalize-space($text),' '), 
       '(.{0,60}) ', 
       '$1
') 

説明

この最初の空白を正規化、空白文字だけの先頭と末尾の配列を削除し、と任意インナーような配列を置換します単一のスペース。

正規化の結果は、標準XPath 2.0関数 replace()の最初の引数として使用されます。

一致パターンは、スペースで終わる最大61文字の任意の(最長の可能な配列である。

置換引数が有する連結、見つかったそのようなシーケンスが終了する前にスペース文字列で置換されることを指定。

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. 

XSLTコード012:NL文字

ここでは、ファイルC:\temp\delete\text.txtから、このテキストを読み、書式設定、完全なソリューションです:場合

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. 

更新

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

<xsl:variable name="vText" select= 
"unparsed-text('file:///c:/temp/delete/text.txt')"/> 

<xsl:template match="/"> 
    <xsl:sequence select= 
    "replace(concat(normalize-space($vText),' '), 
      '(.{0,60}) ', 
      '$1&#xA;') 
    "/> 
</xsl:template> 
</xsl:stylesheet> 

結果は、その各々が60の固定された長さを超えない、ラインのセットでありますテキストはXMLファイルから取り出されますが、これは上記の解決方法に最小限の変更を加えて行うことができます:

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

<xsl:template match="/"> 
    <xsl:sequence select= 
    "replace(concat(normalize-space(text),' '), 
      '(.{0,60}) ', 
      '$1&#xA;') 
    "/> 
</xsl:template> 
</xsl:stylesheet> 

ここで私はすべてのテキストは、XML文書の最上位要素(text命名)の唯一のテキストノードの子であることとします、この変換は、上記の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> 

第1の解決策と同じ結果が生成される。

+1

+1です。 OPはXMLファイルからデータを抽出していたことに注意してください。 – LarsH

+0

@LarsH:ああ、ありがとう、私は何とか反対の印象を持っていました - 彼はプレーンテキストファイルを扱いたいと思っていました。 XMLファイルのバリアントをソリューションに追加します。 –

+0

ワラ!これは本当に役立ちます。君たちありがとう。 – smandape

2

tokenize()または<xsl:analyze-string>は、最大70文字まで許可し、改行文字(スペースなど)で終わる正規表現を使用してこれを効率的に行うことができます。

明示的なコードについては、xquery word wrapのXPathおよびXSLTの回答を参照してください。

+2

+1良い要約の答えです。役に立った答えは –

関連する問題