首页 文章

xslt 1.0使用嵌入式html转义撇号文本

提问于
浏览
0

我试图逃避出现在文本中的撇号,该撇号还包含嵌入的html标签 . 我的XML看起来像:

<segment code="ASDF"> ...
<text> <a href="asdf.aspx?lfn=GYPT_0&amp;u=0" >[ blah] </a> 08:57:11 02 OCT 2013
<b>blah</b>
blah ...

About <a href="EEEERE.aspx?lfn=MAB_0&amp;u=0" ><b>breathing</b></a>: blah you "can't do" an ...[ more of the same ] </text> </segment>

我可以将所有这些内容作为单个字符串(包括所有html标记及其内容)提取:

<xsl:template match="text" >
    , text:'<xsl:copy-of select='node()' />'
</xsl:template>

注意<xsl:copy-of />周围的撇号 - 本练习的结果是JSON,当然,由于 can't 中的撇号,这个特定的JSON会失败 .

Thanx试图了解!!我想要完成的最终结果如下:

text:'<a href="asdf.aspx?lfn=GYPT_0&amp;u=0" >[ blah] </a> 08:57:11 02 OCT 2013
<b>blah</b>
blah ...

About <a href="EEEERE.aspx?lfn=MAB_0&amp;u=0" ><b>breathing</b></a>: blah you "can&apos;t do" an ...[ more of the same ]'

(我正在从xml(其中包含html标记作为字符串)转换为JSON对象(包含带有html标记的字符串),并且由于该值是一个字符串,因此它用单引号括起来,因此任何嵌入的单个-quotes需要以浏览器正确显示的形式转义为javascript . )

迈克尔 - 我已经尝试过你的代码 - 复制并粘贴到我的样式表中 . 产品总是一无所有 - 一个空字符串 . 我也试过像这样使用translate()函数:

translate(node(),"'","&amp;apos;"), which also produces an empty string.

我在样式表中使用迈克尔解决方案所做的具体更改是:

<xsl:with-param name="string" select="."/>
to
<xsl:with-param name="string" select="node()"/>

的 . 不起作用,因为翻译人员将嵌入的html标签视为附加节点,因此不会返回其内容 . node()语句(指定了<output = html)修复了这个问题,但是我发现在哪个node()实际产生非空字符串的唯一情况是它在<xsl:copy-of语句中 . 当它在<xsl:value-of语句中时,它返回一个空字符串 . 我不能写,例如:

<xsl:with-param name="string" select="<xsl:copy-of select="node()"/>" />

我已经尝试过translate()各种xsl:call w / params,但在所有情况下,它们都会失败,因为除非使用<xsl:copy-of select =“node()”/>运算符,否则似乎不会产生任何内容,这不能用作translate()之类的函数中的参数,也不能用作select =“”语句的源 .

我怎么能这样做? thanx r

1 回答

  • 0

    (edited in response to your clarifications)

    您尝试将 ' 转换为 &amp;apos; 是朝着正确方向迈出的一步 . 但是,您不能使用translate()函数 . translate函数用另一个单个字符替换每个单个字符 . 在你的例子中:

    translate($string, "'", "&amp;apos;")
    

    输入$ string中的任何撇号字符都将被转换为(转义的)&符号(替换字符串中的第一个字符) .

    您需要在此处使用的工具是递归命名模板,它将使用表示转义撇号的字符串(多个字符)替换输入字符串中每个出现的撇号 .

    并且,因为最后您希望输出包含 &apos; 而不是 &amp;apos; ,所以必须将结果写入输出并禁用转义 .

    以下样式表:

    XSLT 1.0

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8"/>
    <xsl:strip-space elements="*"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="text">
        <xsl:text>text:'</xsl:text>
        <xsl:apply-templates/>
        <xsl:text>'</xsl:text>
    </xsl:template>
    
    <xsl:template match="text/text()">
        <xsl:call-template name="replace">
            <xsl:with-param name="string" select="."/>
            <xsl:with-param name="search-string">'</xsl:with-param>
            <xsl:with-param name="replace-string">&amp;apos;</xsl:with-param>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="replace">
        <xsl:param name="string"/>
        <xsl:param name="search-string"/>
        <xsl:param name="replace-string"/>
        <xsl:choose>
            <xsl:when test="contains($string, $search-string)">
                <xsl:value-of select="substring-before($string, $search-string)"/>
                <xsl:value-of select="$replace-string" disable-output-escaping="yes"/>
                <xsl:call-template name="replace">
                    <xsl:with-param name="string" select="substring-after($string, $search-string)"/>
                    <xsl:with-param name="search-string" select="$search-string"/>
                    <xsl:with-param name="replace-string" select="$replace-string"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$string"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    </xsl:stylesheet>
    

    当应用于您的示例输入时:

    XML

    <segment code="ASDF"> ...
    <text> <a href="asdf.aspx?lfn=GYPT_0&amp;u=0" >[ blah] </a> 08:57:11 02 OCT 2013
    <b>blah</b>
    blah ...

    About <a href="EEEERE.aspx?lfn=MAB_0&amp;u=0" ><b>breathing</b></a>: blah you "can't do" an ...[ more of the same ] </text> </segment>

    会产生这个 result

    <segment code="ASDF"> ...
    text:'<a href="asdf.aspx?lfn=GYPT_0&amp;u=0">[ blah] </a> 08:57:11 02 OCT 2013
    <b>blah</b>
    blah ...

    About <a href="EEEERE.aspx?lfn=MAB_0&amp;u=0"><b>breathing</b></a>: blah you "can&apos;t do" an ...[ more of the same ] '</segment>

    如果您不想要 segment 包装器元素,请在样式表中再添加一个模板:

    <xsl:template match="segment">
        <xsl:apply-templates select="text"/>
    </xsl:template>
    

    得到这个 result

    text:'<a href="asdf.aspx?lfn=GYPT_0&amp;u=0">[ blah] </a> 08:57:11 02 OCT 2013
    <b>blah</b>
    blah ...

    About <a href="EEEERE.aspx?lfn=MAB_0&amp;u=0"><b>breathing</b></a>: blah you "can&apos;t do" an ...[ more of the same ] '

    我相信这等于“我想要完成的最终结果”,给予或占据一个空间 .

    演示:http://xsltransform.net/eiZQaFq

相关问题