Reputation:
I have a sharepoint list which is designed like below. Here is a sample data of input.
Name ID Navigation_URL ParentID IsShow
Test1 1 # 0 Yes
Test2 2 # 0 Yes
Test2.1 3 # 2 Yes
Test2.1.14 # 3 Yes
How to create an unordered list using xslt function. Output should be like:
<ul>
<li>Test1</li>
<li>Test2
<ul>
<li>Test2.1
<ul>
<li>Test2.1.1</li>
</ul>
</li>
</ul>
</li>
</ul>
Upvotes: 1
Views: 1025
Reputation: 117073
I know nothing about SharePoint, I can only help you with the XSLT part. Once you have an XML document such as:
XML
<root>
<Item>
<Name>Test1</Name>
<ID>1</ID>
<ParentID>0</ParentID>
</Item>
<Item>
<Name>Test2</Name>
<ID>2</ID>
<ParentID>0</ParentID>
</Item>
<Item>
<Name>Test2.1</Name>
<ID>3</ID>
<ParentID>2</ParentID>
</Item>
<Item>
<Name>Test2.1.1</Name>
<ID>4</ID>
<ParentID>3</ParentID>
</Item>
</root>
the following stylesheet:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:key name="item-by-parent" match="Item" use="ParentID" />
<xsl:template match="/root">
<ul>
<xsl:apply-templates select="Item[ParentID='0']"/>
</ul>
</xsl:template>
<xsl:template match="Item">
<xsl:variable name="children" select="key('item-by-parent', ID)" />
<li>
<xsl:value-of select="Name"/>
<xsl:if test="$children">
<ul>
<xsl:apply-templates select="$children"/>
</ul>
</xsl:if>
</li>
</xsl:template>
</xsl:stylesheet>
will transform it to:
Result
<ul>
<li>Test1</li>
<li>Test2<ul>
<li>Test2.1<ul>
<li>Test2.1.1</li>
</ul>
</li>
</ul>
</li>
</ul>
One thing is missed isShow ... It's a node/column based on which I can exclude or not show that corresponding item
You could just add another template:
<xsl:template match="Item[IsShow='No']"/>
This is assuming that excluding an item is also supposed to exclude its descendants.
Upvotes: 1