Is it possible to have HTML text or CDATA inside an XML attribute?

后端 未结 6 1552
情话喂你
情话喂你 2020-11-29 11:04

I keep getting \"XML parser failure: Unterminated attribute\" with my parser when I attempt to put HTML text or CDATA inside my XML attribute. Is there a way to do this or

6条回答
  •  失恋的感觉
    2020-11-29 11:25

    CDATA is unfortunately an ambiguous thing to say here. There are "CDATA Sections", and "CDATA Attribute Type".

    Your attribute value can be of type CDATA with the "CDATA Attribute Type".

    Here is an xml that contains a "CDATA Section" (aka. CDSect):

    
    
    
    
    

    Here is an xml that contains a "CDATA Attribute Type" (as AttType):

    
    
    ]>
    
    
    
    
    

    You cannot use a "CDATA Section" for an Attribute Value: wrong:/>

    You can use a "CDATA Attribute Type" for your Attribute's Type, I think this is actually what happens in the usual case, and your attribute value is actually a CDATA: for an element like , in the raw binary bytestring that is the .xml file, you have guy threep however when the file is processed, the attribute value in memory will be

    guy
    threep
    

    Your problem may lie in 1) producing a right xml file and 2) configuring a "xml processor" to produce an output you want.

    For example, in case you write a raw binary file as your xml by hand, you need to put these escapes inside the attribute value part in the raw file, like I wrote here, instead of

    Then the parse would actually give you a newline, I've tried this with a processor.

    You can try it with a processor like saxon or for poor-man's experiment one like a browser, opening the xml in firefox and copying the value to a text editor - firefox displayed the newline as a space, but copying the string to a text editor showed the newline. (Probably with a better suited processor you could save the direct output right away.)

    Now the "only" thing you need to do is make sure you handle this CDATA appropriately. For example, if you have an XSL stylesheet, that would produce you a html, you can use something like this .xsl for such an xml:

    
    
    
    
      
      
      
        
        
    
        
        

    Which in a browser or with a processor like saxon using java -jar saxon9he.jar -s:eg2.xml -xsl:eg2.xsl -o:eg2.html saxon home edition 9.5 would produce this html-like thing:

    guy
    threep

    which will look like this in a browser:

    guy
    threep
    

    Here I am using a recursive template 'split' from Tomalak, thanks to Mads Hansen, because my target processor doesn't support neither string-join nor tokenize which are version 2.0 only.

提交回复
热议问题