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)
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--