Re: [xmljs-users] New to xml, looking for some guidance
Brought to you by:
djoham,
witchhunter
From: David J. <dj...@ya...> - 2004-07-07 02:03:19
|
Hello! Welcome to XML land. Please see answers in-lined below.... > > First, the method getElementById is quite useful. However, why is it that I cannot get an > element based on some attribute other than id? For example, I have the following xml: > > <faculty class="academic" id="sbeacon"> > <name>Shirley Beacon</name> > <email>sb...@st...</email> > </faculty> > > I can easily extract the node by domTree.getElementById('sbeacon'). But why not the more general > 'getElementByAttribute', where I specify the attribute, and the value? This way I could easily > extract all nodes having the same 'class' in this example. How could I do this? Does this > question stem from my lack of knowledge of xml? > The "id" tag is unique in XML. In ideal worlds, all "id" tags would be unique in an XML string. Thus, getElementById makes sense. Since "id" is the only attribute with this definition, a getElementByAttribute function doesn't make sense for an XML parser. However, if you need one, there is no reason why you couldn't just modify XML for <SCRIPT> to include it. All you would have to do is to clone the code in getElementById. One thing you might consider is XPath for the W3C Parser or TagPath for the classic parser. Both of these will give you the capability of searching by individual attributes. XPath is more powerful in that it will return multiple nodes in a single query. TagPath is more limited, but if you are only after a single node and you know the direct path to it, it may do the job. TagPath is documented in the selectNode documentation in the classic DOM. XPath is documented in the XPath contributed add-on. > Second, is there a better way to organize my xml? I have 3 or 4 categories for what I'm calling > 'class', and I would like to be able to group them (extract only academic, etc). > I'm going to need more info to answer this one, but in general XML is organized in a parent-child manner. For example: <languages> <c-syntax> <java></java> <cplusplus></java> <c></c> </c-syntax> <basic-syntax> <vb></vb> <vbscript></vbscript> </basic-syntax> </languages> you get the idea... > Third, how can I use html links within my xml document? Should I use a dummy character not > likely to occur, and do a replace for the < and > signs? Is there a better way? I have read the > documentation about the convertEscapes function, but as I understand it, this is intended for > escaping the <'s and >'s in the xml tags themselves. I have tried using < and > in the > html tag, and using convertEscapes on the extracted text, but this hasn't worked. Any > suggestions? > This is the ugly part of trying to do XML with javascript. Assuming you're sending the data to a text area, you would send a "<" character to the XML parser in the following way &lt; when the browser parses your text, it turns that text into < Then, it is converted into a "<" character by the XML Parser itself. Ugly, huh? One option is to do something similar to what convertEscapes does, but with different characters. A little trial and error will generally get you through this ugly part. > Finally, browser compatibility is a concern for me. What browsers can handle the classic DOM? > Pretty much any generation 4 browser and up can handle the classic DOM. All modern browsers can handle it just fine as well. > Thank you. I am learning as I go, and any help is really greatly appreciated. > > -- > Chris You're welcome. Good luck! David |