19 Replies Latest reply on Apr 22, 2007 3:19 AM by Newsgroup_User

    xml object getElementById

    Matthew_Gaddis Level 1
      Does the getElementById method work on coldfusion xml document objects? I am working with an xhtml file and would really like to use the getElementById method. I know this can be done with XmlSearch(doc, '//*[@id = ''foo'']') but getElementById I would assume is optimized to use the id index of a dom object. Perhaps I need to run XmlValidate first?

      Thank you
      Matt
        • 1. Re: xml object getElementById
          Daverms Level 3
          Hi,

          I suspect that your XHTML has some Namespaces defined, ignoring the uncessary definitions there may resolve your issue...
          • 2. Re: xml object getElementById
            Matthew_Gaddis Level 1
            It's doubtful that is the case. Here is a quick test series I ran in which all the values failed:
            <cfxml variable="xml_one">
            <root>
            <node id="node1">one</node>
            </root>
            </cfxml>

            <cfxml variable="xml_two">
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            " http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html xmlns=" http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
            <head>
            <title>Test Doc</title>
            </head>
            <body>
            <div id="node1">one</div>
            </body>
            </html>
            </cfxml>

            <h1>Doc w/o a Namespace</h1>
            <cfset e = xml_one.getElementById('node1')>
            <cfset WriteOutput('xml one: ' & isDefined('e'))>

            <h1>Doc w/ a Namespace, pre Validation</h1>
            <cfset e = xml_two.getElementById('node1')>
            <cfset WriteOutput('xml two: ' & isDefined('e'))>

            <h1>Doc w/ a Namespace, post Validation</h1>
            <cfset XmlValidate(xml_two, " http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd")>
            <cfset e = xml_two.getElementById('node1')>
            <cfset WriteOutput('xml two: ' & isDefined('e'))>
            • 3. Re: xml object getElementById
              BKBK Adobe Community Professional & MVP
              Does the getElementById method work on coldfusion xml document objects?
              No. Coldfusion xml is server-side; getElementById is client-side DOM.

              • 4. Re: xml object getElementById
                Matthew_Gaddis Level 1
                That too seems to be an unsatisfactory explanation. DOM is an XML API regardless of Server-side or client-side Xml. Almost all other DOM methods are implemented including parentNode, nextSibling, previousSibling, getElementsByTagName...
                • 5. Re: xml object getElementById
                  BKBK Adobe Community Professional & MVP
                  Coldfusion XML applies only a subset of the XML language. The function getElementById() is client-side DOM.


                  • 6. Re: xml object getElementById
                    Matthew_Gaddis Level 1
                    BKBK,
                    Do you mean to say that getElementById is the only DOM function not implemented in coldfusion's XML?

                    Just to prove my point, you can append the following commands to be above script:
                    <cfdump var=#xml_two.html.getFirstChild().getNextSibling()#>
                    <cfdump var=#xml_two.getElementsByTagName('div')#>
                    • 7. Re: xml object getElementById
                      Matthew_Gaddis Level 1
                      The java 1.4.2 documentation of org.w3c.dom.document.getElementById reads:

                      "Returns the Element whose ID is given by elementId. If no such element exists, returns null. Behavior is not defined if more than one element has this ID. The DOM implementation must have information that says which attributes are of type ID. Attributes with the name "ID" are not of type ID unless so defined. Implementations that do not know whether attributes are of type ID or not are expected to return null."

                      So the question really seems to be, how do you specify the ID attributes of a DOM document?
                      • 8. Re: xml object getElementById
                        Matthew_Gaddis Level 1
                        Here is one way to do it:

                        <cfset xml_two.html.body.div.setIdAttribute(Javacast('string',"id"), JavaCast('boolean', true))>


                        <cfset e = xml_two.getElementById('node1')>
                        <cfdump var=#e#>

                        This seems like a starting point at least...
                        • 9. Re: xml object getElementById
                          Level 7
                          As you touched on, an attribute is not an ID simply because it's call "id".

                          I read that here:
                          http://xerces.apache.org/xerces-j/apiDocs/org/w3c/dom/Document.html#getElementById(java.la ng.String)

                          This works:

                          <cfxml variable="x">
                          <aaa id="a">
                          <bbb id="b">
                          <ccc notid="c">Hello World</ccc>
                          </bbb>
                          </aaa>
                          </cfxml>
                          <cfset x.aaa.bbb.ccc.setIdAttribute("notid", true)>

                          <cfset s = x.getElementById("c").getTextContent()>
                          <cfoutput>#s#</cfoutput>

                          Cheers for raising this... I didn't know about any of this until you
                          prompted me to do some reading! :-)

                          --
                          Adam
                          • 10. xml object getElementById
                            BKBK Adobe Community Professional & MVP
                            BKBK,
                            Do you mean to say that getElementById is the only DOM function not implemented in coldfusion's XML?

                            No. Just that the functionality you seek,

                            <div id="node1">one</div>
                            <cfset e = xml_two.getElementById('node1')>

                            is client-side DOM. You yourself have discovered the difference in the client-side and server-side implementation, namely, that attributes named "ID" are not necessarily DOM IDs and the DOM implementation must know which attributes are of type ID.

                            However, in implying that the function cannot occur on server-side DOM, I stand corrected. It obviously can. In fact, I adapted your example to show how it would work, but Adam has beaten me to it. His example contains the essence.

                            Like Adam, I, too, was motivated by your question to do some reading. I wondered whatever happened to Coldfusion's adoption of DOM Levels 1 and 2 long ago.

                            To attempt another answer to your implementation question, yes, it's all there. Since ColdFusion conforms to the DOM Level 2, Coldfusion XML can implement practically all the functionality of Level-2-DOM. I should also like to thank you for bringing this up. I'm sure much will come from it.






                            • 11. xml object getElementById
                              BKBK Adobe Community Professional & MVP
                              The java 1.4.2 documentation of org.w3c.dom.document.getElementById reads:

                              "Returns the Element whose ID is given by elementId. If no such element exists,
                              returns null. Behavior is not defined if more than one element has this ID. The
                              DOM implementation must have information that says which attributes are of
                              type ID. Attributes with the name "ID" are not of type ID unless so defined.
                              Implementations that do not know whether attributes are of type ID or not are
                              expected to return null."

                              So the question really seems to be, how do you specify the ID attributes of a DOM document?


                              That begins to seem like a real dilemma. At least, for Java 1.4.2 and Coldfusion MX7, which implement DOM Level 2. While the DOM implementation of getElementById requires information about the ID, DOM-2 does not seem to offer a way to make an attribute an element's ID. If DOM-2 has no way of setting an attribute as an ID, then it will be questionable to use getElementById in MX7's XML.

                              The functions setIdAttribute() and getTextContent() that Adam uses are DOM Level 3. I also used setIdAttribute() in my test. But, in place of getTextContent(), I used the Level 2 equivalent getChildNodes().item(0).getNodeValue(). I now realize that my Coldfusion is running on Java 1.5, which supports DOM Level 3. I therefore wondered. If you are on Java 1.4.2, what we suggest here might not work. In any case, it requires further investigation.



                              • 12. Re: xml object getElementById
                                Matthew_Gaddis Level 1
                                Actually I beat Adam to the solution by about 2 hours :).

                                I do not think it is limited to JVM 1.5 because I ran my own tests of setIdAttribute() on my Mac which uses Java 1.4.2.
                                • 13. Re: xml object getElementById
                                  Level 7
                                  > That begins to seem like a real dilemma. At least, for Java 1.4.2 and
                                  > Coldfusion MX7, which implement DOM Level 2. While the DOM implementation of
                                  > getElementById requires information about the ID, DOM-2 does not seem to offer
                                  > a way to make an attribute an element's ID. If DOM-2 has no way of setting an

                                  It - the XML processor that CFMX uses - provides setIdAttribute(), as per
                                  my sample code.

                                  My code runs fine on CFMX7.0.2, with the out-of-the-box JVM.

                                  --
                                  Adam
                                  • 14. Re: xml object getElementById
                                    Level 7
                                    > Actually I beat Adam to the solution by about 2 hours :).

                                    Heh heh. So why did you not save us some time and post the solution then,
                                    eh?

                                    It took me about 30min from first reading your message to having my answer.
                                    Including the time to brush up on a topic I previously had no idea even
                                    existed, let alone any experience with it.

                                    Neener neener neener.

                                    ;-)

                                    --
                                    Adam
                                    • 15. Re: xml object getElementById
                                      BKBK Adobe Community Professional & MVP
                                      Matthew Gaddis said:
                                      > I do not think it is limited to JVM 1.5 because I ran my own tests of
                                      > setIdAttribute() on my Mac which uses Java 1.4.2.


                                      Adam Cameron said:
                                      > It - the XML processor that CFMX uses - provides setIdAttribute(), as per
                                      > my sample code.
                                      >My code runs fine on CFMX7.0.2, with the out-of-the-box JVM.


                                      OK, Java 1.5 is out of consideration. It therefore implies that Coldfusion MX7.0.2 implements some DOM Level 3. Nice, as DOM Level 3 is a major revision and extension of Level 2. Hmm, onwards to find how Level 3 got into Coldfusion.



                                      • 16. Re: xml object getElementById
                                        Matthew_Gaddis Level 1
                                        I dont know if you all are savvy with the upcoming release of cfmx but I was thinking it only seems logical, if not now hopefully in the future, that an xml document would have its id attributes set by the XmlValidate action since both DTDs and Xml Schemas can establish id attributes and nodes.

                                        Adam:
                                        Hey I did, the posting has the timestamp of 04/20/2007 07:26:04 PM. :)
                                        • 17. Re: xml object getElementById
                                          Level 7
                                          > Adam:
                                          > Hey I did, the posting has the timestamp of 04/20/2007 07:26:04 PM. :)

                                          Yeah yeah, that's the post I replied to. Sorry, you didn't seem so
                                          confident that that was the solution. My post merely reinforced that. I
                                          thought. Maybe not.

                                          --
                                          Adam
                                          • 18. xml object getElementById
                                            BKBK Adobe Community Professional & MVP
                                            I was thinking it only seems logical, if not now hopefully in the future, that an xml document would have its id attributes set by the XmlValidate action since both DTDs and Xml Schemas can establish id attributes and nodes.
                                            I also think Coldfusion's XML implementation needs a way of setting the element ID. The CF team could just make it the (Coldfusion) function, say, XmlSetIdAttribute(). In fact, they could simply just raise Coldfusion to DOM level 3.

                                            • 19. Re: xml object getElementById
                                              Level 7
                                              Can I recommend you raise a feature request for both of those?

                                              --
                                              Adam