簡體   English   中英

XSLT 1.0:多個字符串替換的命名模板?

[英]XSLT 1.0: Named template for multiple string substitution?

單詞問題我需要在找到特殊字符串的其他字符串中插入一個字符串。 這些特殊字符串的定義在節點集中。 最接近的通用解決方案是進行多次替換,並將匹配的特殊字符串替換為與特殊字符串連接的插入字符串。 我已經找到了解決方案,但沒有找到任何可行的方法。

例。 輸入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來替換多個不同的字符串。

有任何想法嗎?

在XSLT 1.0中確實很難做到這一點。

問題不只是替換多個字符串。 這里還有一個額外的麻煩,因為replace-string包含search-string(因為您僅添加了前綴)。 這消除了使用單個模板重復遍歷整個文本的可能性,並規定了使用兩個單獨的模板:一個遞歸於多個搜索字符串,另一個遞歸於當前搜索字符串在文本中的出現:

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>

我認為EXSLT提供了str:replacehttp://exslt.org/str/functions/replace/str.replace.template.xsl )來進行多次替換,但是,我很難使它簡單地用一個簡單的替換匹配的單詞字符串,因此我已將其實現從使用copy-of替換為value-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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM