Sorting items after building an XML feed?

34 Views Asked by At

I am working in a CMS (Modern Campus) that is XML-based. XSLT is still a thing i'm attempting to learn on the fly, so I am not well-versed in this area yet.

Our CMS is building a blog feed (which we use for news) using the following code:

        <items>
        <!--Internal News Articles-->
        <xsl:for-each select="$internal-doc/items/item">
            <xsl:sort select="concat(substring(pubDate,7,4), substring(pubDate,1,2),substring(pubDate,4,2))" order="descending"/>
            <xsl:if test="not(articlePreview) or articlePreview != 'true'">
                <item>
                    <xsl:apply-templates select="attribute()|node()" />
                    <searchtags>{string-join(tags/tag, ',')}</searchtags>
                </item>
            </xsl:if>
        </xsl:for-each>     
        <!--External News Articles-->
        <xsl:if test="$include-broadcast='true'"> <!--Check to see if broadcast option is on, if so, get external news articles-->
            <xsl:for-each select="$array">
            <xsl:variable name="external-doc" select="(.)" /> <!--Get current array item-->
            <xsl:variable name="base-url" select="tokenize($external-doc, '/_resources')[1]" /> <!--Get website URL, need to use it for link to news and images-->
            <xsl:for-each select="$external-doc/document(.)/items/item">
                <xsl:sort select="concat(substring(pubDate,7,4), substring(pubDate,1,2),substring(pubDate,4,2))" order="descending"/>
                <xsl:if test="contains(broadcast,$broadcast-unit)">
                    <item>
                        <xsl:attribute name="href" select="concat($base-url,@href)"/>
                        <xsl:apply-templates select="attribute()[not(name() = 'href')]|node()[not(name() = 'hero-image') and not(name() = 'image')]" /> 
                        <xsl:if test="image">
                            <image>
                                <xsl:choose>
                                    <xsl:when test="not(contains(image/img/@src,'://'))">
                                        <img>
                                            <xsl:attribute name="src" select="concat($base-url,image/img/@src)"/>
                                            <xsl:apply-templates select="image/img/attribute()[not(name() = 'src')]" />
                                        </img>
                                    </xsl:when>
                                    <xsl:otherwise><xsl:apply-templates select="image/img" /></xsl:otherwise>
                                </xsl:choose>
                            </image>
                        </xsl:if>
                        <searchtags>{string-join(tags/tag, ',')}</searchtags>
                    </item> 
                </xsl:if>
            </xsl:for-each>
        </xsl:for-each>
        </xsl:if>
    </items>

The code works great - the second half of the code looks at an external blog feed to see if it needs to bring in any items from it. This code works as well. The issue I'm having, or unsure of how to accomplish, is performing a final sort based on the pubDate. As it currently stands, when the feed is built, the internal news articles are sorted, and then when the external news articles are added, they too are sorted, but both are sorted separately. What I want is a final sort so that if an external news article happens to have a newer date than an internal news article, it will appear at the top of the feed, not at the end of the feed, as the code will presently do. How can I accomplish this?

0

There are 0 best solutions below