Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Frederik T 242 posts 373 karma points
    Nov 03, 2011 @ 10:37
    Frederik T
    0

    Selecting multiple XML nodes and more

    Hopefully this is somewhat simple.

    I have this gigantic completly unoptimized xslt (optimizing it is for a later date because its ugly and im not proud of it) that gets job categories from an xml webservice.

    For each category it checks if they have any contents and changes the CSS markup accordingly, it also counts the number of items.

    <li>
    <a href="#filter" title="Kontor" data-option-value=".category109">
    <xsl:for-each select="$Service/ArrayOfJobCategory/JobCategory">
    <xsl:if test="CategoryID = 109">
    <xsl:if test="RsJobCount = 0">
    <xsl:attribute name="class">inactive</xsl:attribute>
    </xsl:if>
    </xsl:if>
    </xsl:for-each>
    Kontor
    <span>
    <xsl:for-each select="$Service/ArrayOfJobCategory/JobCategory">
    <xsl:if test="CategoryID = 109">
    <xsl:choose>
    <xsl:when test="RsJobCount > 0" >
    <xsl:value-of select="RsJobCount"/>
    </xsl:when>
    <xsl:otherwise>
    <xsl:value-of select="RsJobCount"/>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:if>
    </xsl:for-each>
    </span>
    </a>
    </li>

    Yes, that piece of code is repeated about 20 times for each category.

    But anyway, the problem is that some categories has to be condensed into fewer, so an example with the above, category number 109, it now also has to contain the contents from category 101.

    How do i do that? (im guessing my explanation isnt that comprehensive but please ask for further info, i will answer asap).

  • Frederik T 242 posts 373 karma points
    Nov 03, 2011 @ 13:47
    Frederik T
    0

    Sorry for double posting, but editing my original post didnt work.

    Ok, i will try and clarify the bare minimum i need help with. In the code above, i am navigating through an XML retrieved from a webservice ($service), it is supposed to first check if it is as the specific category ID, then check if its zero and if it is change the "class" attribute.

    Then after the category name ("Kontor" in this case) it looks for the specific category again, and checks the value and outputs it, to show the user the number of items in that category.

    Now, several categories has been merged, so some now containg contents of two categories, some three. The problem is i dont know how to check the RsJobCount value for ALL of the categories, and output the sum of RsJobCount for those categories.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 03, 2011 @ 22:33
    Chriztian Steinmeier
    1

    Hi Frederik,

    Here's a way to do it - hopefully it explains itself enough for you to be able to wipe a couple of hundred lines off the file :-)

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        xmlns:make="urn:schemas-microsoft-com:xslt"
        exclude-result-prefixes="umb make"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
        <xsl:variable name="Service" select="--GRAB_FEED_HERE--" />
    
        <!-- Create a lookup table of names to go with the CategoryID ids -->
        <xsl:variable name="categories" select="$Service/ArrayOfJobCategory/JobCategory" />
        <xsl:variable name="categoryProxy">
            <cat id="101">Kontor</cat>
            <cat id="109">Offentlig</cat>
            <cat id="110">Privat</cat>
            <cat id="109_110">Private og Offentlige</cat>
        </xsl:variable>
        <xsl:variable name="category" select="make:node-set($categoryProxy)/cat" />
    
        <xsl:template match="/">
            <ul>
                <!-- Process all <JobCategory> nodes from $Service -->
                <xsl:apply-templates select="$categories">
                    <xsl:sort select="CategoryID" data-type="number" order="ascending" />
                </xsl:apply-templates>
            </ul>
        </xsl:template>
    
        <xsl:template match="JobCategory">
            <xsl:variable name="categoryName" select="$category[@id = current()/CategoryID]" />
            <li>
                <a href="#filter" title="{$categoryName}" data-option-value=".category{CategoryID}">
                    <xsl:if test="RsJobCount = 0">
                        <xsl:attribute name="class">inactive</xsl:attribute>
                    </xsl:if>
    
                    <xsl:value-of select="$categoryName" />
    
                    <span>
                        <xsl:choose>
                            <xsl:when test="RsJobCount = 0">(Ingen)</xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="RsJobCount" />
                            </xsl:otherwise>
                        </xsl:choose>
                    </span>
                </a>
            </li>
        </xsl:template>
    
        <!-- Specific for merging 109 + 110 -->
        <xsl:template match="JobCategory[CategoryID = 109]">
            <xsl:variable name="categoryName" select="$category[@id = '109_110']" />
            <xsl:variable name="jobCount" select="sum(../JobCategory[CategoryID = 109 or CategoryID = 110]/RsJobCount)" />
            <li>
                <a href="#filter" title="{$categoryName}" data-option-value=".category109_110">
                    <xsl:if test="$jobCount = 0">
                        <xsl:attribute name="class">inactive</xsl:attribute>
                    </xsl:if>
    
                    <xsl:value-of select="$categoryName" />
    
                    <span>
                        <xsl:choose>
                            <xsl:when test="$jobCount = 0">(Ingen)</xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="$jobCount" />
                            </xsl:otherwise>
                        </xsl:choose>
                    </span>
                </a>
            </li>     
        </xsl:template>
    
        <xsl:template match="JobCategory[CategoryID = 110]">
            <!-- Skipping this - handled by '109' (You can match more than one in that pattern...) -->
        </xsl:template>
    
    </xsl:stylesheet>
    

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft