Reputation: 7407
I have this XML:
<TreeView>
<Parent text="Installation">
<child company="all" text="Startup" type="video" file="startup.mp4"/>
<child company="all" text="Getting there" type="video" file="small.mp4"/>
<child company="all" text="Steps" type="pdf_document" file="test.pdf"/>
<child company="all" text="Pictures" type="presentation" file="pics.ppx"/>
</Parent>
<Parent text="Usage">
<child company="B" text="Tilbud pane" type="video" file="b1.mp4"/>
<child company="B" text="Report pane" type="pdf_document" file="b2.pdf"/>
<child company="N" text="Tilbud pane" type="video" file="n1.mp4"/>
<child company="N" text="Report pane" type="pdf_document" file="n2.pdf"/>
<child company="D" text="Tilbud pane" type="video" file="d1.mp4"/>
<child company="D" text="Report pane" type="pdf_document" file="d2.pdf"/>
</Parent>
</TreeView>
And this XSLT so far:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="/">
<ul id="LinkedList1" class="LinkedList">
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="Parent">
<li>
<xsl:value-of select="@text"/>
<br/>
<ul>
<xsl:apply-templates select="child"/>
</ul>
</li>
</xsl:template>
<xsl:template match="child[@type='video']">
<li>
<a href="{@file}" class="video">
<img src="play_icon.png" alt="video" title="Video tutorial"/>
<xsl:text> </xsl:text>
<xsl:value-of select="@text"/>
</a>
</li>
</xsl:template>
<xsl:template match="child[@type='pdf_document']">
<li>
<a href="{@file}" class="pdfdoc">
<img src="pdf_icon.png" alt="pdfdoc" title="PDF Document"/>
<xsl:text> </xsl:text>
<xsl:value-of select="@text"/>
</a>
</li>
</xsl:template>
<xsl:template match="child[@type='presentation']">
<li><a href="{@file}" class="presentation">
<img src="powerpoint_icon.png" alt="presentation" title="Power Point presentation"/>
<xsl:text> </xsl:text>
<xsl:value-of select="@text"/>
</a>
</li>
</xsl:template>
</xsl:stylesheet>
It is working perfectly, as intended, but I want to add one more feature to the transformation. I want, depending on the value of company attribute, to include the whole element, or to skip it.
Elaboration: the Child elements, whose company attribute value is "all" must always be included in the transformed file. Afterwards, the rest of the Child elements should be grouped only if they have, i.e., company attribute value "B". Then I will have 3 different XSL files for the different companies. So I need the XSL code only for one company for now.
I am not sure whether I have to use somehow conditional statement or templates for that. I am just bugged, cuz my XSL file became kinda' complicated for me.
If someone can make the addition with my requirements, to the existing code - it will be greatly appreciated.
Upvotes: 0
Views: 571
Reputation: 1796
So if I use this source XML:
<TreeView>
<Parent text="Installation">
<child company="all" text="Startup" type="video" file="startup.mp4"/>
<child company="all" text="Getting there" type="video" file="small.mp4"/>
<child company="all" text="Steps" type="pdf_document" file="test.pdf"/>
<child company="all" text="Pictures" type="presentation" file="pics.ppx"/>
</Parent>
<Parent text="Usage">
<child company="B" text="Tilbud pane" type="video" file="b1.mp4"/>
<child company="B" text="Report pane" type="pdf_document" file="b2.pdf"/>
<child company="N" text="Tilbud pane" type="video" file="n1.mp4"/>
<child company="N" text="Report pane" type="pdf_document" file="n2.pdf"/>
<child company="D" text="Tilbud pane" type="video" file="d1.mp4"/>
<child company="D" text="Report pane" type="pdf_document" file="d2.pdf"/>
</Parent>
and apply this XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<ul id="LinkedList1" class="LinkedList">
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="Parent">
<li>
<xsl:value-of select="@text"/>
<br />
<ul>
<xsl:apply-templates select="child[@company='all' or @company='B']"/>
</ul>
</li>
</xsl:template>
<xsl:template match="child[@type='video']">
<li>
<a href="{@file}" class="video">
<img src="play_icon.png" alt="video" title="Video tutorial">
<xsl:text> </xsl:text>
<xsl:value-of select="@text"/>
</img>
</a>
</li>
</xsl:template>
<xsl:template match="child[@type='pdf_document']">
<li>
<a href="{@file}" class="pdfdoc">
<img src="pdf_icon.png" alt="pdfdoc" title="PDF Document">
<xsl:text> </xsl:text>
<xsl:value-of select="@text"/>
</img>
</a>
</li>
</xsl:template>
<xsl:template match="child[@type='presentation']">
<li>
<a href="{@file}" class="presentation">
<img src="powerpoint_icon.png" alt="presentation" title="Power Point presentation">
<xsl:text> </xsl:text>
<xsl:value-of select="@text"/>
</img>
</a>
</li>
</xsl:template>
</xsl:stylesheet>
you get this output:
<ul class="LinkedList" id="LinkedList1">
<li>Installation<br/>
<ul>
<li>
<a class="video" href="startup.mp4">
<img title="Video tutorial" alt="video" src="play_icon.png"> Startup</img>
</a>
</li>
<li>
<a class="video" href="small.mp4">
<img title="Video tutorial" alt="video" src="play_icon.png"> Getting there</img>
</a>
</li>
<li>
<a class="pdfdoc" href="test.pdf">
<img title="PDF Document" alt="pdfdoc" src="pdf_icon.png"> Steps</img>
</a>
</li>
<li>
<a class="presentation" href="pics.ppx">
<img title="Power Point presentation" alt="presentation"
src="powerpoint_icon.png"> Pictures</img>
</a>
</li>
</ul>
</li>
<li>Usage<br/>
<ul>
<li>
<a class="video" href="b1.mp4">
<img title="Video tutorial" alt="video" src="play_icon.png"> Tilbud pane</img>
</a>
</li>
<li>
<a class="pdfdoc" href="b2.pdf">
<img title="PDF Document" alt="pdfdoc" src="pdf_icon.png"> Report pane</img>
</a>
</li>
</ul>
</li>
</ul>
Consider the <xsl:output indent="yes" omit-xml-declaration="yes"/>
in the stylesheet. In your original stylesheet you have output=html
which causes your <br/>
elements to "loose" the closing tag.
Upvotes: 1