XSLT 2.0を使用してXMLからデータを抽出しています。データには長い行があり、自動的に行を破ることでウィンドウサイズに合わせたいと思っています。XSLTのウィンドウに合わせてテキストを折り返す方法
XSLTでも可能ですか?
XSLT 2.0を使用してXMLからデータを抽出しています。データには長い行があり、自動的に行を破ることでウィンドウサイズに合わせたいと思っています。XSLTのウィンドウに合わせてテキストを折り返す方法
XSLTでも可能ですか?
標準の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
')
"/>
</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
')
"/>
</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の解決策と同じ結果が生成される。
tokenize()
または<xsl:analyze-string>
は、最大70文字まで許可し、改行文字(スペースなど)で終わる正規表現を使用してこれを効率的に行うことができます。
明示的なコードについては、xquery word wrapのXPathおよびXSLTの回答を参照してください。
+1良い要約の答えです。役に立った答えは –
対象のフォーマット(xsl:出力方法)は何ですか?それがHTMLまたはXHTMLの場合、ブラウザはコンテンツがオーバーフローしないようにして、何もする必要はないと思います。もちろん、XSLT自体はウィンドウやウィンドウのサイズを知らず、XMLをテキストや(X)HTMLやその他のXMLに変換します。 –
@MartinHonnen私のXML出力はテキストであり、XHTMLではありません。それをテキスト形式で出力できますか? – smandape