【发布时间】:2011-04-06 22:12:08
【问题描述】:
这个问题是this one 的扩展。我有很多价值观,如下所示:
<myStuff>
<elem1>asdf</elem1>
<elem2>foo bar</elem2>
<elem3>foo bar foo</elem3>
<elem4>foofoo</elem4>
</myStuff>
我一直在对大多数元素进行copy-of(copy-of 中的select 非常具体),然后对生成的 XML 进行查找和替换,但我想将两者结合起来。在我应用此功能的大多数情况下,我会将<xsl:value-of select="whatever"> 中的任何内容替换为<xsl:apply-templates select="whatever"> 并使用以下模板:
<xsl:template match="*">
<xsl:variable name="rep1">
<xsl:choose>
<xsl:when test='matches(., ".*foo.*")'>
<xsl:value-of select='replace(., "foo", "qwerty")' />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='./text()' />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="rep2">
<xsl:choose>
<xsl:when test='matches(., ".*bar.*")'>
<xsl:value-of select='replace($rep1, "bar", "myBar")' />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='$rep1' />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$rep2" />
</xsl:template>
我想使用类似的模板来替换我的代码中的copy-of(因为我在我的其他文件中进行相同的替换,所以我可以使用相同的模板),但我不确定如何做到这一点。
输出如下所示:
<myOtherStuff>
<elem1>asdf</elem1>
<elem2>qwerty myBar</elem2>
<elem3>qwerty myBar qwerty</elem3>
<elem4>qwertyqwerty</elem4>
</myOtherStuff>
感谢所有帮助,并提前致谢!
【问题讨论】:
-
好问题 (+1)。请参阅我的答案以获得完整的解决方案。