473,830 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I'm getting an extra, unwanted attribute

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 ByteArrayInputS tream( bytes );

DocumentBuilder docBuilder =
DocumentBuilder Factory.newInst ance().newDocum entBuilder();
Document doc = docBuilder.newD ocument();
DOMResult domResult = new DOMResult( doc );

TransformerFact ory xformFactory = TransformerFact ory.newInstance ();
Transformer xform = xformFactory.ne wTransformer();
xform.transform ( new StreamSource( is ), domResult );

Element xapDescElement = doc.createEleme ntNS(
"http://ns.adobe.com/xap/1.0/", "rdf:Descriptio n"
);
xapDescElement. setAttribute( "rdf:about" , "" );
xapDescElement. setAttribute( "xmlns:xap" , "http://ns.adobe.com/xap/1.0/" );

Node topRDF = doc.getDocument Element().getCh ildNodes().item ( 1 );
topRDF.appendCh ild( xapDescElement );

and when I write it out as text using:

Writer writer = new OutputStreamWri ter( System.out, "UTF-8" );
StreamResult streamResult = new StreamResult( writer );
xform = xformFactory.ne wTransformer();
xform.setOutput Property( OutputKeys.INDE NT, "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:Descriptio n 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
Feb 18 '07 #1
1 2825
On Feb 18, 10:29 pm,
pauljlucas.remo vet...@removeth istoo.mac.com (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.createEleme ntNS(
"http://ns.adobe.com/xap/1.0/", "rdf:Descriptio n"
);
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:Descriptio n 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
misunderstandin g 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:Descriptio n
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

Feb 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1497
by: scooterm | last post by:
### Question Can anyone name a text editor or XML editor (prefer open source) that can open a simple table XML file in a spreadsheet-type GUI, without adding any extra formatting or junk to the XML file? For example, given the following very simple XML file: <table> <row> <fname>fred</fname>
29
10774
by: Torbjørn Pettersen | last post by:
On http://www.fantafiction.com/forum/index.php there is a gap, but I don't really want it. Actually I don't want it at all. It's a discussion forum using phpBB. So if anyone has any experience in implementing phpBB, and has tips and tricks to share.... -- Torbjørn Pettersen Editor/Webmaster FantaFiction
1
1741
by: Stephen | last post by:
I have a really annoying problem with a datagrid. I have an application which populates a datagrid on the onclick event of a button. The datagrid is bound to an ArrayList which holds the values. Everything worked perfectly for me in my test application however when I copied and pasted the code accross to my main project something strange started to happen. On the on-click event of the button (cmdExcAdrContinue) the datagrid is populated...
6
1822
by: Michael | last post by:
Hi All, I need a few suggestions. I have the following XML segment: <LookUp> <ControlType>CheckBoxGroup</ControlType> <DBField>LastMedDate</DBField> <ControlName>cmbGoal1</ControlName> <Values VALUE="0" BookMark="Goal1Progress"/> <Values VALUE="1" BookMark="Goal1NoProgress"/> <Values VALUE="2" BookMark="Goal1NA"/> </LookUp>
2
2293
by: Kevin Chandler | last post by:
I appologize in advance if this is a novice question. I don't have much ASP.Net experience. I have an NCAA tournment selection page that I use asp:button controls. The problem is that everytime I click one of the buttons, the entire page redraws and the screen is scrolled to the top. How can I stop the entire screen from refreshing or how can I stop the page from scrolling back up to the top? Thanks in advance for your help,
1
1077
by: Nathan Sokalski | last post by:
As most of us probably know, ASP.NET controls do not use an exact copy of a control's ID for the HTML element's name or id attribute. When generating JavaScript as part of a control or event, we need to have a way to reference the HTML elements. The only way I have found to reference the HTML elements in my generated Javascript is the following: "document.getElementById('" & Me.ctrl.ClientID & "')" However, this seems like a lot of...
0
1545
by: Abhinay | last post by:
hi, this is abhinay, i have data ie xml file Ex: <root> <child1> <cc1>aaa</cc1> </child1>
7
13921
by: stef | last post by:
hello, In the previous language I used, when reading a line by readline, the EOL character was removed. Now I'm reading a text-file with CR+LF at the end of each line, Datafile = open(filename,'r') line = Datafile.readline() now this gives an extra empty line
1
1852
hokasu
by: hokasu | last post by:
In the following example, I want the user to input something like: 1 + 2 = 3, then (not included) test its validity. string op, eq; int num1, num2, user_result; cout<<"Please key in your equation."; cin >> num1>>op>>num2>>eq>>user_result; However, I need to "invalid input" this kind of input: 1 + 2 = 3 + 0
0
9781
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9641
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10769
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10477
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7740
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5615
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5777
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.