如何用xslt中不同标签中的值替换花括号内的变量

How to replace variable inside curly braces with values in different tags in xslt

我正在使用 xslt 处理 xml 文件。

<ns1:declarationStatements>
    <ns1:parameterisedEntity>
        <ns2:code>NUTSUPSTATE20</ns2:code>
        <ns2:localeData>
            <ns1:description>
                <![CDATA[** When {s} according to instructions {m}g typically weighs {m}g.]]>
            </ns1:description>
            <ns1:id>20253</ns1:id>
        </ns2:localeData>
        <ns2:specType>FOOD</ns2:specType>
        <ns2:id>6653</ns2:id>
    </ns1:parameterisedEntity>
    <ns1:textParameters>
        <ns1:value>228</ns1:value>
        <ns1:id>68225</ns1:id>
        <ns1:sequence>2</ns1:sequence>
    </ns1:textParameters>
    <ns1:textParameters>
        <ns1:value>cooked</ns1:value>
        <ns1:id>68233</ns1:id>
        <ns1:sequence>0</ns1:sequence>
    </ns1:textParameters>
    <ns1:textParameters>
        <ns1:value>255</ns1:value>
        <ns1:id>68229</ns1:id>
        <ns1:sequence>1</ns1:sequence>
    </ns1:textParameters>
    <ns1:id>133421</ns1:id>
</ns1:declarationStatements>

我想获取 <ns1:description> 中的文本,即-

**When {s} according to instructions {m}g typically weighs {m}g

但我希望将 {s}、{m} 和 {m} 替换为 <ns1:textParameters>/<ns1:value> 中的值。它应该看起来像 -

**When cooked according to instructions 255g typically weighs 228g.

我尝试使用 <xsl:value-of select="ns0:declarationStatements"> 和操纵字符串来做到这一点,但它变得非常乏味和复杂。

此类牙套的数量也可能有所不同。那么我们在 XSLT 中有类似 List 或 Array 的东西吗?

有没有其他方法或技巧可以用来解决这个问题?

谢谢

假设参数要按其 ns1:sequence 值的顺序插入,我将首先定义一个 key 为:

<xsl:key name="text-param" match="ns1:textParameters" use="ns1:sequence" />

然后使用 ns1:description 作为 string 参数调用以下递归模板:

<xsl:template name="merge-params">
    <xsl:param name="string"/>
    <xsl:param name="i" select="0"/>
    <xsl:choose>
        <xsl:when test="contains($string, '{') and contains(substring-after($string, '{'), '}')">
            <xsl:value-of select="substring-before($string, '{')" />
            <xsl:value-of select="key('text-param', $i)/ns1:value" />
            <!-- recursive call -->
            <xsl:call-template name="merge-params">
                <xsl:with-param name="string" select="substring-after($string, '}')" />
                <xsl:with-param name="i" select="$i + 1" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$string" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>