You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Ias <ia...@apache-korea.org> on 2003/05/08 19:09:32 UTC

[PATCH] Allowing XMLSerializer to output " (quotation mark) as it is

Often it is convenient not to convert " to &quot since it's accepted,
tidier, and more readable. Moreover, it is required in case you need to
keep your XML document as it is whenever you modify and store it by
serializing it with XMLSerializer.

For example,

Supposing person.xml
...
  <person id="four.worker">
    <name><family>Worker "Ias"</family> <given>Four</given></name>
    <email>four@foo.com</email>
    <link manager="Big.Boss"/>
  </person>
...

and using the following codes

		Document doc = domParser.parse(new
InputData("personal.xml"));
		OutputFormat format = new OutputFormat(doc);
		format.setIndenting(true);
		XMLSerializer serializer = new XMLSerializer(System.out,
format);
		serializer.serialize(doc); 

results in

...
    <person id="four.worker">
        <name>
            <family>Worker &quot;Ias&quot;</family>
            <given>Four</given>
        </name>
        <email>four@foo.com</email>
        <link manager="Big.Boss"/>
    </person>
...

The patches attached to this message enables you to skip the conversion, so

		Document doc = mydb.parse(new InputData("personal.xml"));
		OutputFormat format = new OutputFormat(doc);
		format.setIndenting(true);
		format.setKeepTextNodeQuot(true); // keep " as it is.
		XMLSerializer serializer = new XMLSerializer(System.out,
format);
		serializer.serialize(doc); 

displays

    <person id="four.worker">
        <name>
            <family>Worker "Ias"</family> 
            <given>Four</given>
        </name>
        <email>four@foo.com</email>
        <link manager="Big.Boss"/>
    </person>  

,which is identical to person.xml.

Hoping these patches are helpful,

Ias.

===========================================================
Lee, Changshin (Korean name)
Ias (International name)
               Company Web Site: http://www.tmax.co.kr
               Personal Web Site: http://www.iasandcb.pe.kr
---------------------------------------------------------
Senior Researcher, Emerging Technology Evangelist & JCP Activities
Coordinator
JCP member - http://jcp.org/en/participation/members/L
R&D Institute
Tmax Soft, Inc. 
JCP member - http://jcp.org/en/participation/members/T
==========================================================

Re: [PATCH] Allowing XMLSerializer to output " (quotation mark) as it is

Posted by Elena Litani <el...@ca.ibm.com>.
Hi Ias, 

Thank you for the patch. I've committed changes to the CVS with minor
modifications:

1. According to the XML 1.0 "quot" entity should be used to convert
double quotes in attribute values.
The spec does not require having an option to do the same for character
data. So I did not commit the new option you've provided on the format,
instead by default double quotes are output as is in character data.
2. I've removed old method and new printTextXML and instead I am using
your method (printXMLChar(int, boolean)).
3. Updated XMLSerializer11 (XML 1.1) to reflect the changes introduced
to serializer.

Thank you for your help,
-- 
Elena Litani / IBM Toronto

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org


RE: [PATCH] Allowing XMLSerializer to output " (quotation mark) as it is

Posted by Ias <ia...@apache-korea.org>.
First of all, thank you for pointing out the issue. I tested the following
case with the previous patches:

	public void testXMLSerialize() throws Exception {
		DOMImplementation di = db.getDOMImplementation();
		Document doc =
di.createDocument("http://www.w3.org/XML/1998/namespace", "web", null);
		Element webElement = doc.getDocumentElement();
		webElement.setAttribute("test", "\"OK\"");
		Text text = doc.createTextNode("\"Ias\"");
		webElement.appendChild(text);
		OutputFormat format = new OutputFormat(doc);
		format.setLineSeparator(LineSeparator.Windows);
		format.setIndenting(true);
		format.setKeepTextNodeQuot(true);
		XMLSerializer serializer = new XMLSerializer(System.out,
format);
		serializer.serialize(doc); 
	}

As you wrote, the result was not well-formed.

<?xml version="1.0" encoding="UTF-8"?>
<web test=""OK"">"Ias"</web>

I revisited printXMLChar method and made some approaches:

1. printXMLChar method has 2 slightly different versions.

printXMLChar(int ch, boolean keepQuot) processes output. printXMLChar(int
ch) is a wrapper to call printXMLChar(int ch, false), which brings you &quot
all the time. 

2. printText methods use printTextXMLChar method instead of printXMLChar.

printTextXMLChar method is to call printXMLChar(int ch, boolean keepQuot)
based on keepTextNodeQuot field of _format object.

The patch attached to this message implements the above approaches. The test
result is

