Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XSL:XSL:when在递归期间不工作_Xml_Xslt - Fatal编程技术网

Xml XSL:XSL:when在递归期间不工作

Xml XSL:XSL:when在递归期间不工作,xml,xslt,Xml,Xslt,我有一个模板,可以检测撇号或引号(“),并在文本中的“\”之前添加。 当文本中只包含撇号或引号时,它会起作用。但当文本中同时包含撇号和引号时, 第二个xsl:when不起作用 `<xsl:template name="replace-string"> <xsl:param name="text" /> <xsl:choose> <xsl:when test='contains($text,"&apos;") '&

我有一个模板,可以检测撇号或引号(“),并在文本中的“\”之前添加。 当文本中只包含撇号或引号时,它会起作用。但当文本中同时包含撇号和引号时, 第二个
xsl:when
不起作用

 `<xsl:template name="replace-string">
    <xsl:param name="text" />
    <xsl:choose>
        <xsl:when test='contains($text,"&apos;") '>
            <xsl:value-of select='substring-before($text,"&apos;")' />
            <xsl:text>&#92;&apos;</xsl:text>
            <xsl:call-template name="replace-string">
                <xsl:with-param name="text"
                    select='substring-after($text,"&apos;")' />
            </xsl:call-template>
        </xsl:when>
        <xsl:when test="contains($text,'&#34;')">
            <xsl:value-of select="substring-before($text,'&#34;')" />
            <xsl:text>&#92;&#34;</xsl:text>
            <xsl:call-template name="replace-string">
                <xsl:with-param name="text"
                    select="substring-after($text,'&#34;')" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>`
`
\;&apos;
\"
`
例如:
Some'text=Some'text;
一些“文本=一些\”文本;
一些“文本”另一个文本=一些“文本”另一个文本;

Some'text“other”text=Some'text“other”text。

在针对以下XSLT运行时,以下输入XML将解决您的问题:

<root>'Sometext'"anothertext'"</root>
“某个文本”和“另一个文本”
XSLT代码:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:variable name="Apos">'</xsl:variable>
<xsl:variable name="Quot">"</xsl:variable>

<xsl:template match="/">
    <xsl:call-template name="replace-string">
        <xsl:with-param name="text" select="*"/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:variable name="strBeforeApos" select='substring-before($text, $Apos)'/>
    <xsl:variable name="strBeforeQuot" select="substring-before($text, $Quot)"/>
    <xsl:variable name="char">
        <xsl:choose>
            <xsl:when test="contains($text, $Apos) and contains($text, $Quot)">
                <xsl:choose>
                    <xsl:when test="string-length($strBeforeApos) &lt; string-length($strBeforeQuot)">
                        <xsl:value-of select="$Apos"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$Quot"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="contains($text, $Apos)">
                <xsl:value-of select="$Apos"/>
            </xsl:when>
            <xsl:when test="contains($text, $Quot)">
                <xsl:value-of select="$Quot"/>
            </xsl:when>
        </xsl:choose>
    </xsl:variable>

    <xsl:choose>
        <xsl:when test="normalize-space($char) != ''">
            <xsl:value-of select='substring-before($text,$char)' />
            <xsl:text>&#92;</xsl:text>
            <xsl:value-of select="$char"/>
            <xsl:call-template name="replace-string">
                <xsl:with-param name="text"
                select='substring-after($text, $char)' />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" />
        </xsl:otherwise>
    </xsl:choose>

</xsl:template>
</xsl:transform>

'
"
\

一个简单的方法是参数化模板并调用它两次,例如:

<?xml version="1.0" encoding="UTF-8"?>
<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:template match="/">
    <xsl:variable name="escape-apos">
        <xsl:call-template name="escape-char">
            <xsl:with-param name="text" select="input"/>
            <xsl:with-param name="char">'</xsl:with-param>
        </xsl:call-template>
    </xsl:variable>

    <output>
        <xsl:call-template name="escape-char">
            <xsl:with-param name="text" select="$escape-apos"/>
            <xsl:with-param name="char">"</xsl:with-param>
        </xsl:call-template>
    </output>
</xsl:template>

<xsl:template name="escape-char">
    <xsl:param name="text"/>
    <xsl:param name="char"/>
    <xsl:choose>
        <xsl:when test="contains($text, $char)">
            <xsl:value-of select="substring-before($text, $char)"/>
            <xsl:value-of select="concat('\', $char)"/>
            <xsl:call-template name="escape-char">
                <xsl:with-param name="text" select="substring-after($text, $char)"/>
                <xsl:with-param name="char" select="$char"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

'
"

此行为的原因是Java、C等语言中的“开关”功能。如果为true,则执行第一个开关,如果为true,则退出,如果为true,则执行下一个或。如果同时使用撇号和引号,则字符串将根据“撇号”进行处理“因为你第一次检查撇号的存在,所以不会让下一次或第二次。尝试这个字符串,它会工作:一些'文本'另一个文本