XSLT 1.0用包含标签的HTML元素替换标签字符

时间:2019-05-28 15:43:09

标签: xslt-1.0

我一直不愿意使用HTML元素替换XSLT字符,但是我一生都无法弄清楚如何让XSLT用包含选项卡的span元素替换选项卡。

我在XHTML文档中拥有一个前置元素,其中包含制表符,我发现了一个XSLT字符串替换模板函数,该函数用来尝试用跨度包围的制表符替换制表符:

    <xsl:template match="xhtml:pre">
        <xsl:element name="{local-name()}">

            <xsl:variable name="BodyText"><span class="tabspan">&#x9;</span></xsl:variable>

            <xsl:call-template name="replace">
                <xsl:with-param name="text" select="@*|node()"/>
                <xsl:with-param name="search" select="'&#x9;'"/>
                <xsl:with-param name="replace" select="$BodyText"/>
            </xsl:call-template>

        </xsl:element>
    </xsl:template>

    <!-- Replacement template function for XSLT 1.0 -->
    <xsl:template name="replace">
        <xsl:param name="text"/>
        <xsl:param name="search"/>
        <xsl:param name="replace"/>
        <xsl:choose>
            <xsl:when test="contains($text, $search)">
                <xsl:variable name="replace-next">
                    <xsl:call-template name="replace">
                        <xsl:with-param name="text" select="substring-after($text, $search)"/>
                        <xsl:with-param name="search" select="$search"/>
                        <xsl:with-param name="replace" select="$replace"/>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:value-of select="concat(substring-before($text, $search),$replace, $replace-next)"/>
            </xsl:when>
            <xsl:otherwise>
                <span class="tabspan">
                    <xsl:value-of select="$text"/>
                </span>
            </xsl:otherwise>
        </xsl:choose>

但是,无论我如何更改代码,span元素要么在输出中丢失(如果我尝试通过上述xsl:variable进行分配),要么它无法正确转换代表尖括号

&gt;
&lt;

异常转换

&quot;

到引号。我很困惑,对XSLT 1.0也不了解。由于转换系统的限制,不能选择XSLT 2.0、3.0和EXSLT。

1 个答案:

答案 0 :(得分:0)

请考虑以下最小示例:

输入

var cnt1=0,cnt2=0,cnt3=0;
$(".one").change(function(){
    cnt1=1;
    if((cnt1==1) && (cnt2==1) && (cnt3==1)){
        alert('test');
    }
});
$(".two").change(function(){
    cnt2=1;
    if((cnt1==1) && (cnt2==1) && (cnt3==1)){
        alert('test');
    }
});
$(".three").change(function(){
    cnt3=1;
    if((cnt1==1) && (cnt2==1) && (cnt3==1)){
        alert('test');
    }
});

XSLT 1.0

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <select name="company1" class="dropdown one">
        <option value="1">Group 1</option>
        <option value="2">Group 2</option>
    </select>
    <select name="company2" class="dropdown two">
        <option value="1">Group 1</option>
        <option value="2">Group 2</option>
    </select>
    <select name="company3" class="dropdown three">
        <option value="1">Group 1</option>
        <option value="2">Group 2</option>
    </select>

结果

<html xmlns="http://www.w3.org/1999/xhtml">
    <head/>
    <body>
    <p>This is an example.</p>
    <pre>Text before    text in the middle  text after</pre>
    </body>
</html>

要使其更接近您的尝试,

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml">
<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="xhtml:pre">
    <xsl:copy>
        <xsl:call-template name="replace-tabs">
            <xsl:with-param name="text" select="."/>
        </xsl:call-template>
    </xsl:copy>
</xsl:template> 

<xsl:template name="replace-tabs">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '&#9;')">
            <xsl:value-of select="substring-before($text, '&#9;')"/>
            <span class="tabspan">
                <xsl:text>&#9;</xsl:text>
            </span>
            <xsl:call-template name="replace-tabs">
                <xsl:with-param name="text" select="substring-after($text, '&#9;')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>