11.4 Cross-Referencing with Keys

In the final example of this chapter, you'll cross-reference identical content in two separate documents. To do this, you'll use a parameter and the document( ) function. The following two XML documents share some identical content. The first, states.xml, lists the names of eight western states in the continental United States:

<?xml version="1.0" encoding="US-ASCII"?>     <usstates>  <western>   <usstate>Arizona</usstate>   <usstate>California</usstate>   <usstate>Idaho</usstate>   <usstate>Montana</usstate>   <usstate>Nevada</usstate>   <usstate>Oregon</usstate>   <usstate>Washington</usstate>   <usstate>Utah</usstate>  </western> </usstates>

The second document, capitals.xml, lists the capitals of these states:

<?xml version="1.0" encoding="US-ASCII"?>     <capitals>  <capital usstate="Arizona">Phoenix</capital>  <capital usstate="California">Sacramento</capital>  <capital usstate="Idaho">Boise</capital>  <capital usstate="Montana">Helena</capital>  <capital usstate="Nevada">Carson City</capital>  <capital usstate="Oregon">Salem</capital>  <capital usstate="Washington">Olympia</capital>  <capital usstate="Utah">Salt Lake City</capital> </capitals>

The value of the usstate attributes in this document is the same as the content of the usstate elements in states.xml. The stylesheet cross.xsl takes advantage of this relationship by bringing this information together in a result tree:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:key name="Capital" match="capitals/capital" use="@usstate"/> <xsl:key name="State" match="usstates/western/usstate" use="."/> <xsl:param name="cr">Arizona</xsl:param>     <xsl:template match="/">  <xsl:apply-templates select="document('capitals.xml')/capitals"/>  <xsl:text>, </xsl:text>  <xsl:value-of select="key('State', $cr)"/> </xsl:template>     <xsl:template match="capitals">  <xsl:value-of select="key('Capital', $cr)"/> </xsl:template>     </xsl:stylesheet>

The stylesheet declares two keys, Capital and State, and a parameter, cr. The first template matches the document root (/) of the main document it intended to process, states.xml. It also applies templates to the capitals node in capitals.xml using the document( ) function.

When templates are applied to the capitals node, the template that matches capitals is invoked, and the key( ) function is called for the key named Capital. The template returns the string value using the key Capital key. Control is then returned to the invoking template after this is accomplished.

After all this takes place, a comma and space are inserted into the result tree by a text element from the first template, and the key( ) function is called again for the key named State. Using Arizona as the default value of cr, the stylesheet, when applied to states.xml, gives you the following answer:

Phoenix, Arizona

Try it with other states, such as Oregon, like this:

xalan -p cr 'Oregon' states.xml cross.xsl

The result you will get is:

Salem, Oregon

Or, you can get the same output using Instant Saxon this way:

saxon states.xml cross.xsl cr="Oregon"

This example demonstrates how keys, combined with other functionality, can be a powerful tool in searching for, and establishing, a relationship between that content, as well as matching, verifying, and extracting content from one or more documents.



Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net