この関数normalize-space
は、先頭と末尾の空白を削除し、一連の空白文字を単一のスペースに置き換えます。XSLT 1.0 で一連の空白文字のみを単一のスペースに置き換えるにはどうすればよいですか? たとえば、"..x.y...\n\t..z."
(読みやすくするためにスペースをドットに置き換えた)は".x.y.z."
.
2693 次
2 に答える
7
次の XPath 1.0 式を使用します。
concat(substring(' ', 1 + not(substring(.,1,1)=' ')),
normalize-space(),
substring(' ', 1 + not(substring(., string-length(.)) = ' '))
)
これを確認するには、次の変換を行います。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select=
"concat(substring(' ', 1 + not(substring(.,1,1)=' ')),
normalize-space(),
substring(' ', 1 + not(substring(., string-length(.)) = ' '))
)
"/>
</xsl:template>
</xsl:stylesheet>
この XML ドキュメントに適用すると:
<t>
<t1> xxx yyy zzz </t1>
<t2>xxx yyy zzz</t2>
<t3> xxx yyy zzz</t3>
<t4>xxx yyy zzz </t4>
</t>
必要な正しい結果が生成されます。
<t>
<t1> xxx yyy zzz </t1>
<t2>xxx yyy zzz</t2>
<t3> xxx yyy zzz</t3>
<t4>xxx yyy zzz </t4>
</t>
于 2011-02-18T05:28:27.767 に答える
2
ベッカーの方法がなければ、落胆したキャラクターをマークとして使用できます。
translate(normalize-space(concat('',.,'')),'','')
注:3つの関数呼び出し...
または、任意の文字を使用して式を繰り返す:
substring(
normalize-space(concat('.',.,'.')),
2,
string-length(normalize-space(concat('.',.,'.'))) - 2
)
XSLT では、変数を簡単に宣言できます。
<xsl:variable name="vNormalize" select="normalize-space(concat('.',.,'.'))"/>
<xsl:value-of select="susbtring($vNormalize,2,string-length($vNormalize)-2)"/>
于 2011-02-18T17:42:53.507 に答える