I. 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:template match="/*">
<screen>
<xsl:for-each select="tokenize(., '\r?\n')">
line: <xsl:sequence select="."/>
</xsl:for-each>
</screen>
</xsl:template>
</xsl:stylesheet>
この変換が提供されたXMLドキュメントに適用される場合:
<screen>
Volume in drive C is SYSTEM Serial number is 2350:717C
Directory of C:\
10/17/97 9:04 <DIR> bin
10/16/97 14:11 <DIR> DOS
10/16/97 14:40 <DIR> Program Files
10/16/97 14:46 <DIR> TEMP
10/17/97 9:04 <DIR> tmp
10/16/97 14:37 <DIR> WINNT
10/16/97 14:25 119 AUTOEXEC.BAT
2/13/94 6:21 54,619 COMMAND.COM
10/16/97 14:25 115 CONFIG.SYS
11/16/97 17:17 61,865,984 pagefile.sys
2/13/94 6:21 9,349 WINA20.386
</screen>
必要な正しい結果(テキストノードの各行の前に文字列が追加されます"line: "
)が生成されます:
<screen>
line:
line: Volume in drive C is SYSTEM Serial number is 2350:717C
line: Directory of C:\
line:
line: 10/17/97 9:04 <DIR> bin
line: 10/16/97 14:11 <DIR> DOS
line: 10/16/97 14:40 <DIR> Program Files
line: 10/16/97 14:46 <DIR> TEMP
line: 10/17/97 9:04 <DIR> tmp
line: 10/16/97 14:37 <DIR> WINNT
line: 10/16/97 14:25 119 AUTOEXEC.BAT
line: 2/13/94 6:21 54,619 COMMAND.COM
line: 10/16/97 14:25 115 CONFIG.SYS
line: 11/16/97 17:17 61,865,984 pagefile.sys
line: 2/13/94 6:21 9,349 WINA20.386
line: </screen>
説明:
tokenize()
オプションのCRをNL文字の前に置くことができるRegExの2番目の引数を持つ関数の適切な使用。
II。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:strip-space elements="*"/>
<xsl:template match="text()" name="lines">
<xsl:param name="pText" select="."/>
<xsl:if test="string-length($pText)">
line: <xsl:text/>
<xsl:value-of select=
"substring-before(concat($pText, '
'), '
')"/>
<xsl:call-template name="lines">
<xsl:with-param name="pText" select=
"substring-after($pText, '
')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
このXSLT1.0変換が同じXMLドキュメント(上記)に適用されると、必要な結果(テキストノードの各行に文字列が付加されます"line: "
)が生成されます。
line:
line: Volume in drive C is SYSTEM Serial number is 2350:717C
line: Directory of C:\
line:
line: 10/17/97 9:04 <DIR> bin
line: 10/16/97 14:11 <DIR> DOS
line: 10/16/97 14:40 <DIR> Program Files
line: 10/16/97 14:46 <DIR> TEMP
line: 10/17/97 9:04 <DIR> tmp
line: 10/16/97 14:37 <DIR> WINNT
line: 10/16/97 14:25 119 AUTOEXEC.BAT
line: 2/13/94 6:21 54,619 COMMAND.COM
line: 10/16/97 14:25 115 CONFIG.SYS
line: 11/16/97 17:17 61,865,984 pagefile.sys
line: 2/13/94 6:21 9,349 WINA20.386
説明:
次の各行を抽出して出力するための再帰的な名前付きテンプレート。停止条件-文字列の長さがゼロの場合。
の適切な使用substring-before()
、substring-after()
およびコードの長さと複雑さを最小限に抑えるためのセンチネル手法。