Hide headings if no matching links

rgraves2's Avatar

rgraves2

21 Oct, 2014 04:36 PM

I have an index block that indexes a list of links. I want to use a format to display only the links that match two data definition fields (subject and type).

First I find the subject of the page based on a calling page (e.g. English), stored in $guide variable.
Next I find all the links that that have subject = English
Then I display all those links under the appropriate type heading.

e.g.
Articles:
- Link 1
- Link 2

Encyclopedia:
- Link 3
- Link 4
- Link 5

This works, however if there are no links for a specific type (e.g. Articles), it still displays the Articles heading. I can't figure out how to use XSLT to determine if there are any matching links before I display the heading.

Any thoughts?

  1. 1 Posted by Ryan Griffith on 21 Oct, 2014 06:13 PM

    Ryan Griffith's Avatar

    Hi Robert,

    Using <xsl:key> is definitely the way to go. I've had some previous success with a slightly different variation of what you have, which I believe would make it easier to check the count as well as maintain the code in general since it's a little simpler.

    When you have a moment, give the attached Format a try for starters and let me know how it works out.

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by rgraves2 on 21 Oct, 2014 06:50 PM

    rgraves2's Avatar

    It's a good start. Only issue is that it displays the headings multiple times. So if there are 2 article links, it displays:

    Articles
    - Link 1
    - Link 2

    Articles
    - Link 1
    - Link 2

  3. 3 Posted by Ryan Griffith on 21 Oct, 2014 06:58 PM

    Ryan Griffith's Avatar

    Ah, my apologies Robert. I think i need to tweak the way the key is generated. When you have a moment, try the attached Format and let me know how it works out.

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by rgraves2 on 21 Oct, 2014 07:22 PM

    rgraves2's Avatar

    It's coming up blank now.

    I should mention the folder structure I'm using for the links. I'm storing them all in a folder called "resources". Originally I was putting them all in that folder but decided to make subfolders for each subject. So it is now:

    /resources/english/
    - Link 1
    - Link 2
    - Link 3
    ...
    /resources/mathematics/
    ...

    Does this make a difference?

  5. 5 Posted by rgraves2 on 21 Oct, 2014 07:28 PM

    rgraves2's Avatar

    It's not displaying anything now.

  6. 6 Posted by Ryan Griffith on 21 Oct, 2014 07:44 PM

    Ryan Griffith's Avatar

    Bummer, sorry about that.

    When you have a moment, if you could provide a sample XML being applied to this Format I would be more than happy to continue to work on it in my local instance. You can obtain the sample XML by editing the Format and, using the Preview Options at the top of the form, selecting the appropriate context assets (ie index block and context page).

    Please let me know if you have any questions.

    Thanks!

  7. 7 Posted by rgraves2 on 21 Oct, 2014 07:53 PM

    rgraves2's Avatar

    I've attached two samples.

    English should have some articles, encyclopedia and websites. The Classical Studies one only has Encyclopedia, so only that heading should appear.

    As you can see, I've grouped the links under the /resources/<name of subject> folders so the format should only have to look in the respective subject folder to find its links.

  8. 8 Posted by rgraves2 on 21 Oct, 2014 07:54 PM

    rgraves2's Avatar

    Files attached. The captchas are hard to read and messing up my posts if I get them wrong.

  9. 9 Posted by Ryan Griffith on 22 Oct, 2014 01:43 PM

    Ryan Griffith's Avatar

    Thank you for providing the sample XML, Robert.

    After a little bit of tinkering, I believe I was able to get the Format working as expected. When you have a moment, give the attached Format a try and let me know how it works out.

    Please let me know if you have any questions.

    Thanks!

  10. 10 Posted by rgraves2 on 22 Oct, 2014 03:11 PM

    rgraves2's Avatar

    Looks like this works. Thanks so much for your prompt replies.

    By chance, can you explain how the key() function works? I've read a bit about it but I don't understand how the information is stored which made it difficult for me to figure out how to use it.

    Thanks

  11. 11 Posted by Ryan Griffith on 22 Oct, 2014 06:31 PM

    Ryan Griffith's Avatar

    Thank you for following up, Robert. I am glad to hear the updated Format did the trick.

    I'd have to say <xsl:key> is probably one of the most difficult things about XSLT. I think of it as a lookup table for elements based on what you enter for the match and use attributes.

    In the code I provided, that first loop is basically looping over the unique values of the type dynamic metadata fields (this is what the XPath that uses the key() method is doing). Since we are looping over the unique values, we can sort on those values as well as filter elements within the XML based on the current value.

    Please let me know if you have any questions.

    Thanks!

  12. 12 Posted by rgraves2 on 22 Oct, 2014 08:12 PM

    rgraves2's Avatar

    Great, thanks again for all your help.

  13. 13 Posted by Ryan Griffith on 23 Oct, 2014 11:54 AM

    Ryan Griffith's Avatar

    Not a problem at all, Robert. I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.

    Have a great day!

  14. Ryan Griffith closed this discussion on 23 Oct, 2014 11:54 AM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM