Tag Archives: XML

Access nested attributes using SimpleXML

I have been working on writing a function to edit an XML document using just SimpleXML. This is a sample of the XML file I am working with today:

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
  <Employee id="a21">
    <First_Name>Levi</First_Name>
    <Last_Name>Jackson</Last_Name>
    <Position>Web Developer</Position>
    <Department>Programming</Department>
    <Status>Full-Time</Status>
    <Contact_Info>
      <Phone type="work">ext.123</Phone>
      <Phone type="cell">850-123-1029</Phone>
      <Email>Aerosox@gmail.com</Email>
    </Contact_Info>
    <Access_Info level="user">
      <Username>ljackson</Username>
      <Password>pass123</Password>
    </Access_Info>
    <Start_Date>2009-01-12</Start_Date>
    <Photo>./images/Unwin.jpg</Photo>
    <SSN>123-12-3121</SSN>
    <Salary>$21,000</Salary>
  </Employee>
</Employees>

Accessing the children of Employees was the easy part. Create the SimpleXML object and the nodes become properties of the object.

$simpleObj = simplexml_load_file($xml);

foreach($simpleObj as $emp){
  $emps = get_object_vars($emp);
  $fn = $emps->First_Name; //returns "Levi"
}

The trouble I had was in accessing the attributes of the child nodes. I was thinking about it in terms of the attributes being arrays, when in fact they were a child simpleXML object! After some reading about the SimpleXML object I came this solution for accessing the user type for my “employee”.

 $level = $emps["Access_Info"]->attributes()->level;

What this does is select the child node of Employee called “Access_Info”. From there the attributes() method is called which returns a new simpleXML object to work with. So what was once hard is now as easy as the first example. Once we have the SimpleXML object we can then access the values quite easily.

Conclusion

So what did I take away from this experience? Well for starters just because it looks complicated, doesn’t make it so. The biggest issue I had was that I was being returned SimpleXML objects and I wasn’t sure how to access the information. It became easier to work with once I could visually see that objects can nests inside one another. Just because the SimpleXML object was a child of another object didn’t mean I couldn’t use the same methods on either. Essentially what I learned was that when given a situation that I am unsure of, the best way to work through it is to assess my options.

elementFormDefault – what is it?

As I researched and wrote schema’s I realized I was writing

<schema 
	xmlns="http://www.w3.org/2001/XMLSchema"
	targetNamespace="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
>

What was wrong with that you might be asking? Well for starters I had no idea what the attribute elementFormDefault did. I had been writing it over and over without knowing what it did just that it worked, this was just asking for trouble. So I did some research and more research. It finally all came together and I figured I would share my findings for everyone to enjoy.

elementFormDefault is an attribute of the schema tag. It is used, in a way, with the targetNamespace attribute of that schema tag. The targetNamespace attribute does what it sounds like it does, it specifies the location of the namespace being used. The two values for elementFormDefault attribute, qualifed and unqualified. Depending on the value of elementFormDefault, the namespace will either be used for all of the elements in the XML document or it will not. If the form is qualified, all of the elements in the XML document use the targetNamespace. If the form is unqualified the elements belong to no namespace unless they are explicitly specified. You would do this inline by adding the attribute form=”qualified” to the element you would like to be included in the namespace. An example of this would be:

<element name="assignment" type="stringo" minOccurs="0" maxOccurs="unbounded" form="qualified">

So to conclude, there aren’t too many scenarios where you would want to set elementFormDefault=”unqualified”, just know it is out there.

“The only source of knowledge is experience”
-Albert Einstein

When to use attributes and when to use elements in XML

So I was wondering today if there is a ruling on when to use attributes in XML. It seems to me it is much easier to parse out data if there are no attributes, but on the other hand I don’t want to put data that isn’t pertinent to the information being held. So my first option was this:

<location>
	<id>11</id>
	<name>Downtown</name>
	<address>225 Main Street Nowheresville</address>
</location>

My second option was this:

<location id="11">
	<name>Downtown</name>
	<address>225 Main Street Nowheresville</address>
</location>

I did some research and found that everyone and their mother was saying, data goes in elements, metadata in attributes. So I did some more research just to make sure I was understanding it completely. I found these two tips to be clearer.

  1. If the information is pertainant to the whole information as a whole, then it is best to make it an element.
  2. Conversely, if the information is used as a reference for other aspects of an application or web site, it is best to set it as an attribute.

Quite possibly the best tips I found were from the X12 Reference Model For XML Design.

  1. “Attributes are atomic and cannot be extended and its existence should serve to remove any and all possible ambiguity of the element it describes. They are ‘adjectives’ to the element ‘noun’.”
  2. “Use elements for data that will be produced or consumed by a business application, and attributes for metadata.”

Further reading: Principles of XML design: When to use elements versus attributes
Elements vs. attributes