XML to WordML using XSLT 1.0 - replace html tags within xml content with wordML formatting tags

前端 未结 4 609
[愿得一人]
[愿得一人] 2021-01-16 05:21

I am creating a WordML document from an xml file whose elements sometimes contain html-formatted text.


  
     html forma         


        
4条回答
  •  终归单人心
    2021-01-16 05:40

    I can most probably help you if only I understood your problem... Is the html in a CDATA section or is it parsed as part of the input doc (and thus well-formed XML)? Since you talk about 'text replacement' I'll assume that you treat the 'html formatted content' as a single string (CDATA) and therefor need a recursive call-template function to perform string replacement. The only way you're going to be able to use an XSL matching template to do what you're doing now is to make the html part of the parsed document (your input document). In such a case you could just match the b tag and replace it with the appropriate output (again: this assumes that it can always be parsed as valid XML). Your problem now has shifted... since (if I understood your problem correctly) what you're trying to do is close the w:t and w:r elements and then 'reopen' them... this is hard because it's (as you probably suspect) very hard to do this nicely in XSLT (you cannot just create an element in template A and then close it in template B). You'll have to start messing with unescaped output etc. to make this happen. I now I've made a lot of assumptions but here is a small example to help you on your way:

    input.xml

    
    
      
        beforeboldafter
      
    
    
    

    convert_html.xsl

    
      
        
      
    
    
    
      
      
      
    
    

    Now running

    xalan input.xml convert_html.xsl
    

    produces

    
    
      
        beforeboldafter
      
    
    
    

    which I guess is what you wanted.

    Hope this helps you somewhat.

提交回复
热议问题