Subject: [xsl] strangeness with <xsl:variable> From: Mike Hostetler <mikeh@xxxxxxxxxxxx> Date: Wed, 20 Dec 2000 11:45:17 -0800 (PST) |
I'm sorry for the long post, but I'm not sure this is me, XSLT, or what, so I thought that I would give as much detail as to what I am trying to do, and hurdles that are popping up, as possible. I'm using Instant Saxon 5.5.1 and an XSLT to generate a HTML file that includes a table of contents and the actual content from the source XML. Obviously, this has two parts - in the content, I have to put in <a name="something"> for every part, chapter, and section marked in the XML. And then, when I generate the TOC, I need to put in <a href="#something"> so the user can clink on it to take them to the desired content. I chose Part numbers, Chapter numbers, and section numbers in the <a name=".."> , because that would be the easiest to generate. So, when you look at the raw HTML output, I want it look like: <a name="partI">Part I</a> <a name="chap1">Chapter 1</a> <a name="sect-I.1.1">Section 1 of Chapter 1</a> (the Roman to Arabic numbering system wasn't my idea, but I decided to keep this number system consistent to the numbering within the document) I did note that I couldn't do <a name={$<xsl:number/>}>, because "name" is an attribute. So, the logical reason would be to put this info into a variable, and do <a name="<$variablename>">. And that's when things got weird. 1. When doing this: <xslvariable name="name"> chap<xsl:number count="chapter"/> </xsl:variable> <a name="{$name}"> [....] I get control characters in the HTML, a la: <a name="%A0%20chap1"> These are the characters before "chap", so I did it this way: <xsl:variable name="name">chap<xsl:number format="1" level="any"/> </xsl:variable> <a name="{$name}"> And that works. This doesn't seem right, because shouldn't XSLT ignore the whitespace before the "chap"? Or is <xsl:variable> an exception to the rule? 2. (this one is even worse) The XML source has appendices, and an introduction as well as chapters, and the numbering of the sections within them need to be different . . . i.e. in the intro it should look like "sect-intro-1", in an appendix as "sect-IV.A.1". I thought the natural way to do this is to use a <xsl:variable> within a <xsl:choose>. So I did this . . . <xsl:template match="section" mode="TOC"> <xsl:choose> <xsl:when test="parent::introduction"> <xsl:variable name="link">sect-intro<xsl:number format="1" level="single" count="section"/> </xsl:variable> </xsl:when> <xsl:when test="parent::chapter"> <xsl:variable name="link">sect-<xsl:number format="I.1.1" level="multiple" count="//part | chapter |section"/> </xsl:variable> </xsl:when> <xsl:otherwise> <xsl:variable name="link">sect-<xsl:number format="I.A.1" level="multiple" count="//part | appendix|section"/> </xsl:variable> </xsl:otherwise> </xsl:choose> <a href="#{$link}"> But Saxon gives me this error: Failed to compile style sheet At a on line 133 of file:/H:/lit_xslt/fnr2lit.xsl: Variable link has not been declared So, the link variable needs to be declared on the outside of the <xsl:choose> loop. This seems like a nasty namespace issue to me .. . . but I did get it to work using this: <xsl:variable name="link"><xsl:choose> <xsl:when test="parent::introduction">sect-intro<xsl:number format="1" level="single" count="section"/> </xsl:when> <xsl:when test="parent::chapter">sect-<xsl:number format="I.1.1" level="multiple" count="//part | chapter |section"/> </xsl:when> <xsl:otherwise>sect-<xsl:number format="I.A.1" level="multiple" count="//part | appendix|section"/> </xsl:otherwise> </xsl:choose></xsl:variable> <a href="#{$link}"> Yes, the <xsl:variable><xsl:choose> statement is one long line, thanks to the whitespace weirdness talked about earlier. So, is this a problem with XSLT? Or is there a better/easier way to do this? My ears are open . . . Mike __________________________________________________ Do You Yahoo!? Yahoo! Shopping - Thousands of Stores. Millions of Products. http://shopping.yahoo.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Text Following <br/>, Dimitre Novatchev | Thread | Re: [xsl] strangeness with <xsl:var, David Carlisle |
[xsl] Text Following <br/>, Edierley Messias | Date | Re: [xsl] Text Following <br/>, Dimitre Novatchev |
Month |