[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Transform XML to XML

Subject: RE: Transform XML to XML
From: JBryant@xxxxxxxxx
Date: Fri, 26 Aug 2005 09:36:29 -0500
xml to rss
> I'm still confused by ".", "*", "node()", etc.  I've read all
> the definitions, but somehow I can't grok them.  Any thoughts?

> -Mike

Hi, Mike,

It helps a lot to remember the idea of context. "." refers to the current 
context node. "*" refers to all the children of the current context node. 
So the basic idea is: first set the context, then select something 
relative to that context.

So, if I have a baby XML document like this:

<foods>
  <fruits>
    <apple>apple</apple>
    <banana>banana</banana>
    <cherry>cherry</cherry>
  </fruit>
  <vegetables>
    <asparagus>asparagus</asparagus>
    <brocolli>brocolli</brocolli>
    <cauliflower>cauliflower</cauliflower>
  </vegetables>
</foods>

Then I can set the context in a number of ways, as shown in the following 
stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <test_out>
      <xsl:apply-templates/>
    </test_out>
  </xsl:template>

  <xsl:template match="fruits">
    <!-- now the context is the fruits node -->
    <xsl:apply-templates/>
    <xsl:for-each select="/foods/vegetables">
      <!-- now the context is the vegetables node -->
      <xsl:apply-templates select="preceding-sibling::fruits/*"/>
      <!-- I'm going to process fruit nodes even though the context is 
vegetables -->
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="apple|banana|cherry">
    <!-- now the context is an individual fruit -->
    <fruit_out><xsl:value-of select="."/></fruit_out>
  </xsl:template>

  <xsl:template match="vegetables"/>

</xsl:stylesheet>

The result is:

<?xml version="1.0" encoding="UTF-8"?>
<test_out>
  <fruit_out>apple</fruit_out>
  <fruit_out>banana</fruit_out>
  <fruit_out>cherry</fruit_out>
  <fruit_out>apple</fruit_out>
  <fruit_out>banana</fruit_out>
  <fruit_out>cherry</fruit_out>
</test_out>

So thinking about context and about how you can get from the current 
context to the bit of data you want helps a big bunch.

(Hopefully, I didn't muddy the waters more than I clarified them for you.)

Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.