【发布时间】:2017-06-01 19:47:57
【问题描述】:
文字问题 我需要在找到特殊字符串的其他字符串中插入一个字符串。这些特殊字符串的定义在一个节点集中。最接近的通用解决方案是进行多次替换,并将匹配的特殊字符串替换为与特殊字符串连接的插入字符串。我已经搜索了一个解决方案,但没有找到任何可行的解决方案。
示例。 输入 XML:
<root name ="theTop.">
<string>Here are some silly words</string>
<string>where I would like</string>
<other>
<string>to append other silly words</string>
</other>
<words>
<word name="word"/>
<word name="silly"/>
</words>
</root>
输出 XML:
<root name ="theTop.">
<string>Here are some theTop.silly theTop.words</string>
<string>where I would like</string>
<other>
<string>to append other theTop.silly theTop.words</string>
</other>
<words>
<word name="word"/>
<word name="silly"/>
</words>
</root>
我还没有想出任何有用的东西。这是我想象骨架的样子的“草图”:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:variable name="append" select="/root/@name"/>
<xsl:variable name="places" select="/root/words/word/@name"/>
<!-- Identity rule.-->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//string">
<xsl:call-template name="replace-multiple">
<xsl:with-param name="text" select="."/>
<xsl:with-param name="replace" select="$places"/>
<xsl:with-param name="with" select="concat($append,$places)"/>
</xsl:call-template>
</xsl:template>
显然这不起作用。它不会迭代节点集 $places 并且我没有命名模板 replace-multiple 来替换多个不同的字符串。
有什么想法吗?
【问题讨论】:
-
您的输入有例如
<words><word>word</word>当您的 XSLT 选择例如/root/words/word/@name所以这对我来说没有意义。然后想要的结果有<word name="word"/>,你要转换那些元素吗?请澄清。 -
啊,对不起。复制粘贴错误。现已修复。