我有一个模板,用于在给定的分隔符处分割字符串:
<xsl:template name="SplitStringAtSeparator">
<xsl:param name="stringToSplit"/>
<xsl:param name="separatorCharacter" />
<xsl:choose>
<xsl:when test="contains($stringToSplit, $separatorCharacter)">
<xsl:value-of select="substring-before($stringToSplit, $separatorCharacter)"/>
<xsl:call-template name="SplitStringAtSeparator">
<xsl:with-param name="stringToSplit"
select="substring-after($stringToSplit, $separatorCharacter)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:if test="$stringToSplit != ''">
<xsl:value-of select="$stringToSplit"/>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
假设stringToSplit
='你好,这是一个要分割的字符串'并且我们要分割空格字符,我是否要找回一个只保留空格字符的文本节点
hellothisisastringtosplit
还是我将获得一个可以迭代的文本节点的节点集?
node[1] = hello
node[2] = this
node[3] = is
node[4] = a
node[5] = string
node[6] = to
node[7] = split
我应该注意,如果不是后者,那么后者就是我要尝试做的事情。
假设您的输入文件是...。
<root>
<stringToSplit>hello this is a string to split</stringToSplit>
</root>
...,并且您想拆分字符串的内容stringToSplit
并处理粒子。可以说该处理只是输出粒子的编号列表。这是您如何做的两个例子。一种解决方案是在XSLT 1.0中,另一种是在XSLT 2中。
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:so="http://stackoverflow.com/questions/32363330"
xmlns:exsl="http://exslt.org/common"
version="1.0"
exclude-result-prefixes="so exsl">
<xsl:output method="html" version="5" doctype-system="" encoding="utf-8" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<html>
<head>
<title>Hello! This is a string to split.</title>
</head>
<body>
<h1>Hello! This is a string to split.</h1>
<p>The following is an example of splitting a string in XSLT 1.0 and then processing each string part.</p>
<h2>String parts</h2>
<ol>
<xsl:apply-templates select="root/stringToSplit" />
</ol>
</body>
</html>
</xsl:template>
<xsl:template name="SplitStringAtSeparator">
<xsl:param name="stringToSplit" />
<xsl:param name="separatorCharacter" />
<xsl:choose>
<xsl:when test="contains($stringToSplit, $separatorCharacter)">
<so:particle><xsl:value-of select="substring-before($stringToSplit, $separatorCharacter)"/></so:particle>
<xsl:call-template name="SplitStringAtSeparator">
<xsl:with-param name="stringToSplit" select="substring-after($stringToSplit, $separatorCharacter)" />
<xsl:with-param name="separatorCharacter" select="$separatorCharacter" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$stringToSplit">
<so:particle><xsl:value-of select="$stringToSplit"/></so:particle>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="stringToSplit">
<xsl:variable name="split-string">
<xsl:call-template name="SplitStringAtSeparator">
<xsl:with-param name="stringToSplit" select="text()" />
<xsl:with-param name="separatorCharacter" select="' '" />
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set( $split-string)" mode="particle-processing" />
</xsl:template>
<xsl:template match="so:particle" mode="particle-processing">
<li><xsl:value-of select="." /></li>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="html" version="5" doctype-system="" encoding="utf-8" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hello! This is a string to split.</title>
</head>
<body>
<h1>Hello! This is a string to split.</h1>
<p>The following is an example of splitting a string in XSLT 2 and then processing each string part.</p>
<h2>String parts</h2>
<ol>
<xsl:analyze-string select="root/stringToSplit/text()" regex="\S*">
<xsl:matching-substring>
<li><xsl:value-of select="."/></li>
</xsl:matching-substring>
</xsl:analyze-string>
</ol>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
在这两种情况下,您都会得到输出文档,例如...
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Hello! This is a string to split.</title>
</head>
<body>
<h1>Hello! This is a string to split.</h1>
<p>The following is an example of splitting a string in XSLT 1.0 and then processing each string part.</p>
<h2>String parts</h2>
<ol>
<li>hello</li>
<li>this</li>
<li>is</li>
<li>a</li>
<li>string</li>
<li>to</li>
<li>split</li>
</ol>
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句