小编Aar*_*ron的帖子

从单个节点中的属性构建XML结构

我有以下XML源:

<element not-relevant="true" bold="true" superscript="true" subscript="false" text="stuff"/>
Run Code Online (Sandbox Code Playgroud)

在任何顺序中,我需要循环遍历特定属性(即只有与我正在构建的HTML相关的属性:bold/superscript/subscript等),并且其中一个属性评估为'true',输出嵌套元素获得以下输出:

<strong>
    <sup>
        stuff
    </sup>
</strong>
Run Code Online (Sandbox Code Playgroud)

我有一种感觉,我需要使用某种递归,如下所示(当然没有无限循环):

<xsl:template match="element">
    <xsl:call-template name="content">
        <xsl:with-param name="text" select="@text"/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="content">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="@bold = 'true'">
            <strong>
                <xsl:copy>
                    <xsl:call-template name="content">
                        <xsl:with-param name="text" select="$text"/>
                    </xsl:call-template>
                <xsl:copy>
            </strong>
        </xsl:when>
        <xsl:when test="@subscript = 'true'">
            <sub>
                <xsl:copy>
                    <xsl:call-template name="content">
                        <xsl:with-param name="text" select="$text"/>
                    </xsl:call-template>
                <xsl:copy>
            </sub>
        </xsl:when>
        <xsl:when test="@superscript = 'true'">
            <sup>
                <xsl:copy>
                    <xsl:call-template name="content">
                        <xsl:with-param name="text" select="$text"/>
                    </xsl:call-template>
                <xsl:copy>
            </sup>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" disable-output-escaping="yes"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template> …
Run Code Online (Sandbox Code Playgroud)

xml xslt xslt-2.0

5
推荐指数
1
解决办法
98
查看次数

XSLT 2.0:for-each循环上的子串之后获取不同值

我有以下输入XML:

<img src="blah.jpg"/>
<img src="blahh.jpg"/>
<img src="blah.gif"/>
Run Code Online (Sandbox Code Playgroud)

我需要一个不同的文件类型输出字符串,格式为:

jpg|gif
Run Code Online (Sandbox Code Playgroud)

我现有的样式表有一些方法:

<xsl:for-each select="distinct-values(descendant::img/@src)">
    <xsl:choose>
        <xsl:when test="position()!=last()">
            <xsl:value-of select="concat(substring-after(.,'.'),'|')"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="substring-after(.,'.')"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)

但是,我得到了一个重复的文件类型,因为我无法将for-for循环中的substring-after放入(它会引发错误,因为您无法一次对多个字符串执行substring-after)。这意味着我只能获取整个@src属性的不同值(而不是句点之后的字符串)。所以我的输出当前看起来像这样:

jpg|jpg|gif
Run Code Online (Sandbox Code Playgroud)

我非常感谢一个简单的XSLT 2.0解决方案,它使我能够做到这一点。

非常感谢您提前抽出宝贵时间-非常感谢。

xml xslt

2
推荐指数
1
解决办法
594
查看次数

标签 统计

xml ×2

xslt ×2

xslt-2.0 ×1