<?xml version="1.0" encoding="UTF-8"?>
<web test="&quot;OK&quot;">"Ias"</web>

, which is well-formed naturally.

The main idea is to differentiate text output from the other output such as
attribute value and namespace uri.

Best Regards,

Ias.

> -----Original Message-----
> From: Elena Litani [mailto:elitani@ca.ibm.com] 
> Sent: Friday, May 09, 2003 4:23 AM
> To: xerces-j-dev@xml.apache.org
> Subject: Re: [PATCH] Allowing XMLSerializer to output " 
> (quotation mark) as it is
> 
> 
> Hi Ias,
> 
> Thank you for looking into this problem. 
> 
> Unfortunately, the fix you provide will make serialize 
> produce not well-formed XML. The printXMLChar is used to 
> print both - character data and attribute values. If '"' 
> appear in attribute value it must be converted to &quot; 
> since attribute values serialized in double quotes.
> 
> The correct fix would be to make serialize convert double 
> quote to quot reference only in attribute values, but avoid 
> doing it for character data. See REVISIT in printXMLChar method. 
> 
> If you can look into providing a correct patch that would be great!
> 
> Thank you,
> -- 
> Elena Litani / IBM Toronto
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
> 
> 

RE: [PATCH] Allowing XMLSerializer to output " (quotation mark) as it is

Posted by Ias <ia...@apache-korea.org>.
First of all, thank you for pointing out the issue. I tested the
following case with the previous patches:

	public void testXMLSerialize() throws Exception {
		DOMImplementation di = db.getDOMImplementation();
		Document doc =
di.createDocument("http://www.w3.org/XML/1998/namespace", "web", null);
		Element webElement = doc.getDocumentElement();
		webElement.setAttribute("test", "\"OK\"");
		Text text = doc.createTextNode("\"Ias\"");
		webElement.appendChild(text);
		OutputFormat format = new OutputFormat(doc);
		format.setLineSeparator(LineSeparator.Windows);
		format.setIndenting(true);
		format.setKeepTextNodeQuot(true);
		XMLSerializer serializer = new XMLSerializer(System.out,
format);
		serializer.serialize(doc); 
	}

As you wrote, the result was not well-formed.

<?xml version="1.0" encoding="UTF-8"?>
<web test=""OK"">"Ias"</web>

I revisited printXMLChar method and made some approaches:

1. printXMLChar method has 2 slightly different versions.

printXMLChar(int ch, boolean keepQuot) processes output.
printXMLChar(int ch) is a wrapper to call printXMLChar(int ch, false),
which brings you &quot all the time. 

2. printText methods use printTextXMLChar method instead of
printXMLChar.

printTextXMLChar method is to call printXMLChar(int ch, boolean
keepQuot) based on keepTextNodeQuot field of _format object.

The patch attached to this message implements the above approaches. The
test result is

<?xml version="1.0" encoding="UTF-8"?>
<web test="&quot;OK&quot;">"Ias"</web>

, which is well-formed naturally.

The main idea is to differentiate text output from the other output such
as attribute value and namespace uri.

Best Regards,

Ias.

-----Original Message-----
From: Elena Litani [mailto:elitani@ca.ibm.com] 
Sent: Friday, May 09, 2003 4:23 AM
To: xerces-j-dev@xml.apache.org
Subject: Re: [PATCH] Allowing XMLSerializer to output " (quotation mark)
as it is

Hi Ias,

Thank you for looking into this problem. 

Unfortunately, the fix you provide will make serialize produce not
well-formed XML. The printXMLChar is used to print both - character data
and attribute values. If '"' appear in attribute value it must be
converted to &quot; since attribute values serialized in double quotes.

The correct fix would be to make serialize convert double quote to quot
reference only in attribute values, but avoid doing it for character
data. See REVISIT in printXMLChar method. 

If you can look into providing a correct patch that would be great!

Thank you,
-- 
Elena Litani / IBM Toronto

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org


Re: [PATCH] Allowing XMLSerializer to output " (quotation mark) as it is

Posted by Elena Litani <el...@ca.ibm.com>.
Hi Ias,

Thank you for looking into this problem. 

Unfortunately, the fix you provide will make serialize produce not
well-formed XML. The printXMLChar is used to print both - character data
and attribute values. If '"' appear in attribute value it must be
converted to &quot; since attribute values serialized in double quotes.

The correct fix would be to make serialize convert double quote to quot
reference only in attribute values, but avoid doing it for character
data. See REVISIT in printXMLChar method. 

If you can look into providing a correct patch that would be great!

Thank you,
-- 
Elena Litani / IBM Toronto

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org