Thread: [xmljs-users] New to xml, looking for some guidance
Brought to you by:
djoham,
witchhunter
From: Christopher B. <cb...@co...> - 2004-07-06 23:36:48
|
Hi xmljs-users, I am very new to xml, and even newer to xml for <script>. I am very unsure of the best way to proceed, and would appreciate an insight, guidance, or cautions anyone can provide. I am charged with updating the website for our academic department, and the only tool available to me is SSI (and javascript). I have gotten the classic DOM to do what I would like, namely extract bits of info, depending on a tag id. I have several questions. 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? 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). 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? Finally, browser compatibility is a concern for me. What browsers can handle the classic DOM? Thank you. I am learning as I go, and any help is really greatly appreciated. -- Chris |
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 |
From: Christopher B. <cb...@co...> - 2004-07-07 03:47:41
|
Hi David, DJ> The "id" tag is unique in XML. In ideal DJ> worlds, all "id" tags would be unique in an DJ> XML string. Thus, getElementById makes sense. DJ> Since "id" is the only attribute with this DJ> definition, a getElementByAttribute function DJ> doesn't make sense for an XML parser. However, DJ> if you need one, there is no reason why you DJ> couldn't just modify XML for <SCRIPT> to DJ> include it. All you would have to do is to DJ> clone the code in getElementById. I see. I had never thought to modify the script. Would it be difficult to have it return all of the nodes that match? I acknowledge your point about 'id' being ideally unique, but what about another attribute, for which several nodes share a common value? I imagine that this would be a quick way to organize data. I also imagine that this is what XPath is for? This is what I meant by my question about a better way to organize my data. Now that I know a bit more of the nomenclature, let me rephrase...Is a tag attribute a good way of categorizing groups of nodes, or should they all just be child nodes? From your example, I take it the latter? >> Third, how can I use html links within my xml <snip>... DJ> This is the ugly part of trying to do XML with DJ> javascript. <snip>... I see...Down & dirty. Sometimes there ain't no shortcuts. DJ> You're welcome. Good luck! Thank you for your thoughtful reply. -- Chris |
From: David J. <dj...@ya...> - 2004-07-07 15:10:57
|
<<Is a tag attribute a good way of categorizing groups of nodes, or should they all just be child nodes? From your example, I take it the latter?>> IMHO, it's better to organize them as child nodes. It make processing easier as well as making the XML code easier to humanly read. It's not always possible though - and the fact that XML still works in the non-perfect-world cases is one of the strong points of the technology. It would be very simple for you to modify the script to return all of the nodes with the specific attribute and value. In the getElementBy* function (either mine or yours) all you need to do is loop until you find a node you're interested in. Instead of returning the node and returning, add the node to a temp array or something and then return. Once you've looped through the entire tree, return the array. XML for <SCRIPT>'s license not only permits you to do this, but encourages you to do so! Best regards, David --- Christopher Brown <cb...@co...> wrote: > Hi David, > > DJ> The "id" tag is unique in XML. In ideal > DJ> worlds, all "id" tags would be unique in an > DJ> XML string. Thus, getElementById makes sense. > DJ> Since "id" is the only attribute with this > DJ> definition, a getElementByAttribute function > DJ> doesn't make sense for an XML parser. However, > DJ> if you need one, there is no reason why you > DJ> couldn't just modify XML for <SCRIPT> to > DJ> include it. All you would have to do is to > DJ> clone the code in getElementById. > > I see. I had never thought to modify the script. Would it be difficult to have it return all of > the nodes that match? I acknowledge your point about 'id' being ideally unique, but what about > another attribute, for which several nodes share a common value? I imagine that this would be a > quick way to organize data. I also imagine that this is what XPath is for? This is what I meant > by my question about a better way to organize my data. Now that I know a bit more of the > nomenclature, let me rephrase...Is a tag attribute a good way of categorizing groups of nodes, > or should they all just be child nodes? From your example, I take it the latter? > > >> Third, how can I use html links within my xml > > <snip>... > > DJ> This is the ugly part of trying to do XML with > DJ> javascript. > > <snip>... > > I see...Down & dirty. Sometimes there ain't no shortcuts. > > DJ> You're welcome. Good luck! > > Thank you for your thoughtful reply. > > -- > Chris > > > > ------------------------------------------------------- > This SF.Net email sponsored by Black Hat Briefings & Training. > Attend Black Hat Briefings & Training, Las Vegas July 24-29 - > digital self defense, top technical experts, no vendor pitches, > unmatched networking opportunities. Visit www.blackhat.com > _______________________________________________ > xmljs-users mailing list > xml...@li... > https://lists.sourceforge.net/lists/listinfo/xmljs-users > |
From: Christopher B. <cb...@co...> - 2004-07-07 18:48:36
|
Hi David, DJ> IMHO, it's better to organize them as child DJ> nodes. <snip>... Yes, now I think I get it. Instead of: <faculty class="academic" id="sbeacon"> <name>Shirley Beacon</name> <email>sb...@st...</email> </faculty> I now have: <class id="academic"> <faculty id="sbeacon"> <name>Shirley Beacon</name> <email>sb...@st...</email> </faculty> </class> DJ> It would be very simple for you to modify the DJ> script to return all of the nodes with the DJ> specific attribute and value. Doesn't seem needed, now, but thanks. A few other quick questions...First, do I need the <root> node? Does it have to be called 'root'? Also, I have seen a few examples showing how to use the browser to parse xml. Should I consider this? Advantages/disadvantages? Thanks! I'm out of questions (for now), I promise! -Chris |