Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > I'm getting an extra, unwanted attribute

Reply
Thread Tools

I'm getting an extra, unwanted attribute

 
 
Paul J. Lucas
Guest
Posts: n/a
 
      02-18-2007
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 =
"<xmpmeta xmlns='adobe:ns:meta/'>\n" +
" <rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'/>\n" +
"</xmpmeta>\n";
byte[] bytes = emptyXMPString.getBytes( "UTF-8" );
InputStream is = new ByteArrayInputStream( bytes );

DocumentBuilder docBuilder =
DocumentBuilderFactory.newInstance().newDocumentBu ilder();
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/", "rdfescription"
);
xapDescElement.setAttribute( "rdf:about", "" );
xapDescElement.setAttribute( "xmlnsap", "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:

<xmpmeta xmlns="adobe:ns:meta/">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdfescription xmlnsap="http://ns.adobe.com/xap/1.0/" rdf:about=""
xmlns:rdf="http://ns.adobe.com/xap/1.0/"/>
</rdf:RDF>
</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 xmlnsap. 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
 
Reply With Quote
 
 
 
 
roy axenov
Guest
Posts: n/a
 
      02-18-2007
On Feb 18, 10:29 pm,
(E-Mail Removed) (Paul J. Lucas)
wrote:
> 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/", "rdfescription"
> );


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:

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

....which is precisely what you're getting.

> xapDescElement.setAttribute( "rdf:about", "" );
> xapDescElement.setAttribute( "xmlnsap",
> "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).

> <xmpmeta xmlns="adobe:ns:meta/">
> <rdf:RDF
> xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
> <rdfescription
> xmlnsap="http://ns.adobe.com/xap/1.0/" rdf:about=""
> xmlns:rdf="http://ns.adobe.com/xap/1.0/"/>
> </rdf:RDF>
> </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 xmlnsap. How
> did that happen?


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

--
roy axenov

 
Reply With Quote
 
 
 
 
Paul J. Lucas
Guest
Posts: n/a
 
      02-18-2007
In comp.text.xml roy axenov <(E-Mail Removed)> wrote:
> On Feb 18, 10:29 pm,
> (E-Mail Removed) (Paul J. Lucas)
> wrote:


> 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/", "rdfescription"
> > );

>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting rid of unwanted characters in the input subramanian100in@yahoo.com, India C++ 7 08-13-2010 01:52 PM
I'm getting an extra, unwanted attribute Paul J. Lucas Java 2 02-18-2007 09:38 PM
picking value of one attribute based on a child elements attribute? XPath? vjethava@gmail.com XML 2 03-06-2006 05:19 AM
XSLT: Making attribute to parent attribute Bostonasian XML 1 09-18-2005 07:30 AM
class attribute to instance attribute Donnal Walter Python 4 07-06-2005 04:35 AM



Advertisments