xsl-list
[Top] [All Lists]

Re: [xsl] [XSL 1.0] How to use apply-templates to process a sub-node

2011-02-07 15:54:20
At 2011-02-07 15:51 -0600, Keith Gilbert wrote:
This is a real basic question, I think, but I can't get my head around it. Given this XML:

<root>
        <story>
                <Head>This is the title</Head>
<Body>This is the body <Big>some of it is bold</Big> and some isn't</Body>
                <Body>This is some more body text.</Body>
        </story>
</root>

And this XSL:

<xsl:stylesheet>
    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates select="Root" />
            </body>
        </html>
    </xsl:template>

    <xsl:template match="Head">
        <h1><xsl:value-of select="."/></h1>
    </xsl:template>

    <xsl:template match="Body">
        <p><xsl:value-of select="."/></p>
    </xsl:template>

    <xsl:template match="Big">
        <bold><xsl:value-of select="."/></bold>
    </xsl:template>

</xsl:stylesheet>

I'm trying to convert the XML to HTML with XSL. I've got it all figured out and working, except for the <Big> text. I don't understand how to recursively get to the <Big> element when it is a sub-element of <Body>. All the examples I've read about in all my books expect the XML to be:

<Body>
        <Big>text here</Big>
</Body>

But this is a different structure, where the <Big> element is in the middle of the Body element.

Can anyone shed some light on how I get to the <Big> element with apply-templates?

Change *every* use of <xsl:value-of/> in your stylesheet to <xsl:apply-templates/> and it will work for you. And it will be protected from future changes to your XML vocabulary.

You are short-circuiting your stylesheet by using <xsl:value-of/>, thus not engaging the template rules you need. By changing your approach to pushing nodes instead of pulling nodes, you will keep your template rules in play.

I hope this helps.

. . . . . . . . . . . Ken

--
Contact us for world-wide XML consulting & instructor-led training
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
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>
--~--

<Prev in Thread] Current Thread [Next in Thread>