XSLT 1.0:迭代字符串中的字符 [英] XSLT 1.0 : Iterate over characters in a string
本文介绍了XSLT 1.0:迭代字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要遍历字符串中的字符以构建 XML 结构.
I need to iterate over the characters in a string to build an XML structure.
目前,我正在这样做:
<xsl:template name="verticalize">
<xsl:param name="text">Some text</xsl:param>
<xsl:for-each select="tokenize(replace(replace($text,'(.)','$1\\n'),'\\n$',''),'\\n')">
<xsl:element name="para">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:template>
这会产生类似的东西:
<para>S</para>
<para>o</para>
<para>m</para>
<para>e</para>
<para> </para>
<para>t</para>
<para>e</para>
<para>x</para>
<para>t</para>
这适用于 Xpath 2.0.但是我需要在 XPath 1.0 环境中应用相同的处理,其中 replace()
方法不可用.
This works fine with Xpath 2.0. But I need to apply the same treatment in a XPath 1.0 environment, where the replace()
method is not available.
您知道实现这一目标的方法吗?
Do you know a way to achieve this ?
推荐答案
<xsl:template name="letters">
<xsl:param name="text" select="'Some text'" />
<xsl:if test="$text != ''">
<xsl:variable name="letter" select="substring($text, 1, 1)" />
<para><xsl:value-of select="$letter" /></para>
<xsl:call-template name="letters">
<xsl:with-param name="text" select="substring-after($text, $letter)" />
</xsl:call-template>
</xsl:if>
</xsl:template>
这篇关于XSLT 1.0:迭代字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文