How do I Emit Escaped XML representation of a Node in my XSLT's HTML Output

后端 未结 2 505
粉色の甜心
粉色の甜心 2020-12-20 00:53

I\'m transforming XML to an HTML document. In this document I want to embed XML markup for a node that was just transformed (the HTML document is a technical spec).

2条回答
  •  自闭症患者
    2020-12-20 01:27

    The answer to this is more complex that you would at first think. Proper "double-escaping" of attribute values and text nodes must occur.

    This XSLT 1.0 template does a correct (though not complete) printing of an XML node, including (an attempt to do) proper pretty-printing with configurable indentation:

    
      
    
       
      
      
    
      
      
    
      
        
          
            
            

    When applied to this test XML:

    
      
        
          
            
    Various bits & pieces
    text and <escaped-text />

    The following output is produced (source code):

    
    
    
    <node id="1">
        <!-- a comment -->
        <stuff type="fl&quot;'&#xD;&#x9;oatsam">
            <details>Various bits &amp; pieces</details>
            <details />
            <details attr="value">
                <childnode>text and &lt;escaped-text /&lt;</childnode>
            </details>
        </stuff>
    </node>
    

    and when viewed in the browser you see:

    
        
        
            
    Various bits & pieces
    text and <escaped-text /<

    Note that by "not complete" I mean that things like namespaces and processing instructions for example are currently unhandled. But its easy to make a template for them.

提交回复
热议问题