The ABCs of XML, Part 2

This article provides more of what you need to know to survive in the world of connected data by introducing XML’s fundamental concepts to those who have managed to avoid this important technology.

Share Print Related RSS
Page 3 of 3 1 | 2 | 3 Next » View on one page

The namespace prefix cc serves as a shorthand or alias notation for the full namespace http://www.cascon.com/Recipe. The actual namespace may be any string value but it is meant to be globally unique. XML parsers don’t enforce uniqueness, nor do they expect any particular notation such as a web universal resource identifier (URI). A web style URI is frequently used because a real web URI like http://www.cascon.com/ is guaranteed to be globally unique across the Internet, which greatly minimizes the chance of colliding namespaces.

To simplify this example, the namespace can be declared as the default namespace with no prefix as shown in the following example:

<Recipe xmlns="http://www.cascon.com/Recipe">
   <Step XPos="600" YPos="600" AcquireUnit="yes">
   <Name>Feed:1</Name>
   <UnitAlias>Unit500</UnitAlias>
   </Step>
</Recipe>

Notice that the namespace attribute xmlns no longer includes the prefix definition cc. Without a prefix, a namespace becomes the default namespace for the element where it’s declared. This makes the Recipe element and all its child element members of the namespace http://www.cascon.com/Recipe. Default namespaces apply to elements only, not to attributes. Therefore, the attributes of the Step element are included in a null namespace (equivalent to xmlns=""), and not the default namespace. This special behavior for attributes can be quite confusing. This quirk of default namespaces isn’t difficult to work around as long as you understand how it works.

You'll likely not need to bother with namespaces in documents created for internal purposes. However, you'll need to understand namespaces when working with vendor-generated XML files. You will see the importance of namespaces in Part 3, which will cover XSLT.


A Sample of XML

THE SAMPLE fragment of XML software code (below) was lifted from a recipe exported from Rockwell Automation's RSBatch product. Rockwell defined the element and attribute names for describing an RSBatch recipe. Other system vendors may define a separate set of elements and attributes to describe a batch recipe. If you are a system integrator that works with batch recipe software from different system vendors, you may choose to define your own system-agnostic batch recipe XML data structure (or schema) for internal development purposes that can easily be converted to/from a vendor specific structure.

<!-- This is an XML comment -->
<Step XPos="600" YPos="600" AcquireUnit="true">
    <Name>FEED:1</Name>
    <StepRecipeID>FEED</StepRecipeID>
    <UnitAlias>UNIT500</UnitAlias>
    <FormulaValue>
       <Name>AGIT_SPEED_SP</Name>
       <Display>true</Display>
       <Value />
       <Real>75</Real>
       <EngineeringUnits>RPM</EngineeringUnits>
    </FormulaValue>
</Step>

The first thing to notice about the code fragment is the angle brackets (< and >) which mark an XML “tag.” An XML “element” includes a start tag like <Step>, an end tag like </Step>, and everything between the two.  Notice that the Step element contains four child elements Name, StepRecipeID, UnitAlias, and FormulaValue.  The FormulaValue element contains five child elements. This parent/child relationship between elements shows that XML can support hierarchical data structures.

XML is designed to be self-describing. A document's data is stored in element values and attribute values, so that element and attribute names describe the data they hold much like data is described in a relational database by table names and field or column names.

Data stored as an element value is the text between start and end tags. In this sample, the value for the element EngineeringUnits is RPM. Data stored as an attribute value is found on the quoted right side of a Name="Value" pair. In our example, the Step element contains three attributes named XPos, YPos, and AcquireUnit, which have attribute values of 600, 600, and true respectively.

Resources on XML

There are many reference books and online resources for learning more about XML. Recommended online resources include: 


  About the Author
John T. Sever is president and founder of Cascade Controls Inc. Sever has more than 20 years of experience as a process and automation engineer and can be reached at 708/802-6000 or at johnsever@cascon.com.

 

Page 3 of 3 1 | 2 | 3 Next » View on one page
Share Print Reprints Permissions

What are your comments?

Join the discussion today. Login Here.

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments