I have an .xml which I need to display using XSL. Now, the entities in the XML are videos with tags. Not sure if it matters, but a video can have multiple tags.
Goal: What I need to do, is to (for a specific page) show all items that have the tag “cat1” for example. This is simple enough, I’ll just use an if:
<xsl:if test="tag[. ="cat1"]">
However, it should also NOT list the h1-tag in case there are no items tagged with “cat1” – that should only be displayed IF there are any videos with the tag “cat1”.
Problem: If there ARE videos with “cat1” present, no problem.
If there ARE NO videos with the tag “cat1” present, the h1-tag will be shown, but no videos. This is obviously not acceptable.
So the question becomes;
How do I filter the video-items first by checking their tags, and only after making sure there are still videos left, display the rest of the code?
Here is the code for the xml:
<videos>
<video>
<title>Video title</title>
<subtitle></subtitle>
<description_long>
<![CDATA[Description]]>
</description_long>
<link language="English">http://URL here</link>
<tag>cat1</tag>
</video>
</demos>
Here is the code for the .xsl I’ve been trying:
<xsl:template match="/">
<xsl:for-each select="videos/video">
<xsl:sort data-type="text" order="ascending" />
<xsl:if test="tag[. ="cat1"]">
<h1>Category 1 videos</h1>
<div class="video">
<xsl:value-of select="title" />
</div>
</xsl:if>
</xsl:choose>
</xsl:for-each>
</xsl:template>
This transformation:
when applied on the provided XML document (corrected to be well-formed and added a second
videoelement):produces the wanted result:
When applied on this XML document (no cat1):
>
again the wanted answer (nothing) is produced.