Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Using the XSLT function document() (See related posts)

I found an XSLT solution recently using the built-in function document() to include an additional XML document into the current XML document.

Using the file lunch.xml with drink.xml I can show what is available for today's lunch menu.

file: lunch.xml
   1  
   2  <food>
   3    <item name="banana"/>
   4    <item name="apple"/>
   5    <item name="custard"/>
   6    <item name="crisps"/>
   7  </food>  


file: drink.xml
   1  
   2  <drinks>
   3    <drink name="water"/>
   4    <drink name="orange"/>
   5    <drink name="lemonade"/>
   6    <drink name="cream soda"/>
   7    <drink name="tea"/>
   8    <drink name="coffee"/>
   9    <drink name="blackcurrant juice"/>
  10    <drink name="ginger ale with lime"/>
  11  </drinks>


file: lunch.xsl
   1  
   2  <?xml version="1.0" encoding="UTF-8"?>
   3  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   4  
   5  <xsl:template match="food">
   6  <html>
   7    <head><title>Food menu</title></head>
   8    <body>
   9    <h1>To eat</h1>
  10    <ul>
  11    <xsl:apply-templates select="item" />
  12    </ul>
  13    <h1>To drink</h1>
  14    <xsl:value-of select="document('drink.xml')/drinks/drink[2]/@name"/>
  15    </body>
  16  </html>
  17           
  18  </xsl:template>
  19  
  20  <xsl:template match="item">
  21    <li><xsl:value-of select="@name"/></li>
  22  </xsl:template>
  23  
  24  </xsl:stylesheet>
  25  


output:
   1  
   2  <html><body>
   3  <h1>To eat</h1>
   4  <ul>
   5  <li>banana</li>
   6  <li>apple</li>
   7  <li>custard</li>
   8  <li>crisps</li>
   9  </ul>
  10  <h1>To drink</h1>orange</body></html>

You need to create an account or log in to post comments to this site.


Click here to browse all 5545 code snippets

Related Posts