【问题标题】:XSLT 1.0: Named template for multiple string substitution?XSLT 1.0:用于多个字符串替换的命名模板?
【发布时间】: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 来替换多个不同的字符串。

有什么想法吗?

【问题讨论】:

  • 您的输入有例如&lt;words&gt;&lt;word&gt;word&lt;/word&gt; 当您的 XSLT 选择例如/root/words/word/@name 所以这对我来说没有意义。然后想要的结果有&lt;word name="word"/&gt;,你要转换那些元素吗?请澄清。
  • 啊,对不起。复制粘贴错误。现已修复。

标签: xml xslt xslt-1.0


【解决方案1】:

这在 XSLT 1.0 中确实很难做到。

问题不仅仅是替换多个字符串。这里还有一个额外的复杂性,因为替换字符串包含搜索字符串(因为您只添加了一个前缀)。这消除了使用单个模板重复遍历整个文本的可能性,并规定使用两个单独的模板:一个递归遍历多个搜索字符串,另一个遍历当前搜索字符串在文本中的出现:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="string">
    <xsl:copy>
        <xsl:call-template name="multi-prefix">
            <xsl:with-param name="text" select="."/>
        </xsl:call-template>
    </xsl:copy> 
</xsl:template>

<xsl:template name="multi-prefix">
    <xsl:param name="text"/>
    <xsl:param name="search-strings" select="/root/words/word/@name"/>
    <xsl:choose>
        <xsl:when test="$search-strings">
            <xsl:call-template name="multi-prefix">
                <xsl:with-param name="text">
                    <xsl:call-template name="single-prefix">
                        <xsl:with-param name="text" select="$text"/>
                        <xsl:with-param name="search-string" select="$search-strings[1]"/>
                    </xsl:call-template>
                </xsl:with-param>
                <xsl:with-param name="search-strings" select="$search-strings[position() > 1]"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="single-prefix">
    <xsl:param name="text"/>
    <xsl:param name="prefix" select="/root/@name"/>
    <xsl:param name="search-string"/>
    <xsl:choose>
        <xsl:when test="contains($text, $search-string)">
            <xsl:value-of select="substring-before($text, $search-string)"/>
            <xsl:value-of select="$prefix"/>
            <xsl:value-of select="$search-string"/>
            <xsl:call-template name="single-prefix">
                <xsl:with-param name="text" select="substring-after($text, $search-string)"/>
                <xsl:with-param name="search-string" select="$search-string"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

【讨论】:

    【解决方案2】:

    我认为EXSLT 为多个替换提供了str:replace (http://exslt.org/str/functions/replace/str.replace.template.xsl),但是,我很难让它简单地用一个简单的字符串替换匹配的单词,所以我已经覆盖了它的实现,从使用copy-ofvalue-of:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:exsl="http://exslt.org/common"
        xmlns:str="http://exslt.org/strings"
        exclude-result-prefixes="exsl str"
        version="1.0">
    
        <xsl:import href="http://exslt.org/str/functions/replace/str.replace.template.xsl"/>
    
        <xsl:template name="str:_replace">
            <xsl:param name="string" select="''"/>
            <xsl:param name="replacements" select="/.."/>
            <xsl:choose>
                <xsl:when test="not($string)"/>
                <xsl:when test="not($replacements)">
                    <xsl:value-of select="$string"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:variable name="replacement" select="$replacements[1]"/>
                    <xsl:variable name="search" select="$replacement/@search"/>
                    <xsl:choose>
                        <xsl:when test="not(string($search))">
                            <xsl:value-of select="substring($string, 1, 1)"/>
                            <xsl:value-of select="$replacement/node()"/>
                            <xsl:call-template name="str:_replace">
                                <xsl:with-param name="string" select="substring($string, 2)"/>
                                <xsl:with-param name="replacements" select="$replacements"/>
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="contains($string, $search)">
                            <xsl:call-template name="str:_replace">
                                <xsl:with-param name="string" select="substring-before($string, $search)"/>
                                <xsl:with-param name="replacements" select="$replacements[position() > 1]"/>
                            </xsl:call-template>
                            <xsl:value-of select="$replacement/node()"/>
                            <xsl:call-template name="str:_replace">
                                <xsl:with-param name="string" select="substring-after($string, $search)"/>
                                <xsl:with-param name="replacements" select="$replacements"/>
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="str:_replace">
                                <xsl:with-param name="string" select="$string"/>
                                <xsl:with-param name="replacements" select="$replacements[position() > 1]"/>
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="string">
            <xsl:param name="insert" select="ancestor::root/@name"/>
            <xsl:param name="search" select="ancestor::root/words/word"/>
            <xsl:param name="replace">
                <xsl:for-each select="$search">
                    <replace>
                        <xsl:value-of select="concat($insert, .)"/>
                    </replace>          
                </xsl:for-each>
            </xsl:param>
            <xsl:copy>
                <xsl:call-template name="str:replace">
                    <xsl:with-param name="string" select="."/>
                    <xsl:with-param name="search" select="$search"/>
                    <xsl:with-param name="replace" select="exsl:node-set($replace)/replace"/>
                </xsl:call-template>
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 2021-10-10
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2011-03-05
      相关资源
      最近更新 更多