Hide headings if no matching links
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?
- displayinglinks.xslt 2.38 KB
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
1 Posted by Ryan Griffith on 21 Oct, 2014 06:13 PM
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 Posted by rgraves2 on 21 Oct, 2014 06:50 PM
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 Posted by Ryan Griffith on 21 Oct, 2014 06:58 PM
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 Posted by rgraves2 on 21 Oct, 2014 07:22 PM
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 Posted by rgraves2 on 21 Oct, 2014 07:28 PM
It's not displaying anything now.
6 Posted by Ryan Griffith on 21 Oct, 2014 07:44 PM
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 Posted by rgraves2 on 21 Oct, 2014 07:53 PM
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 Posted by rgraves2 on 21 Oct, 2014 07:54 PM
Files attached. The captchas are hard to read and messing up my posts if I get them wrong.
9 Posted by Ryan Griffith on 22 Oct, 2014 01:43 PM
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 Posted by rgraves2 on 22 Oct, 2014 03:11 PM
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 Posted by Ryan Griffith on 22 Oct, 2014 06:31 PM
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 Posted by rgraves2 on 22 Oct, 2014 08:12 PM
Great, thanks again for all your help.
13 Posted by Ryan Griffith on 23 Oct, 2014 11:54 AM
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!
Ryan Griffith closed this discussion on 23 Oct, 2014 11:54 AM.