Subject: RE: [xsl] Transform XML to XML From: JBryant@xxxxxxxxx Date: Fri, 26 Aug 2005 09:36:29 -0500 |
> 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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Transform XML to XML, Wendell Piez | Thread | Re: [xsl] Transform XML to XML, David Carlisle |
RE: [xsl] RE: Yahoo shopping feed, , Michael Kay | Date | RE: [xsl] Transform XML to XML, Michael Kay |
Month |