I'm getting an extra, unwanted attribute

P

Paul J. Lucas

I have code that builds a DOM using the Java org.w3c.dom API and it adds an
extra attribute to one of the elements that I don't want. Not only that, the
value it adds is wrong.

The code to create the DOM is:

String emptyXMPString =
"<x:xmpmeta xmlns:x='adobe:ns:meta/'>\n" +
" <rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'/>\n" +
"</x:xmpmeta>\n";
byte[] bytes = emptyXMPString.getBytes( "UTF-8" );
InputStream is = new ByteArrayInputStream( bytes );

DocumentBuilder docBuilder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docBuilder.newDocument();
DOMResult domResult = new DOMResult( doc );

TransformerFactory xformFactory = TransformerFactory.newInstance();
Transformer xform = xformFactory.newTransformer();
xform.transform( new StreamSource( is ), domResult );

Element xapDescElement = doc.createElementNS(
"http://ns.adobe.com/xap/1.0/", "rdf:Description"
);
xapDescElement.setAttribute( "rdf:about", "" );
xapDescElement.setAttribute( "xmlns:xap", "http://ns.adobe.com/xap/1.0/" );

Node topRDF = doc.getDocumentElement().getChildNodes().item( 1 );
topRDF.appendChild( xapDescElement );

and when I write it out as text using:

Writer writer = new OutputStreamWriter( System.out, "UTF-8" );
StreamResult streamResult = new StreamResult( writer );
xform = xformFactory.newTransformer();
xform.setOutputProperty( OutputKeys.INDENT, "yes" );
xform.transform( new DOMSource( doc ), streamResult );

I get:

<x:xmpmeta xmlns:x="adobe:ns:meta/">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description xmlns:xap="http://ns.adobe.com/xap/1.0/" rdf:about=""
xmlns:rdf="http://ns.adobe.com/xap/1.0/"/>
</rdf:RDF>
</x:xmpmeta>

There are two problems:
1. The second xmlns:rdf attribute shouldn't be there at all.
2. The value is wrong: it's the same as xmlns:xap. How did that happen?

I'd prefer to get tid of the extra xmlns:rdf attribute completely, but, if
I can't, how can I get it to at least be the correct value?

- Paul
 
R

roy axenov

I have code that builds a DOM using the Java org.w3c.dom
API and it adds an extra attribute to one of the elements
that I don't want. Not only that, the value it adds is
wrong.

Disclaimer: last time I coded in Java was years ago. Still,
the problem seems obvious to me.
Element xapDescElement = doc.createElementNS(
"http://ns.adobe.com/xap/1.0/", "rdf:Description"
);

Now, what are you doing here? You're your DOM API asking to
create a 'Description' element in
'http://ns.adobe.com/xap/1.0/' namespace, specifying the
'rdf' namespace prefix. Naturally, the result should look
as:

<rdf:Description xmlns:rdf="http://ns.adobe.com/xap/1.0/"/>

....which is precisely what you're getting.
xapDescElement.setAttribute( "rdf:about", "" );
xapDescElement.setAttribute( "xmlns:xap",
"http://ns.adobe.com/xap/1.0/" );

First of all, language lawyers might correct me if get
something wrong, but those xmlns:foo thingies are not
attributes per se, or at least, they are extremely special
attributes. The proper term is namespace declaration nodes,
or something like that. So, I doubt setAttribute() is the
proper way of creating them. And even if does work, what
did you expect to happen? You declaring a namespace with
xap namespace prefix, but the element uses the rdf prefix
anyway.

So the problem is not in the API, the API does precisely
what you asked it to do. The problem is that you seem
either unsure about what you actually want, or you're
misunderstanding something about the way the XML namespaces
work.

Since I'm completely unfamiliar with XMP and RDF, I'm not
sure what you're doing wrong about your data, but I'd
recommend re-reading the specs on those formats (and
probably look up some examples to see how it actually
works).
<x:xmpmeta xmlns:x="adobe:ns:meta/">
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description
xmlns:xap="http://ns.adobe.com/xap/1.0/" rdf:about=""
xmlns:rdf="http://ns.adobe.com/xap/1.0/"/>
</rdf:RDF>
</x:xmpmeta>

Sure, it looks wrong, but it's what you asked for.
There are two problems:
1. The second xmlns:rdf attribute shouldn't be there at
all.

You're creating an element in that namespace using the rdf
prefix--of course it has to be declared, since the previous
declaration differs.
2. The value is wrong: it's the same as xmlns:xap. How
did that happen?

It's the namespace you used in createElementNS().
 
P

Paul J. Lucas

In comp.text.xml roy axenov said:
On Feb 18, 10:29 pm,
(e-mail address removed) (Paul J. Lucas)
wrote:
Disclaimer: last time I coded in Java was years ago. Still,
the problem seems obvious to me.


Now, what are you doing here? You're your DOM API asking to
create a 'Description' element in 'http://ns.adobe.com/xap/1.0/'
namespace ...

Oh yeah. It was just a silly mistake I couldn't see. Nothing beats
another pair of eyes. Thanks!

- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top