Converting XML to escaped text in XSLT

前端 未结 8 1666
挽巷
挽巷 2020-12-03 02:54

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

Source:



  

        
相关标签:
8条回答
  • 2020-12-03 03:43

    instead of escaping you can add the text inside a CDATA section. Text inside a CDATA section will be ignored by the parser, similar to if it was escaped.

    your example would look like this

    <TestElement>
    <![CDATA[
    <abc>
      <def ghi="jkl">
        mnop
      </def>
    </abc>
    ]]>
    </TestElement>
    

    using following XSLT snippet:

    <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
     <xsl:copy-of select="/"/>
     <xsl:text disable-output-escaping="yes">]]</xsl:text>
     <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
    
    0 讨论(0)
  • 2020-12-03 03:45

    Do you need to use XSLT? Because, for reasons explained by Pavel Minaev, it would be much simpler to use another tool. An example with xmlstartlet:

    % xmlstarlet escape
    <?xml version="1.0" encoding="utf-8"?>
    <abc>
      <def ghi="jkl">
        mnop
      </def>
    </abc>
    [Control-D]
    &lt;?xml version="1.0" encoding="utf-8"?&gt;
    &lt;abc&gt;
      &lt;def ghi="jkl"&gt;
        mnop
      &lt;/def&gt;
    &lt;/abc&gt;
    
    0 讨论(0)
提交回复
热议问题