You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Daniel Yokomizo <da...@gmail.com> on 2008/03/29 21:45:24 UTC

How to disable attribute normalization

Hi,

    I'm parsing (disabling validation) a document that declared a DTD
but I would like to get the raw attribute values instead of the
normalized values. In particular I need to keep entity references as
they were written. I came up with this FAQ
(http://xerces.apache.org/xerces-j/faq-write.html#faq-7) that seems to
declare that it is impossible (i.e. attribute normalization happens if
there's a DTD present) and I found the XMLScanner class that, via the
method scanAttributeValue, does the attribute normalization. I noticed
that we have a getNonNormalizedValue() method but the SAX parser layer
uses AttributesProxy which hides the getNonNormalizedValue() method.
    Is there any way to configure Xerces to not normalize attribute
values even when the DTD is declared?

    Best regards,
    Daniel Yokomizo

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


Re: How to disable attribute normalization

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
keshlam@us.ibm.com wrote on 03/30/2008 05:53:51 PM:

>
> > > If you need to know entity-reference boundaries, you can get that
> > > information by asking the parser to generate a DOM which has Entity
> > > Reference Nodes and then looking at the children of the attribute
> > > nodes.
> >
> > Xerces-J doesn't support that. See the rationale here [1] from Andy
Clark.
>
> Beg pardon? There's a long-standing Feature for the DOM parser,
> create-entity-ref-nodes, which toggles this... and in fact, the
> documentation says it defaults to true.
>
> Are you confused, or is the documentation out of date?

That feature only has an effect on entity references which appear in
element content. Xerces has no support [1][2] for preserving entity
references in attribute values.

[1] http://marc.info/?t=117027061700003&r=1&w=2
[2] http://issues.apache.org/jira/browse/XERCESJ-1225

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org


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


Re: How to disable attribute normalization

Posted by ke...@us.ibm.com.
> > If you need to know entity-reference boundaries, you can get that
> > information by asking the parser to generate a DOM which has Entity
> > Reference Nodes and then looking at the children of the attribute
> > nodes.
> 
> Xerces-J doesn't support that. See the rationale here [1] from Andy 
Clark.

Beg pardon? There's a long-standing Feature for the DOM parser, 
create-entity-ref-nodes, which toggles this... and in fact, the 
documentation says it defaults to true.

Are you confused, or is the documentation out of date?

Re: How to disable attribute normalization

Posted by Daniel Yokomizo <da...@gmail.com>.
On Sun, Mar 30, 2008 at 6:17 PM,  <ke...@us.ibm.com> wrote:
>
> If you need to know entity-reference boundaries, you can get that
> information by asking the parser to generate a DOM which has Entity
> Reference Nodes and then looking at the children of the attribute nodes.
> (I'm not sure offhand whether there's a SAX equivalent.)

I using SAX rather than DOM because my library is compiling a xml
template (not XSL but another template format), so for me building the
AST using SAX is much easier and reduces both the time and the space
usage. Using DOM in this case would just create a large intermediate
representation that would be discarded anyway and would force me to
walk the tree instead of doing a state machine (which I prefer for
compilers).

> If you want to suppress the other kinds of normalization applied to
> attributes, I would suggest that you don't want attributes in the first
> place. Change your document design so this information is carried by a child
> element. (See much past discussion -- just about everywhere that XML is
> discussed! -- about when to use attributes vs. child elements; this is one
> classic example where there is a practical reason rather than just one of
> style.)

What I'm doing can't really restrict the things that the source XML
contains, because any XML is a valid template for my library. For
example if I have <foo bar="foo &amp; bar"/> I need to be able to
parse this and compile a template that when run will result in <foo
bar="foo &amp; bar"/>, essentially the identity function. Actually
this is one of the problems that (now) I don't think if it's possible
to solve with SAX: create a handler that will build a exact copy of
the parsed XML.

>  ______________________________________
>  "... Three things see no end: A loop with exit code done wrong,
>  A semaphore untested, And the change that comes along. ..."
>   -- "Threes" Rev 1.1 - Duane Elms / Leslie Fish
> (http://www.ovff.org/pegasus/songs/threes-rev-11.html)

Best regards,
Daniel Yokomizo.

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


Re: How to disable attribute normalization

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
keshlam@us.ibm.com wrote on 03/30/2008 02:17:01 PM:

> If you need to know entity-reference boundaries, you can get that
> information by asking the parser to generate a DOM which has Entity
> Reference Nodes and then looking at the children of the attribute
> nodes.

Xerces-J doesn't support that. See the rationale here [1] from Andy Clark.

> (I'm not sure offhand whether there's a SAX equivalent.)

There isn't one. SAX has no mechanism for reporting entity reference
boundaries within attribute values.

> If you want to suppress the other kinds of normalization applied to
> attributes, I would suggest that you don't want attributes in the
> first place. Change your document design so this information is
> carried by a child element. (See much past discussion -- just about
> everywhere that XML is discussed! -- about when to use attributes
> vs. child elements; this is one classic example where there is a
> practical reason rather than just one of style.)
>
> ______________________________________
> "... Three things see no end: A loop with exit code done wrong,
> A semaphore untested, And the change that comes along. ..."
>  -- "Threes" Rev 1.1 - Duane Elms / Leslie Fish (http://www.ovff.
> org/pegasus/songs/threes-rev-11.html)

[1]
http://mail-archives.apache.org/mod_mbox/xerces-j-dev/200109.mbox/%3c3B9C5915.E05D28AC@apache.org%3e

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org


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


Re: How to disable attribute normalization

Posted by ke...@us.ibm.com.
If you need to know entity-reference boundaries, you can get that 
information by asking the parser to generate a DOM which has Entity 
Reference Nodes and then looking at the children of the attribute nodes. 
(I'm not sure offhand whether there's a SAX equivalent.)

If you want to suppress the other kinds of normalization applied to 
attributes, I would suggest that you don't want attributes in the first 
place. Change your document design so this information is carried by a 
child element. (See much past discussion -- just about everywhere that XML 
is discussed! -- about when to use attributes vs. child elements; this is 
one classic example where there is a practical reason rather than just one 
of style.)

______________________________________
"... Three things see no end: A loop with exit code done wrong,
A semaphore untested, And the change that comes along. ..."
  -- "Threes" Rev 1.1 - Duane Elms / Leslie Fish (
http://www.ovff.org/pegasus/songs/threes-rev-11.html)

Re: How to disable attribute normalization

Posted by Daniel Yokomizo <da...@gmail.com>.
Hi Michael,

On Sun, Mar 30, 2008 at 5:35 PM, Michael Glavassevich
<mr...@ca.ibm.com> wrote:
> Hi Daniel,
>
>  "Daniel Yokomizo" <da...@gmail.com> wrote on 03/29/2008 04:45:24
>  PM:
>
>
>  > Hi,
>  >
>  >     I'm parsing (disabling validation) a document that declared a DTD
>  > but I would like to get the raw attribute values instead of the
>  > normalized values. In particular I need to keep entity references as
>  > they were written. I came up with this FAQ
>  > (http://xerces.apache.org/xerces-j/faq-write.html#faq-7) that seems to
>  > declare that it is impossible (i.e. attribute normalization happens if
>  > there's a DTD present) and I found the XMLScanner class that, via the
>  > method scanAttributeValue, does the attribute normalization. I noticed
>  > that we have a getNonNormalizedValue() method but the SAX parser layer
>  > uses AttributesProxy which hides the getNonNormalizedValue() method.
>
>  That method is part of XNI [1]. If you really need the non-normalized text
>  you'd need to change your application so that it uses XNI directly (rather
>  than SAX).

Thanks for your help (again). I was hoping to use the SAX interface
and not depend explicitly on Xerces, because I'm developing a library
which will be (hopefully) independent of the SAX implementation.
There's a hack I can do to "trick" Xerces, which will work with any
parser too, and I'll probably do it (essentially I'll decorate the
reader I'm giving to the parser transforming every & into &amp; but
after it's resolved by the parser it'll become & again, so &amp;
becomes &amp;amp; which the parser transform into &amp;.

>  >     Is there any way to configure Xerces to not normalize attribute
>  > values even when the DTD is declared?
>
>  Whether your document has a DTD or not is irrelevant. The FAQ (on the
>  Xerces 1.x site) you read is wrong. Normalization [2] is required for every
>  attribute value. You cannot disable this behaviour.
>
>  >     Best regards,
>  >     Daniel Yokomizo
>  >
>
>  Thanks.
>
>  [1] http://xerces.apache.org/xerces2-j/javadocs/xni/index.html
>  [2] http://www.w3.org/TR/2006/REC-xml-20060816/#AVNormalize
>
>  Michael Glavassevich
>  XML Parser Development
>  IBM Toronto Lab
>  E-mail: mrglavas@ca.ibm.com
>  E-mail: mrglavas@apache.org

Best regards,
Daniel Yokomizo.

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


Re: How to disable attribute normalization

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Daniel,

"Daniel Yokomizo" <da...@gmail.com> wrote on 03/29/2008 04:45:24
PM:

> Hi,
>
>     I'm parsing (disabling validation) a document that declared a DTD
> but I would like to get the raw attribute values instead of the
> normalized values. In particular I need to keep entity references as
> they were written. I came up with this FAQ
> (http://xerces.apache.org/xerces-j/faq-write.html#faq-7) that seems to
> declare that it is impossible (i.e. attribute normalization happens if
> there's a DTD present) and I found the XMLScanner class that, via the
> method scanAttributeValue, does the attribute normalization. I noticed
> that we have a getNonNormalizedValue() method but the SAX parser layer
> uses AttributesProxy which hides the getNonNormalizedValue() method.

That method is part of XNI [1]. If you really need the non-normalized text
you'd need to change your application so that it uses XNI directly (rather
than SAX).

>     Is there any way to configure Xerces to not normalize attribute
> values even when the DTD is declared?

Whether your document has a DTD or not is irrelevant. The FAQ (on the
Xerces 1.x site) you read is wrong. Normalization [2] is required for every
attribute value. You cannot disable this behaviour.

>     Best regards,
>     Daniel Yokomizo
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org

Thanks.

[1] http://xerces.apache.org/xerces2-j/javadocs/xni/index.html
[2] http://www.w3.org/TR/2006/REC-xml-20060816/#AVNormalize

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org


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