6.3 Copying Nodes from Two Documents

Using copy-of, you can piece together new documents by copying the nodes you want wholesale. So far, you have only seen how to copy nodes out of one document as, customarily, an XSLT processor handles only one input document or source tree. This example will show you how to process more than one input document with the XSLT function document( ). You caught a glimpse of document( ) in Chapter 4; Chapter 13 will cover document( ) in more detail.

In addition to eu.xml, you will also find other.xml in examples/ch06, which contains a list of non-EU states, shown in Example 6-3.

Example 6-3. An XML document listing non-EU states
<?xml version="1.0" encoding="UTF-8"?>     <!-- Other non-EU, European states  -->     <eu>  <other>   <state>Albania</state>   <state>Andorra</state>   <state>Belarus</state>   <state>Estonia</state>   <state>Bosnia-Herzegovina</state>   <state>Croatia</state>   <state>Iceland</state>   <state>Liechtenstein</state>   <state>Macedonia, Former Yugoslav Republic of</state>   <state>Moldova</state>   <state>Monaco</state>   <state>Norway</state>   <state>Russia</state>   <state>San Marino</state>   <state>Serbia and Montenegro</state>   <state>Switzerland</state>   <state>Ukraine</state>   <state>Vatican City</state>  </other> </eu>

Using the document( ) function, the stylesheet two.xsl takes nodes out of other.xml in addition to eu.xml:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/>     <xsl:template match="eu">  <xsl:copy>   <xsl:comment>    <xsl:text>Member states: </xsl:text>    <xsl:value-of select="count(member/state)"/>   </xsl:comment>   <xsl:copy-of select="member"/>   <xsl:comment>    <xsl:text>Candidate states: </xsl:text>    <xsl:value-of select="count(candidate/state)"/>   </xsl:comment>   <xsl:copy-of select="candidate"/>   <xsl:comment>    <xsl:text>Other states: </xsl:text>    <xsl:value-of select="count(document('other.xml')/eu/other/state)"/>   </xsl:comment>   <xsl:copy-of select="document('other.xml')/eu/other"/>  </xsl:copy> </xsl:template>     </xsl:stylesheet>

The nodes in the first source tree contained in the file eu.xml are fed to the XSLT processor via the command line. The document( ) function picks up the source in other.xml at the stylesheet level. This stylesheet also uses the count( ) function to count state elements, arriving at the number of countries in each category: member, candidate, and other. With this command:

xalan -i 1 eu.xml two.xsl

the processor will produce the following result, shown in Example 6-4.

Example 6-4. An XML document listing European countries and their relation to the EU
<?xml version="1.0" encoding="UTF-8"?> <eu>  <!--Member states: 15-->  <member>   <state>Austria</state>   <state founding="yes">Belgium</state>   <state>Denmark</state>   <state>Finland</state>   <state founding="yes">France</state>   <state founding="yes">Germany</state>   <state>Greece</state>   <state>Ireland</state>   <state founding="yes">Italy</state>   <state founding="yes">Luxembourg</state>   <state founding="yes">The Netherlands</state>   <state>Portugal</state>   <state>Spain</state>   <state>Sweden</state>   <state>United Kingdom</state>  </member>  <!--Candidate states: 13-->  <candidate>   <state>Bulgaria</state>   <state>Cyprus</state>   <state>Czech Republic</state>   <state>Estonia</state>   <state>Hungary</state>   <state>Latvia</state>   <state>Lithuania</state>   <state>Malta</state>   <state>Poland</state>   <state>Romania</state>   <state>Slovakia</state>   <state>Slovenia</state>   <state>Turkey</state>  </candidate>  <!--Other states: 18-->  <other>   <state>Albania</state>   <state>Andorra</state>   <state>Belarus</state>   <state>Estonia</state>   <state>Bosnia-Herzegovina</state>   <state>Croatia</state>   <state>Iceland</state>   <state>Liechtenstein</state>   <state>Macedonia, Former Yugoslav Republic of</state>   <state>Moldova</state>   <state>Monaco</state>   <state>Norway</state>   <state>Russia</state>   <state>San Marino</state>   <state>Serbia and Montenegro</state>   <state>Switzerland</state>   <state>Ukraine</state>   <state>Vatican City</state>  </other> </eu>


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

Similar book on Amazon

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