Issue Details (XML | Word | Printable)

Key: XALANJ-2091
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Christine Li
Reporter: Felix Röthenbacher
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
XalanJ2

Namespace attributes not added as attributes when using TransformerIdentityImpl

Created: 05/Apr/05 12:14 AM   Updated: 06/Feb/08 05:09 AM
Return to search
Component/s: transformation
Affects Version/s: 2.6
Fix Version/s: 2.7.1

Time Tracking:
Not Specified

File Attachments:
  Size
Text File Licensed for inclusion in ASF works Patch.txt 2006-11-08 07:13 PM Christine Li 3 kB
Text File Licensed for inclusion in ASF works TransformerIdentityImpl.java.diff 2005-04-05 12:17 AM Felix Röthenbacher 0.8 kB
Issue Links:
Dependants
 
Reference
 

Xalan info: PatchAvailable
Resolution Date: 08/Nov/06 09:07 PM


 Description  « Hide
Namespace attributes are not added as attributes to the resulting
DOM tree when using TransformerIdentityImpl. The problem is, that
the namespace-prefix feature is not set to true and therefore the
namespace attributes are not added. This does not conform to the
DOM spec saying that also namespace attributes may be handled as
normal attributes.

I will attach a patch which fixes the issue by calling

           reader.setFeature("http://xml.org/sax/features/namespace-prefixes",
                             true);

of the XMLReader.


The following code shows the problem:

The input XML document looks like this:

<?xml version="1.0"?>
<test xmlns:testns="http://testns.org" testns:testattr="3"/>

And the code used to extract the namespace attributes is as follows:

  FileInputStream fis = new FileInputStream("test.xml");
  Transformer trfs = TransformerFactory.newInstance().newTransformer();
  StreamSource ss = new StreamSource(fis);
  DOMResult dr = new DOMResult();
  trfs.transform(ss, dr);
  Document doc = (Document)dr.getNode();
  Node node = doc.getFirstChild();
  System.out.println("First node: " + node.getLocalName());
  NamedNodeMap nodes = node.getAttributes();
  for (int i=0; i<nodes.getLength(); i++) {
    System.out.println(" Attribute: " + nodes.item(i).getLocalName());
  }

Finally, the the output looks like this:

  First node: test
  Attribute: testattr

instead of the expected output:

  First node: test
  Attribute: testns
  Attribute: testattr

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Felix Röthenbacher added a comment - 05/Apr/05 12:17 AM
Patch for enabling namespace attribute inclusion.

Brian Minchau added a comment - 05/Apr/05 04:50 AM
The patch needs careful consideration. In a note to xalan-j-users David Bertoni wrote:
>
>
> That's because namespace declaration are represented in the data model as
> namespaces nodes, not attribute nodes:
>
>
> http://www.w3.org/TR/xpath#namespace-nodes
>
> Dave
>

I too have concerns about the patch. In a serialized XML document (a stream of characters, usually written to a file) namespace nodes appear as attributes, xmlns:prefix="someURI".
But that doesn't mean that they actually ARE attributes in the data model. It seems to me with Felix suggested change that the DOM would have both a namespace node and an attribute for the prefix/uri mapping, and I'm not sure that having both is correct.


Felix Röthenbacher added a comment - 05/Apr/05 06:59 AM
>Comment by Brian Minchau [04/Apr/05 09:50 PM]
>
>The patch needs careful consideration. In a note to xalan-j-users David Bertoni wrote:
>>
>>
>> That's because namespace declaration are represented in the data model as
>> namespaces nodes, not attribute nodes:
>>
>>
>> http://www.w3.org/TR/xpath#namespace-nodes
>>
>> Dave
>>
>
>I too have concerns about the patch. In a serialized XML document (a stream of characters,
>usually written to a >file) namespace nodes appear as attributes, xmlns:prefix="someURI".
>But that doesn't mean that they actually ARE attributes in the data model. It seems to me
>with Felix suggested >change that the DOM would have both a namespace node and an attribute
>for the prefix/uri mapping, and I'm not sure that having both is correct.

If you are speaking about DOM and its relevant data model you have to look at
its specification http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/
especially the chapter about namespaces: http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#Namespaces-Considerations
where it states that

"... As far as the DOM is concerned, special attributes used for declaring XML namespaces are
still exposed and can be manipulated just like any other attribute. .."

That means in words of the DOM API that you have a org.w3c.dom.Node object from the DOMResult
produced by the transformation with its various node types. But, there is no namespace node
type and namespace attributes are handled "... just like any other attribute".

It's strange that the sources from Sun's JDK expose an implementation in that way (based
on Xalan) but the current trunk does not. It looks like a regression happened here.

Michael Wechner added a comment - 05/Apr/05 07:02 AM
It seems that this "feature" actually works with older Xalan versions, like for instance the one which is shipped with SUN JDK 1.4 (JDK 1.4 is packaged with an old version (2.2D11).

So it might have been removed on purpose from the recent versions and one needs to handle the namespaces differently, but if so, then how?

Thanks

Michi

Henry Zongaro added a comment - 24/Oct/06 07:44 PM
I think this is the same problem described in XALANJ-1749, but I haven't confirmed so I won't return this as a duplicate.

Henry Zongaro added a comment - 24/Oct/06 07:50 PM
I guess I should have pointed to XALANJ-762, since XALANJ-1749 was returned as a duplicate of 762.

Brian Minchau added a comment - 06/Nov/06 09:48 PM
Assigning to Christine Li per the JIRA triage on November 6, 2006.

Christine Li added a comment - 08/Nov/06 07:13 PM
A patch to fix the namespace nodes for DOMBuilder.

Henry Zongaro added a comment - 08/Nov/06 07:59 PM
I have reviewed Christine's patch[1] and believe that it correctly resolves the problem.

[1] https://issues.apache.org/jira/secure/attachment/12344607/Patch.txt

Christine Li added a comment - 08/Nov/06 09:07 PM
The patch has been checked in. Please verify the changes. Thanks,

Brian Minchau added a comment - 11/Dec/07 04:57 PM
Would the originator of this issue please verify that this issue is fixed in the 2.7.1 release, by adding a comment to this issue, so that we can close this issue.

A lack of response by February 1, 2008 will be taken as consent that we can close this resolved issue.

Regards,
Brian Minchau

Jörg Heinicke added a comment - 27/Dec/07 01:11 PM - edited
In my opinion there is still an issue with the implementation. Since startPrefixMapping() now changes the state of the DOMBuilder endPrefixMapping() must be implemented as well. The current implementation breaks at least when namespace prefixes are reassociated with a different namespace uri after the scope of the inner prefix mapping is left.

Jörg Heinicke added a comment - 06/Feb/08 05:09 AM
Any response to my last comment/concern?