Senin, 27 November 2017

Java XML DOM: How Are Id Attributes Special?

Java XML DOM: How Are Id Attributes Special?

These attributes are special because of their type and not because of their name. This is easy to appreciate when there is an XML Schema involved, since XML Schema supports datatypes for both XML elements and XML attributes. The XML attributes are defined to be of a simple type (e.g. xs:string, xs:integer, xs:dateTime, xs:anyURI). The attributes being discussed here are defined with the xs:ID built-in datatype (see section 3.3.8 of the XML Schema Part 2: Datatypes). With either the above XML Schema or DTD, the following element will be identified by the ID value of "xyz". Attributes with names that are not "id" might have an attribute type of ID! To improve this situation, the xml:id was subsequently invented (see xml:id W3C Recommendation). This is an attribute that always has the same prefix and name, and is intended to be treated as an attribute with attribute type of ID. However, whether it does will depend on the parser being used is aware of xml:id or not.

Since many parsers were initially written before xml:id was defined, it might not be supported. In Java, getElementById() finds elements by looking for attributes of type ID, not for attributes with the name of "id". In the above example, getElementById("xyz") will return that foo element, even though the name of the attribute on it is not "id" (assuming the DOM knows that bar has an attribute type of ID). So how does the DOM know what attribute type an attribute has? Explicitly indicate to the DOM that it is treated as an attribute type of ID. The third option is done using the setIdAttribute() or setIdAttributeNS() or setIdAttributeNode() methods on the org.w3c.dom.Element class. This has to be done for each element node that has one of these type of attributes on them. There is no simple built-in method to make all occurrences of attributes with a given name (e.g. "id") be of attribute type ID.

This third approach is only useful in situations where the code calling the getElementById() is separate from that creating the DOM. If it was the same code, it already has found the element to set the ID attribute so it is unlikely to need to call getElementById(). Also, be aware that those methods were not in the original DOM specification. The XPath in the original question gave a result because it was only matching the attribute name. If that had been used, the XPath would have given the same result as getElementById() (i.e. no match found). Two important features of ID should be highlighted. Firstly, the values of all attributes of attribute type ID must be unique to the whole XML document. In the following example, if personId and companyId both have attribute type of ID, it would be an error to add another company with companyId of id24601, because it will be a duplicate of an existing ID value. Even though the attribute names are different, it is the attribute type that matters. Secondly, the attributes are defined on elements rather than the entire XML document. So attributes with the same attribute name on different elements might have different attribute type properties. In the following example XML document, if only alpha/@bar has an attribute type of ID (and no other attribute was), getElementById("xyz") will return an element, but getElementById("abc") will not (since beta/@bar is not of attribute type ID). Also, it is not an error for the attribute gamma/@bar to have the same value as alpha/@bar, that value is not considered in the uniqueness of IDs in the XML document because it is is not of attribute type ID.







How do I get started with programming? Probably the most important and most often asked question. I spent hours searching for tutorials and guides on where to start. After a while, a learning plan started to take shape. The plan consisted of not just the things I needed to learn to make a simple Android app, but also the fundamentals of programming that would help me in other areas as well (especially web development). The plan was simple. Learn programming principles and its fundamentals. Learn about clean code and working with others. Then learn how Android works and put it all together in a simple app. The plan was set and off I went. I spent the first two months on this Stanford video on introduction to programming. It focuses on Java which was perfect as Android uses the Java programming language (killed two birds with one stone). I also grabbed a couple of Java books and did all the lessons in them, and I even watched all of the Clean code videos (even those that I didn’t understand at the time).

Tidak ada komentar:

Posting Komentar

Adbox