Search code examples
xsltc1-cms

Find item in function output


I have two functions:

GetHomeXml() -> returns a collection of pages with a custom meta type
SiteMapXml() -> standard sitemap xml

SiteMapXml() has a property (isCurrent) that gets returned if the page is the current page. I need to determine if the name of the page appears in GetHomeXml(), and whether the node in GetHomeXml() has an Image.FileName property assigned to it.

I can get both returned, but I cannot figure out the XSL to accomplish this. Following is what I have so far:

<xsl:param name="currentPage" select="/in:inputs/in:result[@name='SitemapXml']/Page/@iscurrent" />

<xsl:for-each select="/in:inputs/in:result[@name='GetHomeXml']/Image">
   <xsl:if test="@Image.PageTitle = $currentPage.Title">
      <img src="@Image.FileName" />
   </xsl:if>
</xsl:for-each>

I created a custom meta data entry, and added 3 fields to it {IsTab, Image, Color}. I want to determine that if the current page contains an Image field, and the Page.Title assigned to it, is the same as the current page, to render the designated image (it's a data reference field, with an image lookup property to the images directory) into the tag.

But this code does not seem to return anything?

Update:

The XML that is returned for GetHomeXml() is as follows:

<!-- Function Call Result (0 ms), XPath /in:inputs/in:result[@name='GetHomeXml']/IsTab -->
 <in:result name="GetHomeXml">
    <IsTab Id="9eba448e-9a30-478e-81b2-530bc7da2545" IsTab="true" BackgroundImg="MediaArchive:34f9be39-8273-4960-8cc6-e6b76f34e6ac" BackgroundImg.FileName="home-group-2.png" PageId.Title="Contact Us" xmlns=""/>
    <IsTab Id="a46e2e98-ffcd-4675-8840-389d1a7f46ca" IsTab="true" PageId.Title="Welcome" xmlns=""/>
    <IsTab Id="c76fa101-8c63-46e2-9431-e18ce875866d" IsTab="false" PageId.Title="What we do" xmlns=""/>

I want to get the name of the image, based on the current page (PageId.Title) I need to match back to the returned value associated with the current page in SiteMapXml:

<!-- Function Call Result (0 ms), XPath /in:inputs/in:result[@name='SitemapXml']/Page -->
 <in:result name="SitemapXml">
     <Page Id="62c66cb6-b2ec-4469-b0d7-54bc61b22c20" Title="Home" MenuTitle="Home" UrlTitle="Home" Description="Default web site for... Please do not change any of the settings for this site. Add, modify and delete pages underneath this web site, but do no touch this site." ChangedDate="2011-12-04T22:36:33.2651194+02:00" ChangedBy="admin" URL="/Home/c1mode(unpublished)" Depth="1" xmlns="">
        <Page Id="041c7d66-60cd-4098-ac98-728c0db111a1" Title="Welcome" MenuTitle="Welcome" UrlTitle="Welcome" Description="" ChangedDate="2011-12-04T22:38:06.2815949+02:00" ChangedBy="admin" URL="/Home/Welcome/c1mode(unpublished)" Depth="2"/>
        <Page Id="8ae4d8a5-f4d9-43ed-85de-90b6d3a6f0b8" Title="Contact Us" MenuTitle="Contact Us" UrlTitle="Contact-Us" Description="" ChangedDate="2011-12-04T22:54:10.1503871+02:00" ChangedBy="admin" URL="/Home/Contact-Us/c1mode(unpublished)" isopen="true" iscurrent="true" Depth="2"/>
        <Page Id="ed5560a4-140b-4851-ac19-5ddc6c66a770" Title="What we do" MenuTitle="What we do" UrlTitle="What-we-do" Description="" ChangedDate="2011-11-28T07:23:25.8851421+02:00" ChangedBy="admin" URL="/Home/What-we-do/c1mode(unpublished)" Depth="2"/>
    </Page>
</in:result>
</in:result>

Solution

  • Instead of looping through the resultset, use a xpath predicate to match the page by title. For example, the URL of the welcome page would be given by:

    $pages[@Page.Title='Welcome']/@URL
    

    Here is a complete XSLT which shows the URLs for the HomeXML pages:

    <xsl:param name="homeXml" select="/in:inputs/in:result[@name='GetHomeXml']/IsTab" />
    <xsl:param name="sitemap" select="/in:inputs/in:result[@name='SitemapXml']/Page" />
    <xsl:template match="/">
        <html>
            <head>
                <!-- markup placed here will be shown in the head section of the rendered page -->
            </head>
            <body>
                <xsl:for-each select="$homeXml">
                    <xsl:variable name="pageTitle" select="@PageId.Title" />
                    <xsl:variable name="sitemapPage" select="$sitemap//Page[@Title=$pageTitle]/@URL" />
                    <xsl:value-of select="$sitemapPage" />
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
    

    Corresponding XsltCake (like JsFiddle, but for Xslt)