Subject: [xsl] Passing tokenized attribute as a variable From: Mat Bergman <matbergman@xxxxxxxxx> Date: Mon, 27 Jun 2005 09:07:40 -0700 (PDT) |
I'm generating a set of nested HTML unordered lists from an XML file with this format: <menudata> <menu name="link1" excludeSite="0"/> <menu name="link2" excludeSite="2" > <menu name="link2a" excludeSite="1,2"/> <menu name="link2b" excludeSite="0"/> </menu> </menudata> The "excludeSite" attribute determines if that particular node is displayed in the rendered HTML. For reasons that relate more to politics than well-formed XML, my client has formatted the XML so that the excludeSite attribute may hold multiple values, requiring me to tokenize that attribute to process each value individually. I have successfully worked with the EXSLT tokenize() template, and I have a stylesheet (with help from this outstanding mailing list) to generate my HTML unordered lists. However, I am having trouble passing the tokenized attribute from my tokenize() template to my list template. I think I must not fully understand how XSLT handles variables. This is a simplified version of what I thought would work: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes"/> <!-- siteID determines if a link is displayed for a specific cobrand --> <xsl:param name="siteID" select="2"/> <xsl:template name="newMenu" match="/menudata"> <xsl:call-template name="write-menu"> <xsl:with-param name="items" select="menu"/> </xsl:call-template> </xsl:template> <!-- Generate HTML unordered list --> <xsl:template name="write-menu"> <xsl:param name="items" select="/.."/> <ul> <xsl:for-each select="$items"> <!-- Get tokenized attribue -- why doesn't this work? --> <xsl:variable name="currentExsite"> <xsl:call-template name="tokenize"> <xsl:with-param name="string" select="@excludeSite"/> <xsl:with-param name="delimiters" select="','"/> </xsl:call-template> </xsl:variable> <!-- Write the list item only if the excludeSite attribute does not match the siteID --> <xsl:if test="$currentExsite!=$siteID"> <li> <xsl:value-of select="@name"/> <xsl:if test="menu"> <xsl:call-template name="write-menu"> <xsl:with-param name="items" select="menu"/> </xsl:call-template> </xsl:if> </li> </xsl:if> </xsl:for-each> </ul> </xsl:template> <!-- Tokenize template based on EXLT tokenize() template --> <xsl:template name="tokenize"> <!-- [paramaters excluded from this example for brevity] --> <xsl:call-template name="_tokenize-delimiters"> <!-- [paramaters excluded from this example for brevity] --> </xsl:call-template> </xsl:template> <xsl:template name="_tokenize-delimiters"> <!-- [paramaters excluded from this example for brevity] --> <xsl:choose> <xsl:when test="not($delimiter)"> <!-- This is where I am having trouble getting the tokenized value --> <xsl:variable name="currentExsite"><xsl:value-of select="$string"/></xsl:variable> </xsl:when> </xsl:choose> <!-- [The rest of this template is omitted for brevity] --> </xsl:template> </xsl:stylesheet> It's obvious even to me, a novice XSLT developer, that this stylesheet would be greatly simplified if I broke out the "excludeSites" as individual nodes rather than attributes. I may in fact ditch this approach and build it that way, in spite of my client's requirements. However, I'm also curious how variables could be properly used in this instance. Thanks, -Mat __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] [ANN] Saxon 6.5.4, G. Ken Holman | Thread | [xsl] writing a plugin to access xs, Rahil |
[xsl] [ANN] Saxon 6.5.4, Michael Kay | Date | [xsl] writing a plugin to access xs, Rahil |
Month |