Subject: Re: [xsl] "apply-templates" doesn't work for novice From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx> Date: Thu, 27 Sep 2001 19:39:11 -0400 |
I am a novice with XSL. Here is a simplified version of the XML document i am trying to transform.
<Notebook> <List> <Cell> <CellGroupData> <List> <Cell> <String>This is a Title</String> <Style> <String>Title</String> </Style> </Cell> </List> </CellGroupData> </Cell> <List> <Options> <Option> <Symbol>FrontEndVersion</Symbol> <String>4.1 for Microsoft Windows</String> </Option> </Options> </Notebook>
When I use a narrowing down approach for the above document like so:
<xsl:template match="Notebook"> <xsl:apply-templates select="List/*"/> </xsl:template>
<!--Cell Group Level--> <xsl:template match="CellGroupData"> <xsl:apply-templates select="List"/> </xsl:template>
<xsl:template match="Cell/String"> <xsl:value-of select="String"/> </xsl:template>
followed by my VALUE-OF style-specific template:
"<xsl:template match="Cell/Style[String='Title']"> @Title:<xsl:value-of select="../String"/></xsl:template>"
I get exactly the result on the text output document that I want and expect. It looks like this: @Title:This is a title.
The problem is that my XML document is MUCH more complicated than the one shown above, so the "value-of" approach ends up being incredibly buggy and tedious because I have to get every instance listed: "data/list/data/cell/group/testdata/style/string" for every single style (of which there are hundreds). Also text with inline styles is weirdly situated and it is difficult to catch this way as well.
I would very much like to use an "apply-templates" approach to avoid this; but when i change the above style-specific template to:
"<xsl:template match="Cell/Style[String='Title']"> @Title:<xsl:apply-templates select="../String"/></xsl:template>
I get no text at all.
If I change one of the narrowing templates like the third one listed above(match="Cell/String) so that the value-of select ="." I get duplicates of all the text:
This is a title.@Title:This is a title.
Is there no happy medium between none or double using the apply-templates approach?
<Cell> <String>This is a Title</String> <Style> <String>Title</String> </Style> </Cell>
<xsl:template match="Cell"> <xsl:apply-templates select="Style"/> <xsl:apply-templates select="String"/> <!-- selecting the children to change the default order (which would be document order) --> </xsl:template>
<xsl:template match="Style"> <!-- using this template to generate your "tag" prefix --> <xsl:text>@</xsl:text> <xsl:apply-templates/> <!-- you could use value-of select=".", but why not let the processor do it for you when it gets down to the text node --> <xsl:text>:</xsl:text> </xsl:template>
I feel that there is something fundamental that i am missing here that is not allowing me to use the apply-templates approach. Can it be done, and if so how? An example of code WITH AN EXPLANATION of why it works would be VERY much appreciated.
Enjoy, Wendell
====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] "apply-templates" doesn't wor, Greg Martel | Thread | [xsl] ancestor question, Amy Kaizerman |
[xsl] ancestor question, Amy Kaizerman | Date | [xsl] Re: Organizing list of items, Yang |
Month |