Replace special characters like – and — occuring in an xml document with corresponding code like – etc

后端 未结 2 542
借酒劲吻你
借酒劲吻你 2020-12-21 12:08

I wish to replace special characters like & ndash; and & mdash; occuring in an xml document with corresponding code like & #150; etc

i have an input xml

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-21 12:48

    Either the DTD mentioned at will include the entity references used (like ) or it is in error (or I suppose the input XML could have been in error in trying to use an entity it should be able to use).

    If it does include them, then you need to set your XSLT processor to validate the document according to its DTD. (I don't know how to do this in your case, as I know the XSLT part of the problem, but not the specifics of how to use XSLT in Java).

    If not, you'll have to fix it.

    Get a copy of http://www.w3.org/2003/entities/2007/w3centities-f.ent (while it would work to just reference that URI itself, the W3 would prefer if you didn't, and you'll not have better performance this way).

    Then create your own bookfull.dtd that includes:

    
    %w3centities-f;
    

    Or alternatively, that includes the contents of that file directly within the DTD.

    Now in interpreting the input document, the entity references can be resolved. For example, in the above is defined by:

    
    

    Or in other words; "whenever appears, replace it with ".

    This happens at the XML parsing step prior to the XSLT stylesheet being run, so as far as the XSLT is concerned, the content it received contained , not , and it treats it as such.

提交回复
热议问题