在 xml 上应用模板之前,是否可以在 xsl 参数上应用模板

发布于 2024-09-11 01:23:44 字数 110 浏览 13 评论 0原文

我有一个 xsl 参数,其中一个字符串。我想解析该字符串,拆分它,对于每个子字符串值,我想应用 xsl 中的模板。

这可能吗?如果是这样,您能建议一个乐观的解决方案吗?

谢谢

I have a xsl parameter which a string. I want to parse that string, split it and for each substring value I want to apply the template in the xsl.

Is this possible? If so, Can you please advise an optimistic solution?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜灵血窟げ 2024-09-18 01:23:44

编辑:误解了这个问题,抱歉。

答案是

输入:

<secuence>Item1 Item2 Item3</secuence>

样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="secuence/text()" name="secuence">
        <xsl:param name="string" select="."/>
        <xsl:param name="separator" select="' '"/>
        <xsl:if test="$string != ''">
            <xsl:choose>
                <xsl:when test="contains($string,$separator)">
                    <xsl:call-template name="secuence">
                        <xsl:with-param name="string" select="substring-before($string,$separator)"/>
                        <xsl:with-param name="separator" select="$separator"/>
                    </xsl:call-template>
                    <xsl:call-template name="secuence">
                        <xsl:with-param name="string" select="substring-after($string,$separator)"/>
                        <xsl:with-param name="separator" select="$separator"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <!-- Your desired template -->
                    <Item>
                        <xsl:value-of select="$string"/>
                    </Item>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

结果:

<secuence>
    <Item>Item1</Item>
    <Item>Item2</Item>
    <Item>Item3</Item>
</secuence>

Edit: Missundertood the question, sorry.

The answer is yes.

Input:

<secuence>Item1 Item2 Item3</secuence>

Stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="secuence/text()" name="secuence">
        <xsl:param name="string" select="."/>
        <xsl:param name="separator" select="' '"/>
        <xsl:if test="$string != ''">
            <xsl:choose>
                <xsl:when test="contains($string,$separator)">
                    <xsl:call-template name="secuence">
                        <xsl:with-param name="string" select="substring-before($string,$separator)"/>
                        <xsl:with-param name="separator" select="$separator"/>
                    </xsl:call-template>
                    <xsl:call-template name="secuence">
                        <xsl:with-param name="string" select="substring-after($string,$separator)"/>
                        <xsl:with-param name="separator" select="$separator"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <!-- Your desired template -->
                    <Item>
                        <xsl:value-of select="$string"/>
                    </Item>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Result:

<secuence>
    <Item>Item1</Item>
    <Item>Item2</Item>
    <Item>Item3</Item>
</secuence>
痴情换悲伤 2024-09-18 01:23:44

不确定您的意思,但复制此模式可能会有所帮助: XSLT - 将逗号分隔的文本拆分和呈现为 HTML 的最佳方式

Unsure what you mean but copying this pattern may help: XSLT - Best way to split and render comma separated text as HTML

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文