Converting XML to escaped text in XSLT

2019-01-11 06:44发布

How can I convert the following XML to an escaped text using XSLT?

Source:

<?xml version="1.0" encoding="utf-8"?>
<abc>
  <def ghi="jkl">
    mnop
  </def>
</abc>

Output:

<TestElement>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;abc&gt;&lt;def ghi="jkl"&gt;
    mnop
  &lt;/def&gt;&lt;/abc&gt;</TestElement>

Currently, I'm trying the following XSLT and it doesn't seem to work properly:

<?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" encoding="utf-8" />
  <xsl:template match="/">
    <xsl:variable name="testVar">
      <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
    </xsl:variable>

    <TestElement>
      <xsl:value-of select="$testVar"/>
    </TestElement>
  </xsl:template>
</xsl:stylesheet>

Output of XSLT statement by the .NET XslCompiledTransform comes out as the following:

<?xml version="1.0" encoding="utf-8"?><TestElement>

    mnop

</TestElement>

标签: xml xslt
8条回答
闹够了就滚
2楼-- · 2019-01-11 07:15

I attempted to implement the answer provided by Pavel Minaev and want to point out that this is very dangerous for large strings as each character in the input string is recursed over individually, causing the recursion depth to quickly run out. I attempted to run it over a few lines of text and it caused a stack overflow (lol).

Instead, I use a template that does not need to examine each individual char, rather it will out put the text until it finds a string that needs to be replaced. This can then be used to escape characters:

<xsl:template name="Search-And-Replace">
    <xsl:param name="Input-String"/>
    <xsl:param name="Search-String"/>
    <xsl:param name="Replace-String"/>  
    <xsl:choose>
        <xsl:when test="$Search-String and contains($Input-String, $Search-String)">
            <xsl:value-of select="substring-before($Input-String, $Search-String)"/>
            <xsl:value-of select="$Replace-String"/>        
            <xsl:call-template name="Search-And-Replace">
                <xsl:with-param name="Input-String" select="substring-after($Input-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="$Input-String"/>
        </xsl:otherwise>
    </xsl:choose>   
</xsl:template> 

Then its just a matter of calling that template for the char that you want to escape..

<xsl:call-template name="Search-And-Replace">
            <xsl:with-param name="Input-String" select="Hi I am a string &amp; I am awesome"/>
            <xsl:with-param name="Search-String" select="'&amp;'"/>
            <xsl:with-param name="Replace-String" select="'&amp;amp;'"/>
    </xsl:call-template>

In order to escape multiple characters in the one string, I used a wrapper template that uses variables...

<xsl:template name="EscapeText">
    <xsl:param name="text" />

    <xsl:variable name="a">
    <xsl:call-template name="Search-And-Replace">
            <xsl:with-param name="Input-String" select="$text"/>
            <xsl:with-param name="Search-String" select="'&amp;'"/>
            <xsl:with-param name="Replace-String" select="'&amp;amp;'"/>
        </xsl:call-template>            
    </xsl:variable>

    <xsl:variable name="b">     
        <xsl:call-template name="Search-And-Replace">
            <xsl:with-param name="Input-String" select="$a"/>
            <xsl:with-param name="Search-String" select="'&quot;'"/>
            <xsl:with-param name="Replace-String" select="'&amp;quot;'"/>
        </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="c">     
        <xsl:call-template name="Search-And-Replace">
            <xsl:with-param name="Input-String" select="$b"/>
            <xsl:with-param name="Search-String">&apos;</xsl:with-param>
            <xsl:with-param name="Replace-String" select="'&amp;apos;'"/>
        </xsl:call-template>
    </xsl:variable>         

    <xsl:variable name="d">     
        <xsl:call-template name="Search-And-Replace">
            <xsl:with-param name="Input-String" select="$c"/>
            <xsl:with-param name="Search-String" select="'&gt;'"/>
            <xsl:with-param name="Replace-String" select="'&amp;gt;'"/>
        </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="e">
        <xsl:call-template name="Search-And-Replace">
            <xsl:with-param name="Input-String" select="$d"/>
            <xsl:with-param name="Search-String" select="'&lt;'"/>
            <xsl:with-param name="Replace-String" select="'&amp;lt;'"/>
        </xsl:call-template>
    </xsl:variable>     
    <!--this is the final output-->
    <xsl:value-of select="$e"/>     
</xsl:template> 

This proved to be much safer for large strings as it no longer has to recurse for each individual character in the input string.

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-11 07:20

You can prevent the extra namespace nodes by adding a test in the namespace output:


<xsl:variable name="curnode" select="."/>
    <xsl:for-each select="namespace::*"> 
       <xsl:variable name="nsuri" select="."/>
       <xsl:if test="$curnode/descendant-or-self::*[namespace-uri()=$nsuri]">
       ...
查看更多
登录 后发表回答