Using Metadata to create information services So far this semester we have explored how metadata and structured information is used as a core component of digital documents (e.g. HTML) and how representations of print and digital documents can be described using multiple metadata schemas (e.g. MARC, Dublin Core and Qualified Dublin Core) and encoded using multiple encoding platforms (e.g. HTML, XML and MARC). As our last task in exploring the creation and representation of digital documents we are going to learn about the role of metadata and structured information in creating information services. For our purposes an information service is defined as “an operation or task that fills a specific information need.” We experimented with information services earlier in the semester when we used Cascading Style Sheets (CSS) to create custom views of an HTML document. We have also seen how a suite of information services can be designed together to help librarians accomplish the cataloging tasks (e.g. MARCEdit). In this exercise we are going to explore a technology called eXtensible Stylesheet Language (XSL). XSL is similar to the other metadata schemas that we have explored (HTML, DC, MARC) in that it has a defined set of elements and attributes. It is also similar to HTML in that it is designed to be processed by a specialized program. While HTML documents are designed to be processed by web-browsers, XSL documents are designed to be processed by an XSL processing engine. HTML Browsers have pretty good XSL processors in them and we will see that in action but there are also specialized applications such as Exchanger that allow you to apply an XSL style sheet to an XML file. In this set of exercises, we will create XSL programs (also called XSL templates) and become familiar with how to apply them to XML files. In our first exercise, we will create a common ‘first program’ for many people, the hello world program. In our second and third exercises, we will use an RSS/DC xml file and see how XSL interacts with that file as we create HTML documents.. Our development platform will be Exchanger. We will use a web-browser as our transformation engine. Our goal for this exercise is to be familiar enough with the transformation process to complete exercise 2 Exercise 1 – XML & XSL In this exercise, we will create an XML file that contains the words “hello world” and use XSL to retrieve data from that file. Step 1: Create a new XML file in your XML editor. This file follows a simple made up standard using root as a main element and message as : 1. <?xml version="1.0" encoding="UTF-8"?> 2. <root> 3. 4. <message>Hello World</message> </root> a. Save the XML file, giving it a name of your choice In the above XML file take a moment to identify the core elements: The root element of the document is: The value of the element <message> is: Recall for a moment the work we did with the Document Object Model (DOM). The DOM defines a method that we can use to access elements and attributes in our document and help us work with documents to either control display, manipulate content or, in this case, run a program that turns that content into a new document. XSL traverses the DOM using a method called XPath. XPath is a querying language similar in many ways to structured query language (SQL) used in relational database management systems. Just like SQL, XPath allows you to get specific data from your XML document. XPath statements are pretty easy to create: XPath statement to get the value of the <message> element in our document 5. /root/message Pretty underwhelming right? XPath in essence looks a lot like a hierarchical filesystem structure because it basically is a hierarchical structure. XPath supports absolute references (e.g. /root/message) and contextual references (e.g. ./message). A contextual reference is usually used within the context of another element (more about this later!). In order to get the value of our <message> element we use an XSL element called <value-of> 6. <xsl:value-of select=”/root/message”/> Key Questions Take a moment to examine the code here and answer the following questions What is the namespace of the value-of element? What attribute does that element have? What is the value of the attribute of your element? What is the meaning of the last “/” in the <xsl:value-of…> element? Incredibly, when you apply an XSL stylesheet to an XML document it pulls the data from the XML document and displays it according to the instructions in your XSL resource. Step 2: Now that we have a basic understanding of how XSL and XPath work Let’s create an XSL file that we will use to transform our XML file into an HTML file. a. Create an XSL file in your XML editor: b. Click on New >> select XML file. If prompted, choose “1.0” Notice the declaration of the XSL namespace in the stylesheet element. This is just like our work with Dublin Core in XML. You have to define the namespaces that you are going to use in your document. The powerful aspect here is that defining a namespace brings in a suite of functions that we will use to process our XML document! Notice also the use of HTML elements in the program. XSL documents are primarily designed for transformation and output – they access values in a source XML document and echo those values along with formatting to the screen or to a new document. Key Questions Take a moment to look at the program below and answer the following questions: 7. 8. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> 9. <html> 10. <body> 11. <h1> 12. <xsl:value-of select="/root/message"/> 13. </h1> 14. </body> 15. </html> 16. </xsl:template> 17. </xsl:stylesheet> Look at the element <xsl:template> Notice that this element has an attribute (match) with the XPath value (/). Assume that we called this stylesheet from our other XML document. Where in this source XML document does this XPath expression match? Consider what you have learned so far about XSL. If we used this XSL file to transform our XML document what type of document would be output? Step 3: Type the program into Exchanger and save it. Make sure your XSL stylesheet is well formed and validates. Step 4: Lets transform our XML document using our XSL stylesheet. Assign your XSL file to your XML file a. Put the following line of code in the top of your XML document right under the XML element declaration and before your <root> element. 18. <?xml-stylesheet type="text/xsl" href="[XSLFILENAME.xsl]"?> This command links our XSL stylesheet to our XML document. The two key attributes here are type (defines the type of document) and href (defines the location of the document) Step 5: From within a web-browser, Open your XML file. What do you see? Key Questions Consider the following schematic representing a webpage built with XML, XSL and CSS. What would some advantages and disadvantages of this approach be as compared to the single document approach that we worked with earlier in the semester?” Advantages Disadvantages Exercise 2 – Working with multiple records in an XML file In our first exercise we found that XSL can transform an XML document into an HTML document. XSL also includes a set of operations that allow us to work with multiple records in a single file. In fact XSL has so many functions that it ca be used to create a dynamic web service! In this exercise, we will get started with this by working with an RSS file of Dublin core records coded in XML. Step 6: During this exercise, we will use XSL to create a list of the titles in the XML file coded in HTML. a. Download the XML file from our course website (http://erikmitchell.info/lbsc_670/class_07_xsl0) for this exercise (Newbooks.xml) b. In this exercise, we want to loop through each title in our RSS file and output the title with a link to the record in the catalog. First, let’s look at the RSS file. Open the newbooks.xml file and answer the following questions: Key Questions What is an absolute XPath expression to the <item> element? What is an absolute XPath expression to the <title> element of the RSS feed? What are the relative (e.g. from <item>) XPath expressions for the book title and link? Book XPath: Link XPath: Before we move too far along with this process we need to understand one more XSL element <xsl:for-each>: This element creates a looping structure that repeats for every instance of an element. So in our XML file we have multiple <item> elements. This means that we will use an <xsl:for-each> loop to get to every <item> element. With this information in hand (XPath expressions for <title>, <item> and relative expressions for book title and link) and our understanding of <xsl:for-each> lets examine our XSL program: 19. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms"> 20. <xsl:template match="/"> 21. <html> 22. <body> 23. <h1> 24. <xsl:value-of select="/rss/channel/title"/> 25. </h1> 26. <ul> 27. <xsl:for-each select="/rss/channel/item"> 28. <li> 29. <xsl:value-of select="./dc:title"/> 30. </li> 31. </xsl:for-each> 32. </ul> 33. </body> 34. </html> 35. </xsl:template> 36. </xsl:stylesheet> Key Questions Review the code above and answer the following questions. Lets pay attention to a specific section of code (look below). This code executes the <xsl:for-each> process which loops through each instance of the <item> element in our RSS file. 1. <xsl:for-each select="/rss/channel/item"> 2. <li> 3. <xsl:value-of select="./dc:title"/> 4. </li> 5. </xsl:for-each> Note how within the <xsl:for-each> element we define a generic process (e.g. output an li element, output a link to a feed item, and close the li element). This will be repeated for every <item> element in the file. Take a moment to write out the HTML for the first instance of <item> in your XML file based on the instructions in this set of code Step 7: Let’s continue creating our XSL file. Create a new XSL file in your XML editor (see above instructions) Step 8: Code your XSL file to: a. Output the name of the feed in an <h1> element b. Go into each item element and output the title of the book c. Enter the code from our example 2 xsl file d. Save the file Step 9: Associate the XSL file with the newbooks.xml document (See previous instructions in this worksheet) Step 10: Load the newbooks.xml document in your webbrowser. What happened? Exercise 3 – Adding sorting and links to our list In this exercise, we will sort our results and add links to our list of titles. In order to do this, we will have to use the <xsl:attribute> element. The code below is substantially the same as our above example with the exception of the addition of the <a> element (see bolded code) A few notes on the new code: Notice how we use <xsl:attribute name=””/> to define an attribute for our <a> element We will use the <xsl:sort select=””/> element to have the file sort by title 1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms"> 2. <xsl:template match="/"> 3. <html> 4. <body> 5. <h1> 6. <xsl:value-of select="/rss/channel/title"/> 7. </h1> 8. <ul> 9. <xsl:for-each select="/rss/channel/item"> 10. <xsl:sort select="./dc:title"/> 11. <li> 12. <a><xsl:attribute name="href"><xsl:value-of select="./link"/></xsl:attribute><xsl:value-of select="./dc:title"/></a> 13. </li> 14. </xsl:for-each> 15. </ul> 16. </body> 17. </html> 18. </xsl:template> 19. </xsl:stylesheet> Step 11: Implement the changes to your XSL file. Save your XSL file and refresh newbooks.xml in your web-browser. Did you get links? Did the items sort properly? Wrap up This exercise has been our first foray into programming and software design. If you master the exercises in this worksheet you have the skills required to complete assignment 2. As the semester continues we will explore other forms of structured information. Each of these structures (e.g. Taxonomies, SOLR indexes) will introduce us to different aspects of the relationship between structured data and information services. There is a wide range of systems and programming platforms that we are not going to talk about this semester though. If you enjoyed your experience with XSL I would recommend continuing down that path and learning more about XSL systems and even a full-fledged programming language like Python, PHP or Perl.
© Copyright 2025 Paperzz