如何在 XSLT 的帮助下对 Escaped-XML 内容进行转义? [英] How to unescape Escaped-XML content with the help of XSLT?

查看:30
本文介绍了如何在 XSLT 的帮助下对 Escaped-XML 内容进行转义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何取消转义已经转义的 xml.

My question is How to un-escape xml that has already been escaped.

我尝试了 Tomalak 提供的代码以响应 How在 XSLT 的帮助下对 XML 字符进行转义?,但我无法做到我想要的.

I tried the code provided by Tomalak in response to How to unescape XML characters with help of XSLT?, but I can't get that to do what I want.

我有 SoapMsg Xml.正文包含一些元素,其中之一是字符串.这个字符串包含转义的 XML.这通常在 RPC SoapMsg 中完成,因为它们不允许复杂的类型.为了解决这个问题,他们将 Escaped-Xml 嵌入到字符串元素中,请参阅下面输入中的 sXmlParameters.

I have SoapMsg Xml. The body contains a few elements one of which is a String. This string contains Escaped XML. This is often done in RPC SoapMsg because they don't allow complex types. To Get around this they embed Escaped-Xml inside a String Element, see sXmlParameters in the input below.

示例输入:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:pan="http://wsdl.somebody.com/Stuff/">
  <soap:Header />
  <soap:Body>
    <pan:SomeCommand>
      <first>eefbb52a0fee443cbda838caffbc2654</first>
      <second>f26eb2f5dabc457ca045e64585f7b185</second>
      <sXmlParameters>&lt;PARAMETERS&gt;&lt;TIMEOUTDATETIME&gt;2011-03-15
        2:09:48.997&lt;/TIMEOUTDATETIME&gt;&lt;/PARAMETERS&gt;</sXmlParameters>
    </pan:SomeCommand>
  </soap:Body>
</soap:Envelope>

我也看到这个数据用 <![CDATA[>]]> 转义,我也需要取消转义.

I also see this data escaped with <![CDATA[>]]>, I need to un-escape it also.

转换后的输出:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:pan="http://wsdl.somebody.com/Stuff/">
  <soap:Header />
  <soap:Body>
    <pan:SomeCommand>
      <first>eefbb52a0fee443cbda838caffbc2654</first>
      <second>f26eb2f5dabc457ca045e64585f7b185</second>
      <sXmlParameters>
        <PARAMETERS>
           <TIMEOUTDATETIME>2011-03-15 2:09:48.997</TIMEOUTDATETIME>
        </PARAMETERS>
      </sXmlParameters>
    </pan:SomeCommand>
  </soap:Body>
</soap:Envelope>

推荐答案

这已经解决了你一半的问题——而不是 CDATA 部分:

This will already take care of half of your problem – not the CDATA part:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="//sXmlParameters">
        <xsl:copy>
            <xsl:call-template name="unescape">
                <xsl:with-param name="escaped" select="string(.)"/>
            </xsl:call-template>
        </xsl:copy>
    </xsl:template>

    <xsl:template name="unescape">
        <xsl:param name="escaped"/>
        <xsl:choose>
            <xsl:when test="contains($escaped,'&lt;')">
                <xsl:variable name="beforeelem" select="substring-before($escaped,'&lt;')"/>
                <xsl:variable name="elemname1" select="substring-before(substring-after($escaped,'&lt;'),' ')"/>
                <xsl:variable name="elemname2" select="substring-before(substring-after($escaped,'&lt;'),'&gt;')"/>
                <xsl:variable name="elemname3" select="substring-before(substring-after($escaped,'&lt;'),'/&gt;')"/>
                <xsl:variable name="hasattributes" select="string-length($elemname1) &gt; 0 and ((string-length($elemname2)=0 or string-length($elemname1) &lt; string-length($elemname2)) and (string-length($elemname3)=0 or string-length($elemname1) &lt; string-length($elemname3)))"/>
                <xsl:variable name="elemclosed" select="string-length($elemname3) &gt; 0 and (string-length($elemname2)=0 or string-length($elemname3) &lt; string-length($elemname2))"/>
                <xsl:variable name="elemname">
                    <xsl:choose>
                        <xsl:when test="$hasattributes">
                            <xsl:value-of select="$elemname1"/>
                        </xsl:when>
                        <xsl:when test="not($elemclosed)">
                            <xsl:value-of select="$elemname2"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$elemname3"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:variable name="elemclosetag" select="concat('&lt;/',$elemname,'&gt;')"/>
                <xsl:variable name="innercontent">
                    <xsl:if test="not($elemclosed)">
                        <xsl:call-template name="skipper-before">
                            <xsl:with-param name="source" select="substring-after(substring-after($escaped,'&lt;'),'&gt;')"/>
                            <xsl:with-param name="delimiter" select="$elemclosetag"/>
                        </xsl:call-template>
                    </xsl:if>
                </xsl:variable>
                <xsl:variable name="afterelem">
                    <xsl:choose>
                        <xsl:when test="not($elemclosed)">
                            <xsl:call-template name="skipper-after">
                                <xsl:with-param name="source" select="substring-after(substring-after($escaped,'&lt;'),'&gt;')"/>
                                <xsl:with-param name="delimiter" select="$elemclosetag"/>
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="substring-after(substring-after($escaped,'&lt;'),'/&gt;')"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:element name="{$elemname}">
                    <xsl:if test="$hasattributes">
                        <xsl:call-template name="unescapeattributes">
                            <xsl:with-param name="escapedattributes">
                                <xsl:choose>
                                    <xsl:when test="not($elemclosed)">
                                        <xsl:value-of select="normalize-space(substring-after($elemname2,' '))"/>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:value-of select="normalize-space(substring-after($elemname3,' '))"/>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:with-param>
                        </xsl:call-template>
                    </xsl:if>
                    <xsl:call-template name="unescape">
                        <xsl:with-param name="escaped" select="$innercontent"/>
                    </xsl:call-template>
                </xsl:element>
                <xsl:call-template name="unescape">
                    <xsl:with-param name="escaped" select="$afterelem"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="unescapetext">
                    <xsl:with-param name="escapedtext" select="$escaped"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="unescapeattributes">
        <xsl:param name="escapedattributes"/>
        <xsl:variable name="attrname" select="substring-before($escapedattributes,'=')"/>
        <xsl:variable name="attrquote" select="substring($escapedattributes,string-length($attrname)+2,1)"/>
        <xsl:variable name="attrvalue" select="substring-before(substring-after($escapedattributes,$attrquote),$attrquote)"/>
        <xsl:variable name="afterattr" select="substring-after(substring-after($escapedattributes,$attrquote),$attrquote)"/>
        <xsl:attribute name="{$attrname}">
            <xsl:call-template name="unescapetext">
                <xsl:with-param name="escapedtext" select="$attrvalue"/>
            </xsl:call-template>
        </xsl:attribute>
        <xsl:if test="contains($afterattr,'=')">
            <xsl:call-template name="unescapeattributes">
                <xsl:with-param name="escapedattributes" select="normalize-space($afterattr)"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template name="unescapetext">
        <xsl:param name="escapedtext"/>
        <xsl:call-template name="string-replace-all">
            <xsl:with-param name="text">
                <xsl:call-template name="string-replace-all">
                    <xsl:with-param name="text">
                        <xsl:call-template name="string-replace-all">
                            <xsl:with-param name="text" select="$escapedtext"/>
                            <xsl:with-param name="replace">&amp;gt;</xsl:with-param>
                            <xsl:with-param name="by">&gt;</xsl:with-param>
                        </xsl:call-template>
                    </xsl:with-param>
                    <xsl:with-param name="replace">&amp;lt;</xsl:with-param>
                    <xsl:with-param name="by">&lt;</xsl:with-param>
                </xsl:call-template>
            </xsl:with-param>
            <xsl:with-param name="replace">&amp;amp;</xsl:with-param>
            <xsl:with-param name="by">&amp;</xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <!-- replaces substrings in strings -->
    <xsl:template name="string-replace-all">
        <xsl:param name="text"/>
        <xsl:param name="replace"/>
        <xsl:param name="by"/>
        <xsl:choose>
            <xsl:when test="contains($text, $replace)">
                <xsl:value-of select="substring-before($text,$replace)"/>
                <xsl:value-of select="$by"/>
                <xsl:call-template name="string-replace-all">
                    <xsl:with-param name="text" select="substring-after($text,$replace)"/>
                    <xsl:with-param name="replace" select="$replace"/>
                    <xsl:with-param name="by" select="$by"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- returns the substring after the last delimiter -->
    <xsl:template name="skipper-after">
        <xsl:param name="source"/>
        <xsl:param name="delimiter"/>
        <xsl:choose>
            <xsl:when test="contains($source,$delimiter)">
                <xsl:call-template name="skipper-after">
                    <xsl:with-param name="source" select="substring-after($source,$delimiter)"/>
                    <xsl:with-param name="delimiter" select="$delimiter"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$source"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- returns the substring before the last delimiter -->
    <xsl:template name="skipper-before">
        <xsl:param name="source"/>
        <xsl:param name="delimiter"/>
        <xsl:param name="result"/>
        <xsl:choose>
            <xsl:when test="contains($source,$delimiter)">
                <xsl:call-template name="skipper-before">
                    <xsl:with-param name="source" select="substring-after($source,$delimiter)"/>
                    <xsl:with-param name="delimiter" select="$delimiter"/>
                    <xsl:with-param name="result">
                        <xsl:if test="result!=''">
                            <xsl:value-of select="concat($result,$delimiter)"/>
                        </xsl:if>
                        <xsl:value-of select="substring-before($source,$delimiter)"/>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$result"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

这篇关于如何在 XSLT 的帮助下对 Escaped-XML 内容进行转义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