I'm getting an extra, unwanted attribute

Discussion in 'Java' started by Paul J. Lucas, Feb 18, 2007.

  1. 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
     
    Paul J. Lucas, Feb 18, 2007
    #1
    1. Advertising

  2. Paul J. Lucas

    roy axenov Guest

    On Feb 18, 10:29 pm,
    (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/", "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().

    --
    roy axenov
     
    roy axenov, Feb 18, 2007
    #2
    1. Advertising

  3. In comp.text.xml roy axenov <> wrote:
    > On Feb 18, 10:29 pm,
    > (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/", "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 ...


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

    - Paul
     
    Paul J. Lucas, Feb 18, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AMD Desktop
    Replies:
    3
    Views:
    1,947
    Steve C. Orr [MVP, MCSD]
    Nov 2, 2004
  2. fitwell
    Replies:
    2
    Views:
    532
    fitwell
    Oct 20, 2003
  3. Paul J. Lucas
    Replies:
    2
    Views:
    410
    Paul J. Lucas
    Feb 18, 2007
  4. mathieu
    Replies:
    3
    Views:
    620
    Bo Persson
    Sep 4, 2009
  5. , India
    Replies:
    7
    Views:
    337
    Jorgen Grahn
    Aug 13, 2010
Loading...

Share This Page