Jamie
Jamie

Reputation: 85

javax Transformer preserve the escaping entity

I finally gave up the transformation of xslt after the failure of several tryings (if you are interested, you can check my question here: don't want xslt intepret the entity characters). And now I'm working on the javax Transformer trying to solve the problem.

The problem is that I would like to keep the escaping of apostrophe ' in the xml and html:

< cell i='0' j='0' vi='0' parity='Odd' subType='-1'> &#34;String&#39;</cell> 

output is what I don't want:

    < td nowrap="true" class="gridCell" i="0" j="0">"String'< /td>

I would like the result as follows:

    < td nowrap="true" class="gridCell" i="0" j="0">&#34;String&#39;< /td>

I don't know whether we could use the method transform to do this, and I see a similar question, but he needs the opposite thing : How Do You Prevent A javax Transformer From Escaping Whitespace?

I appreciate any help from you, thank you!

Upvotes: 0

Views: 728

Answers (2)

Flynn1179
Flynn1179

Reputation: 12075

Strictly speaking, there's no difference to an XML parser between an ', &#39;, &#x27 or &apos; in a text node, In an attribute it's slightly different, given that the value has to be enclosed between " or '; if you've enclosed an attribute's value in ', you MUST use an entity to specify an apostrophe within the value, and an XML serializer will do the same.

I'm a bit rusty with XML handling in Java, but I know in C# you can have your transform generate an XML document object, and create a class that inherits from the XmlWriter class to serialize it however you wish, by overriding the 'WriteString' method. Hopefully there's something similar you can do in Java, possibly by implementing the Result interface, or perhaps passing a DOMResult in that you can work with. I can't remember how you normally serialize a Document in java, but it should be something you can manipulate or override.

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163262

XML defines &apos; and ' to mean the same thing, so most XML tools are going to treat the distinction as irrelevant. I think you should question the requirement - why are you trying to do this? It's a bit like trying to preserve the spaces around the "=" sign in an attribute: it's pointless.

Upvotes: 1

Related Questions