It’s been two years since our office began our long, strange trip down content management highway. There have been many twists and turns along this road but quite possibly the biggest speed bump we’ve come across has been the application of a new coding language: XSLT. The idea of learning an entirely new language was daunting to be sure, and I’d heard from many seasoned programmers that mastering XSLT was not easy.
I’m composing this ongoing series of blog entries because of the lack of quality XSLT content out there on the interwebs. While our office was learning about XSLT we had very little in the way of helpful tutorial resources. I’ll list the ones we found helpful at the bottom of this entry and if you have any questions or suggestions for future entries, please feel free to comment. This entry will serve as an introduction of sorts to XML and any subsequent post will involve “real world” examples of XSLT.
First off XSLT works differently than the aforementioned languages. In short, XSLT takes an existing XML file (or files) and then “transforms” that XML into another form of XML like XHTML or even more XML. Why would you need to do this? One reason is that XML is a great, semantic way to keep data. Not only are you storing data, but you are storing that data in a very wide open language that you create yourself and expand upon as you need - hence the e(X)tensible piece of the name XML or XSLT.
Here’s some example XML:
<?xml version="1.0" encoding="UTF-8"?>
<system-index-block>
<calling-page>
<system-page>
<name>news-release</name>
<title>This is a headline</title>
<summary>This is where the release’s summary text would go.</summary>
<path>/University Affairs Sites/page</path>
<system-data-structure>
<story-content>
<p>Here is some page content.</p>
</story-content>
</system-data-structure>
</system-page>
</calling-page>
</system-index-block>
This example is directly from our content management system (CMS), CascadeServer. The system houses content as XML, which allows for a lot of data reuse. All the XML/XSLT I use for this series will be taken straight from our CMS. Above you see a typical set of XML. If you have any experience with a form of HTML then this should look familiar. Let’s start with some basic vocabulary that you’ll need to know before you start in with XSLT.
XML content
- declaration - This is the weird part at the top that tells whatever program that’s opening the file that it’s dealing with a version 1.0 XML file with UTF-8 encoding.
- root node - every XML file is made up of what is called elements or nodes; they’re the same thing. The “root” node of the above document is the
<system-index-block>piece of code.
This raises the important concept of hierarchy within the XML. Any form of XML is a nice little happy family, and so family terminology fits it splendidly. These terms directly relate to the way you “travel” through the XML in XSLT, using XPath, so pay particular attention to these concepts.
Hierarchy
- child - a node’s child node is the direct descendant to the node in question. In the document above the child node to the root node is the
<calling-page>element. - descendant - this refers to any children, grandchildren, etc. of a node. So a conceivable candidate for a descendant to the root node above would be
<name>,<title>or<path>. - sibling - a sibling of a node is pretty simple. Any node that is on the same “level” as the contextual node is a sibling. The three listed in the previous example are all siblings.
- parent - just like a node can have a child, it can also have a parent. The parent node of
<path>above is<system-page>. - ancestor - This refers to the exact opposite to the descendant. Any node that is directly above the contextual node is considered an ancestor. So above, an ancestor of
<system-page>would be<system-index-block>.
That’s enough for now. The next entry will deal with the first concepts of XSLT you’ll need to know. Until next time, here are some great XML/XSLT resources to take a look at. See you in a couple days!
Resources
- The W3C’s XML in Ten Points article - quick explanation of what XML is. Also, here is Digital Web’s introduction to XML.
- Oxygen XML Editor - This editor is fantastic and it only costs a little bit of money. It is specifically made for XML/XSLT development and has a great debugger view to use.
- W3Schools - I use this site A LOT when I’m developing. Its list of useful elements and functions is invaluable.
Leave a comment
Note: Comments are moderated. If published, comments may be edited for length, style and clarity.