Search

I’m pretty new to Symphony and XML and XPath and XSLT etc. etc.

I’m working on a page that displays four different categories of items and I want to be able to highlight the currently selected category.

<xsl:variable name="page" select="'data/manufacturers/entry/type'"/>
<xsl:choose>
    <xsl:when test="$page = 'firearms'">
        <a href="manufacturersfirearms" class="current">Firearms</a>
    </xsl:when>
    <xsl:otherwise>
        <a href="manufacturersfirearms">Firearms</a>
    </xsl:otherwise> 
</xsl:choose>

If I set the test to ‘1=1’ it works. if I set the variable to ‘firearms’ it works. If I do a value-of select=“‘data/manufacturers/entry/type’” it returns firearms. So why doesn’t this work?

P.S. I want to send a quick thanks to everyone who has already helped me so much as I’ve searched for answers to my many questions already. You guys rock!

Fixed code. Use 4 spaces to indent code and retain formatting. - Lewis

I assume you have a field called type. Does the manufactures data source grab more than one entry?

Post or pastie your XML to help us get a better context.

I guess your problem is here:

<xsl:variable name="page" select="'data/manufacturers/entry/type'"/>

By using single quotation marks you set the variable value to the string value, which is data/manufacturers/entry/type!

Try this:

<xsl:variable name="page" select="data/manufacturers/entry/type"/>

Nice catch Michael!

Thanks, Lewis! I hope it will work now…

Thanks for the responses, guys.

Yes, there are multiple entries under manufacturers. Could multiple entries screw up how the test interprets it? I can’t figure out how to post xml right now because my devkit has gone bonkers and ?debug isn’t working… sigh

You were right, michael-e. I ran that as a value-of to check and it did return the wrong string. So I’ve updated to your correction and now value-of is returning the correct string “firearms”. But it gets worse.

So now I’ve sprinkled some troubleshooting statements throughout the code check if I’ve been making some bad assumptions. I found something interesting:

<xsl:value-of select="data/manufacturers/entry/type"/> <!--returns 'firearms' as expected-->
<xsl:variable name="page" select="data/manufacturers/entry/type"/>
<xsl:value-of select="$page"/> <!--returns 'firearms' as expected-->
<xsl:if test="$page='firearms'">
    Working Correctly <!--failed to print-->
</xsl:if>   

<xsl:choose>
    <xsl:when test="$page = 'firearms'">
        <xsl:value-of select="$page"/><!--returns nothing-->
        <a href="manufacturersfirearms" class="current">Firearms</a>
    </xsl:when>
    <xsl:otherwise>
        <a href="manufacturersfirearms">Firearms</a>
    </xsl:otherwise>
</xsl:choose>

So there it is. Whatever data I’m pulling from data/manufacturers/entry/type is not a string - value-of can interpret it and return a string, but it’s not a string. I think… So now I know what the problem is, but I don’t know the solution. Would there be some way I could force it, through a function or something to turn the return from data/manufacturers/entry/type into a string?

It’s a little difficult to tell the issue without seeing the whole picture. It’d be great if we can see the XML and XSLT as a whole. The reason is that depending on your XML, you might be selecting a node-set or a piece of text.

To make sure you are indeed selecting a piece of text, you’ll need to be more specific in your XPath:

data/manufacturers/entry[1]/type

The above will grab the first entry’s type.

If you’re selecting a sequence of entries and displaying its type:

<xsl:for-each select="data/manufacturers/entry">
    <xsl:value-of select="type"/>
</xsl:for-each>

Both of the above will result in a string type.

Alternatively, you can cast your value as a string by:

<xsl:value-of select="string($page)"/>

However, it’s rare that you need to do any type casting in XSLT so it’s likely that a couple of changes in your XSLT will fix your problem without resorting to using it.

Thanks, Allen.

Lewis, you were right to point me at the XML. I got ?debug to work again and I found:

    <manufacturers>
        <section id="11" handle="manufacturer-item">manufacturer item</section>
        <entry id="11">
            <name handle="american-western-arms">American Western Arms</name>
            <url handle="http-wwwawagunscom">http://www.awaguns.com/</url>
            <type>
                <item handle="firearms">firearms</item>
            </type>
        </entry>

Haha! just a little trick embedded in there. I appended /item on the end and the page worked!

Thanks, everyone. It’s been quite educational.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details